forked from FooSoft/yomichan-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmnedict_generic_terms.go
80 lines (71 loc) · 1.86 KB
/
jmnedict_generic_terms.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
package rikaitan
import (
"golang.org/x/exp/slices"
)
type genericTermMap map[string]map[string][]string
type genericTermInfo struct {
expressionToTagToGlosses genericTermMap
usedSequences map[sequence]bool
currentSequence sequence
}
func newGenericTermInfo() genericTermInfo {
return genericTermInfo{
expressionToTagToGlosses: genericTermMap{},
usedSequences: map[sequence]bool{},
}
}
func (i *genericTermInfo) NewSequence() sequence {
seq := i.currentSequence + 1
for i.usedSequences[seq] {
seq += 1
}
i.AddUsedSequence(seq)
i.currentSequence = seq
return seq
}
func (i *genericTermInfo) AddUsedSequence(s sequence) {
i.usedSequences[s] = true
}
func (i *genericTermInfo) AddGlosses(exp string, tags []string, gloss string) {
if i.expressionToTagToGlosses[exp] == nil {
i.expressionToTagToGlosses[exp] = map[string][]string{}
}
for _, tag := range tags {
glosses := i.expressionToTagToGlosses[exp][tag]
if !slices.Contains(glosses, gloss) {
glosses = append(glosses, gloss)
i.expressionToTagToGlosses[exp][tag] = glosses
}
}
}
func (i *genericTermInfo) IsGenericName(headword headword, definitions []string) bool {
if headword.IsKanaOnly() {
// No reason to process these terms.
return false
}
isGenericName := true
for _, definition := range definitions {
if !isTransliteration(definition, headword.Reading) {
isGenericName = false
break
}
}
return isGenericName
}
func (i *genericTermInfo) Terms() (terms []dbTerm) {
for expression, tagToGlosses := range i.expressionToTagToGlosses {
seq := i.NewSequence()
for tag, glosses := range tagToGlosses {
term := dbTerm{
Expression: expression,
Sequence: seq,
}
for _, gloss := range glosses {
term.Glossary = append(term.Glossary, gloss)
}
term.addDefinitionTags(tag)
terms = append(terms, term)
}
}
return terms
}