2015-09-29 22:35:25 +00:00
|
|
|
package adjacency
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2015-10-01 13:38:42 +00:00
|
|
|
// "fmt"
|
2015-10-05 19:56:21 +00:00
|
|
|
"path/filepath"
|
2015-09-29 22:35:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type AdjacencyGraph struct {
|
|
|
|
Graph map[string][6]string
|
2015-10-05 19:56:21 +00:00
|
|
|
averageDegree float32
|
|
|
|
Name string
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 19:56:21 +00:00
|
|
|
|
|
|
|
var AdjacencyGph []AdjacencyGraph;
|
2015-09-29 22:35:25 +00:00
|
|
|
func init(){
|
|
|
|
//todo get currentloc so that i don't have to know the whole path
|
|
|
|
log.SetFlags(log.Lshortfile)
|
2015-10-05 19:56:21 +00:00
|
|
|
AdjacencyGph = append(AdjacencyGph, buildQwerty())
|
|
|
|
AdjacencyGph = append(AdjacencyGph, buildDvorak())
|
|
|
|
AdjacencyGph = append(AdjacencyGph, buildKeypad())
|
|
|
|
AdjacencyGph = append(AdjacencyGph, buildMacKeypad())
|
|
|
|
|
|
|
|
|
2015-09-29 22:35:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildQwerty() AdjacencyGraph {
|
2015-10-05 19:56:21 +00:00
|
|
|
filePath, _ := filepath.Abs("adjacency/Qwerty.json")
|
|
|
|
return getAdjancencyGraphFromFile(filePath, "qwerty")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
func buildDvorak() AdjacencyGraph {
|
2015-10-05 19:56:21 +00:00
|
|
|
filePath, _ := filepath.Abs("adjacency/Dvorak.json")
|
|
|
|
return getAdjancencyGraphFromFile(filePath, "dvorak")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
func buildKeypad() AdjacencyGraph {
|
2015-10-05 19:56:21 +00:00
|
|
|
filePath, _ := filepath.Abs("adjacency/Keypad.json")
|
|
|
|
return getAdjancencyGraphFromFile(filePath, "keypad")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
func buildMacKeypad() AdjacencyGraph {
|
2015-10-05 19:56:21 +00:00
|
|
|
filePath, _ := filepath.Abs("adjacency/MacKeypad.json")
|
|
|
|
return getAdjancencyGraphFromFile(filePath, "mac_keypad")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 19:56:21 +00:00
|
|
|
func getAdjancencyGraphFromFile(filePath string, name string) AdjacencyGraph {
|
2015-09-29 22:35:25 +00:00
|
|
|
data, err := ioutil.ReadFile(filePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var graph AdjacencyGraph;
|
|
|
|
err = json.Unmarshal(data, &graph)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-10-05 19:56:21 +00:00
|
|
|
graph.Name = name
|
2015-09-29 22:35:25 +00:00
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:38:42 +00:00
|
|
|
//on qwerty, 'g' has degree 6, being adjacent to 'ftyhbv'. '\' has degree 1.
|
|
|
|
//this calculates the average over all keys.
|
|
|
|
//TODO double check that i ported this correctly scoring.coffee ln 5
|
|
|
|
func (adjGrp AdjacencyGraph) CalculateAvgDegree() (float32) {
|
2015-10-05 19:56:21 +00:00
|
|
|
if adjGrp.averageDegree != float32(0) {
|
|
|
|
return adjGrp.averageDegree
|
|
|
|
}
|
2015-10-01 13:38:42 +00:00
|
|
|
var avg float32
|
|
|
|
var count float32
|
|
|
|
for _, value := range adjGrp.Graph {
|
|
|
|
|
|
|
|
for _, char := range value {
|
|
|
|
if char != "" || char != " " {
|
|
|
|
avg += float32(len(char))
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-05 19:56:21 +00:00
|
|
|
adjGrp.averageDegree = avg/count
|
|
|
|
|
|
|
|
return adjGrp.averageDegree
|
2015-10-01 13:38:42 +00:00
|
|
|
}
|
|
|
|
|