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-06 18:43:37 +00:00
|
|
|
averageDegree float64
|
2015-10-05 19:56:21 +00:00
|
|
|
Name string
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 19:56:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-29 22:35:25 +00:00
|
|
|
func buildQwerty() AdjacencyGraph {
|
2015-10-06 18:43:37 +00:00
|
|
|
filePath, _ := filepath.Abs("Qwerty.json")
|
|
|
|
return GetAdjancencyGraphFromFile(filePath, "qwerty")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
func buildDvorak() AdjacencyGraph {
|
2015-10-06 18:43:37 +00:00
|
|
|
filePath, _ := filepath.Abs("Dvorak.json")
|
|
|
|
return GetAdjancencyGraphFromFile(filePath, "dvorak")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
func buildKeypad() AdjacencyGraph {
|
2015-10-06 18:43:37 +00:00
|
|
|
filePath, _ := filepath.Abs("Keypad.json")
|
|
|
|
return GetAdjancencyGraphFromFile(filePath, "keypad")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
func buildMacKeypad() AdjacencyGraph {
|
2015-10-06 18:43:37 +00:00
|
|
|
filePath, _ := filepath.Abs("MacKeypad.json")
|
|
|
|
return GetAdjancencyGraphFromFile(filePath, "mac_keypad")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 18:43:37 +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
|
2015-10-06 18:43:37 +00:00
|
|
|
func (adjGrp AdjacencyGraph) CalculateAvgDegree() (float64) {
|
|
|
|
if adjGrp.averageDegree != float64(0) {
|
2015-10-05 19:56:21 +00:00
|
|
|
return adjGrp.averageDegree
|
|
|
|
}
|
2015-10-06 18:43:37 +00:00
|
|
|
var avg float64
|
|
|
|
var count float64
|
2015-10-01 13:38:42 +00:00
|
|
|
for _, value := range adjGrp.Graph {
|
|
|
|
|
|
|
|
for _, char := range value {
|
|
|
|
if char != "" || char != " " {
|
2015-10-06 18:43:37 +00:00
|
|
|
avg += float64(len(char))
|
2015-10-01 13:38:42 +00:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-05 19:56:21 +00:00
|
|
|
adjGrp.averageDegree = avg/count
|
|
|
|
|
|
|
|
return adjGrp.averageDegree
|
2015-10-01 13:38:42 +00:00
|
|
|
}
|
|
|
|
|