-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse.go
More file actions
159 lines (135 loc) · 3.39 KB
/
parse.go
File metadata and controls
159 lines (135 loc) · 3.39 KB
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
149
150
151
152
153
154
155
156
157
158
159
package romanus
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"path"
"runtime"
"strings"
"github.com/derekparker/trie"
)
// SearchNode represents a search result for a given paragraph inquiry. Article
// is nil when the match falls inside a part's Introduction.
type SearchNode struct {
Part *Part
Article *Article
Section *Section
Paragraph *Paragraph
}
// String creates a string representation for the SearchNode,
// ex. Part 1, Article 1, Section 1, Paragraph 1; for hits inside a part's
// Introduction the form is Part N, Introduction, Section M, Paragraph K.
func (s *SearchNode) String() string {
if s.Article == nil {
return fmt.Sprintf("Part %d, Introduction, Section %d, Paragraph %d",
s.Part.PartNumber,
s.Section.SectionNumber,
s.Paragraph.ParagraphNumber)
}
return fmt.Sprintf("Part %d, Article %d, Section %d, Paragraph %d",
s.Part.PartNumber,
s.Article.ArticleNumber,
s.Section.SectionNumber,
s.Paragraph.ParagraphNumber)
}
const catechismTar = "catechism.tar.gz"
const catechismFilename = "catechism.json"
// NewCatechism parses and creates a new catechism instance
func NewCatechism() *Catechism {
_, currFile, _, _ := runtime.Caller(0)
filename := fmt.Sprintf("%s/%s", path.Dir(currFile), catechismTar)
catechism := &Catechism{
searchTree: trie.New(),
}
f, _ := os.Open(filename)
defer f.Close()
gzf, _ := gzip.NewReader(f)
defer gzf.Close()
tr := tar.NewReader(gzf)
for {
h, err := tr.Next()
if err == io.EOF {
break
}
switch h.Name {
case catechismFilename:
catechism.Parts = decode(tr)
}
}
// Build search tree
for i := range catechism.Parts {
p := &catechism.Parts[i]
for k := range p.Introduction.Sections {
s := &p.Introduction.Sections[k]
for l := range s.Paragraphs {
par := &s.Paragraphs[l]
catechism.searchTree.Add(strings.ToLower(par.Text), SearchNode{
Part: p,
Article: nil,
Section: s,
Paragraph: par,
})
}
}
for j := range p.Articles {
a := &p.Articles[j]
for k := range a.Sections {
s := &a.Sections[k]
for l := range s.Paragraphs {
par := &s.Paragraphs[l]
catechism.searchTree.Add(strings.ToLower(par.Text), SearchNode{
Part: p,
Article: a,
Section: s,
Paragraph: par,
})
}
}
}
}
catechism.buildSummary()
return catechism
}
func decode(r io.Reader) []Part {
var parts []Part
json.NewDecoder(r).Decode(&parts)
return parts
}
func (c *Catechism) buildSummary() {
c.PartSummary = []PartSummary{}
for i := range c.Parts {
part := c.Parts[i]
ps := PartSummary{
Title: part.Title,
PartNumber: part.PartNumber,
Articles: []ArticleSummary{},
}
for j := range part.Articles {
a := part.Articles[j]
ps.Articles = append(ps.Articles, ArticleSummary{
Title: a.Title,
Heading: a.Heading,
ArticleNumber: a.ArticleNumber,
})
}
c.PartSummary = append(c.PartSummary, ps)
}
}
// Search finds top matching paragraphs based on the given query.
// The number of search results is restricted by maxResults.
func (c *Catechism) Search(query string, maxResults int) []SearchNode {
t := c.searchTree
keys := t.FuzzySearch(strings.ToLower(query))
var nodes []SearchNode
for k := range keys {
res, _ := t.Find(keys[k])
nodes = append(nodes, res.Meta().(SearchNode))
if len(nodes) >= maxResults {
break
}
}
return nodes
}