-
Notifications
You must be signed in to change notification settings - Fork 3
/
morph_data.go
171 lines (148 loc) · 3.5 KB
/
morph_data.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package opencorpora
import (
"compress/bzip2"
"encoding/gob"
"encoding/xml"
"io"
"net/http"
"os"
"sort"
"github.com/pahanini/mafsa"
)
const dictURL = "http://opencorpora.org/files/export/dict/dict.opcorpora.xml.bz2"
// MorphData is a struct to encode and save
type MorphData struct {
Tree []byte
Tag Tag
Metas [][]Meta
}
// ImportFromWeb imports data from opencorpora site
func (d *MorphData) ImportFromWeb() (err error) {
resp, err := http.Get(dictURL)
if err != nil {
return err
}
defer resp.Body.Close()
bz2 := bzip2.NewReader(resp.Body)
return d.ImportFromReader(bz2)
}
// ImportFromXMLFile reads XML file and saves in MorphData
func (d *MorphData) ImportFromXMLFile(fp string) (err error) {
f, err := os.Open(fp)
if err != nil {
return err
}
return d.ImportFromReader(f)
}
// ImportFromReader reads XML data from reader and saves in MorphData
func (d *MorphData) ImportFromReader(r io.Reader) (err error) {
decoder := xml.NewDecoder(r)
d.Tag = Tag{}
d.Metas = [][]Meta{}
// ww temporary keeps all dict before add
// it to d.buildTree, tm associates tag names with Grammemes
ww := wordForms{}
tm := tagMap{}
for {
// exit loop if nothing to decode
t, _ := decoder.Token()
if t == nil {
break
}
switch se := t.(type) {
case xml.StartElement:
// place pointer to grammeme into d.Tag
// and temporary tagMap
if se.Name.Local == "grammeme" {
var g Grammeme
if err = decoder.DecodeElement(&g, &se); err != nil {
return err
}
d.Tag = append(d.Tag, g)
tm[g.Name] = len(d.Tag) - 1
}
// generate all wordForms from lemma
if se.Name.Local == "lemma" {
var l Lemma
if err = decoder.DecodeElement(&l, &se); err != nil {
return err
}
for _, f := range l.Forms {
f.GrammemeNames = append(l.Main.GrammemeNames, f.GrammemeNames...)
ww = append(ww, newWordForm(f, tm))
}
}
}
}
bt := mafsa.New()
sort.Sort(ww)
for _, w := range ww {
if !bt.Contains(w.word) {
if err = bt.Insert(w.word); err != nil {
return err
}
d.Metas = append(d.Metas, []Meta{w.meta})
} else {
d.Metas[len(d.Metas)-1] = append(d.Metas[len(d.Metas)-1], w.meta)
}
}
bt.Finish()
d.Tree, err = bt.MarshalBinary()
return err
}
// Save saves MorphData to file
func (d *MorphData) Save(fp string) error {
f, err := os.Create(fp)
if err != nil {
return err
}
defer f.Close()
return d.WriteMorphData(f)
}
// Load loads MorphData from file
func (d *MorphData) Load(fp string) error {
f, err := os.Open(fp)
if err != nil {
return err
}
defer f.Close()
return d.ReadMorphData(f)
}
// WriteMorphData creates, encodes and writes MorphData to a io.Writer
func (d *MorphData) WriteMorphData(w io.Writer) (err error) {
g := gob.NewEncoder(w)
return g.Encode(d)
}
// ReadMorphData reads, decodes and saves MorphData from a io.Reader
func (d *MorphData) ReadMorphData(r io.Reader) (err error) {
g := gob.NewDecoder(r)
return g.Decode(d)
}
// --- internal data structs ----
type tagMap map[string]int
type wordForm struct {
word string
meta Meta
}
func newWordForm(f Form, tm tagMap) wordForm {
wf := wordForm{
word: f.Value,
meta: Meta{},
}
for _, n := range f.GrammemeNames {
if grammemeIndex, ok := tm[n.Value]; ok {
wf.meta.GrammemeIndexes = append(wf.meta.GrammemeIndexes, int8(grammemeIndex))
}
}
return wf
}
type wordForms []wordForm
func (w wordForms) Len() int {
return len(w)
}
func (w wordForms) Less(i, j int) bool {
return w[i].word < w[j].word
}
func (w wordForms) Swap(i, j int) {
w[i], w[j] = w[j], w[i]
}