forked from FooSoft/yomichan-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmnedict.go
136 lines (121 loc) · 3.36 KB
/
jmnedict.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
package rikaitan
import (
"os"
"regexp"
jmdict "github.com/Ajatt-Tools/jmdict-go"
)
func jmnedictPublicationDate(dictionary jmdict.Jmnedict) string {
unknownDate := "unknown"
idx := len(dictionary.Entries) - 1
if len(dictionary.Entries) == 0 {
return unknownDate
} else if len(dictionary.Entries[idx].Translations) == 0 {
return unknownDate
} else if len(dictionary.Entries[idx].Translations[0].Translations) == 0 {
return unknownDate
}
dateGloss := dictionary.Entries[idx].Translations[0].Translations[0]
r := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
date := r.FindString(dateGloss)
if date != "" {
return date
} else {
return unknownDate
}
}
func jmnedictSenseTerm(headword headword, seq sequence, sense jmdict.JmnedictTranslation, senseNumber int) dbTerm {
term := dbTerm{
Expression: headword.Expression,
Reading: headword.Reading,
Sequence: seq,
}
for _, gloss := range sense.Translations {
term.Glossary = append(term.Glossary, gloss)
}
term.addDefinitionTags(sense.NameTypes...)
term.Score = calculateTermScore(senseNumber, 0, headword)
return term
}
func jmnedictTerms(headword headword, entry jmdict.JmnedictEntry, g genericTermInfo) []dbTerm {
terms := []dbTerm{}
for idx, sense := range entry.Translations {
if g.IsGenericName(headword, sense.Translations) {
g.AddGlosses(headword.Expression, sense.NameTypes, headword.Reading)
} else {
g.AddUsedSequence(entry.Sequence)
senseTerm := jmnedictSenseTerm(headword, entry.Sequence, sense, idx+1)
terms = append(terms, senseTerm)
}
}
return terms
}
func jmnedictHeadwords(entry jmdict.JmnedictEntry) (headwords []headword) {
// Note that JMnedict doesn't (currently) use priority tags,
// frequency tags, or any sort of reading/kanji restrictions.
for _, reading := range entry.Readings {
for _, kanji := range entry.Kanji {
h := headword{
Expression: kanji.Expression,
Reading: reading.Reading,
}
h.Index = len(headwords)
headwords = append(headwords, h)
}
}
if len(entry.Kanji) == 0 {
for _, reading := range entry.Readings {
h := headword{
Expression: reading.Reading,
Reading: reading.Reading,
}
h.Index = len(headwords)
headwords = append(headwords, h)
}
}
return headwords
}
func jmnedictExportDb(inputPath, outputPath, language, title string, stride int, pretty bool) error {
reader, err := os.Open(inputPath)
if err != nil {
return err
}
defer reader.Close()
dictionary, entities, err := jmdict.LoadJmnedictNoTransform(reader)
if err != nil {
return err
}
genericTermInfo := newGenericTermInfo()
terms := dbTermList{}
for _, entry := range dictionary.Entries {
headwords := jmnedictHeadwords(entry)
for _, headword := range headwords {
newTerms := jmnedictTerms(headword, entry, genericTermInfo)
terms = append(terms, newTerms...)
}
}
terms = append(terms, genericTermInfo.Terms()...)
tags := dbTagList{}
tags = append(tags, entityTags(entities)...)
recordData := map[string]dbRecordList{
"term": terms.crush(),
"tag": tags.crush(),
}
if title == "" {
title = "JMnedict"
}
jmnedictDate := jmnedictPublicationDate(dictionary)
title = title + " [" + jmnedictDate + "]"
index := dbIndex{
Title: title,
Revision: "JMnedict." + jmnedictDate,
Sequenced: true,
Attribution: edrdgAttribution,
}
return writeDb(
outputPath,
index,
recordData,
stride,
pretty,
)
}