-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
141 lines (117 loc) · 2.79 KB
/
parse.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
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
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
func (s *SearchNode) String() string {
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)
// Build testaments, books, chapters, verses
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 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]
t := strings.ToLower(par.Text)
catechism.searchTree.Add(t, SearchNode{
Part: &p,
Article: &a,
Section: &s,
Paragraph: &par,
})
}
}
}
}
// Build summary
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,
ArticleNumber: a.ArticleNumber,
})
}
c.PartSummary = append(c.PartSummary, ps)
}
}
// Search finds top matching verses based on the given query.
// The number of search results are 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
}