-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (128 loc) · 2.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"
"github.com/awalterschulze/gographviz"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
source := flag.String("s", "", "data source (required)")
output := flag.String("o", "", "output file")
flag.Parse()
if *source == "" {
flag.Usage()
return fmt.Errorf("-s data source is must be required")
}
words, err := readWords(*source)
if err != nil {
return err
}
dotfile, err := generateDotfile(generateTrie(words), *output)
fmt.Println(dotfile)
return err
}
// ファイルから入力
func readWords(source string) ([]string, error) {
f, err := os.Open(source)
if err != nil {
return nil, err
}
defer f.Close()
words := []string{}
s := bufio.NewScanner(f)
for s.Scan() {
words = append(words, s.Text())
}
if err := s.Err(); err != nil {
return nil, err
}
return words, nil
}
// Trie木を生成
func generateTrie(words []string) *Node {
t := newNode("", make(map[rune]*Node), false)
for _, w := range words {
t.insert(w)
}
return t
}
var id = 0
type Node struct {
ID int
Key string
Children map[rune]*Node
End bool
}
func newNode(k string, c map[rune]*Node, e bool) *Node {
id++
return &Node{
ID: id,
Key: k,
Children: c,
End: e,
}
}
func (n *Node) getLabel() string {
if n.Key == "" {
return "\"0\""
}
return fmt.Sprintf("\"%s\"", n.Key)
}
func (n *Node) getShape() string {
if n.End {
return "\"doublecircle\""
}
return "\"circle\""
}
func (n *Node) insert(w string) error {
runes := []rune(w)
currentNode := n
for i, r := range runes {
if nextNode, ok := currentNode.Children[r]; ok {
currentNode = nextNode
} else {
currentNode.Children[r] = newNode(string(r), make(map[rune]*Node), false)
currentNode = currentNode.Children[r]
}
// 終端にチェック
if i == len(runes)-1 {
currentNode.End = true
}
}
return nil
}
// TODO: error handling
// Trie木からdotファイルを生成
func generateDotfile(trie *Node, output string) (string, error) {
g := gographviz.NewGraph()
g.SetName("G")
g.SetDir(true)
var fontSize = "35"
var visitAll func(n *Node)
visitAll = func(n *Node) {
for _, v := range n.Children {
g.AddNode("G", strconv.Itoa(n.ID), map[string]string{"label": n.getLabel(), "shape": n.getShape(), "fontsize": fontSize})
g.AddNode("G", strconv.Itoa(v.ID), map[string]string{"label": v.getLabel(), "shape": v.getShape(), "fontsize": fontSize})
g.AddEdge(strconv.Itoa(n.ID), strconv.Itoa(v.ID), true, nil)
visitAll(v)
}
}
visitAll(trie)
if output != "" {
f, err := os.Create(output)
if err != nil {
return "", err
}
f.WriteString(g.String())
return "", nil
}
return g.String(), nil
}