forked from FooSoft/yomichan-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmdict_glossary.go
279 lines (247 loc) Β· 8.68 KB
/
jmdict_glossary.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package rikaitan
import (
"fmt"
"strconv"
jmdict "github.com/Ajatt-Tools/jmdict-go"
)
func glossaryContainsLanguage(glossary []jmdict.JmdictGlossary, language string) bool {
hasGlosses := false
for _, gloss := range glossary {
if glossContainsLanguage(gloss, language) {
hasGlosses = true
break
}
}
return hasGlosses
}
func glossContainsLanguage(gloss jmdict.JmdictGlossary, language string) bool {
if gloss.Language == nil && language != "eng" {
return false
} else if gloss.Language != nil && language != *gloss.Language {
return false
} else {
return true
}
}
func makeGlossListItem(gloss jmdict.JmdictGlossary, language string) any {
contents := []any{gloss.Content}
listItem := contentListItem(contentAttr{}, contents...)
return listItem
}
func makeInfoGlossListItem(gloss jmdict.JmdictGlossary, language string) any {
// Prepend gloss with "type" (literal, figurative, trademark, etc.)
glossTypeCode := *gloss.Type
contents := []any{}
if name, ok := glossTypeCodeToName[LangCode{language, glossTypeCode}]; ok {
if name != "" {
italicStyle := contentAttr{fontStyle: "italic"}
contents = append(contents, contentSpan(italicStyle, "("+name+")"), " ")
}
} else {
fmt.Println("Unknown glossary type code " + *gloss.Type + " for build language " + language)
contents = append(contents, "["+glossTypeCode+"] ")
}
contents = append(contents, gloss.Content)
listItem := contentListItem(contentAttr{}, contents...)
return listItem
}
func makeSourceLangListItem(sourceLanguage jmdict.JmdictSource, language string) any {
contents := []any{}
var srcLangCode string
if sourceLanguage.Language == nil {
srcLangCode = "eng"
} else {
srcLangCode = *sourceLanguage.Language
}
// Format: [Language] ([Partial?], [Wasei?]): [Original word?]
// [Language]
if langName, ok := langCodeToName[LangCode{language, srcLangCode}]; ok {
contents = append(contents, langName)
} else {
contents = append(contents, srcLangCode)
fmt.Println("Unable to convert ISO 639 code " + srcLangCode + " to its full name in language " + language)
}
// ([Partial?], [Wasei?])
var sourceLangTypeCode string
if sourceLanguage.Type == nil {
sourceLangTypeCode = ""
} else {
sourceLangTypeCode = *sourceLanguage.Type
}
var sourceLangType string
if val, ok := sourceLangTypeCodeToType[LangCode{language, sourceLangTypeCode}]; ok {
sourceLangType = val
} else {
sourceLangType = sourceLangTypeCode
fmt.Println("Unknown source language type code " + sourceLangTypeCode + " for build language " + language)
}
if sourceLangType != "" && sourceLanguage.Wasei == "y" {
contents = append(contents, " ("+sourceLangType+", wasei)")
} else if sourceLangType != "" {
contents = append(contents, " ("+sourceLangType+")")
} else if sourceLanguage.Wasei == "y" {
contents = append(contents, " (wasei)")
}
// : [Original word?]
if sourceLanguage.Content != "" {
contents = append(contents, ": ")
attr := contentAttr{lang: ISOtoHTML[srcLangCode]}
contents = append(contents, contentSpan(attr, sourceLanguage.Content))
}
listItem := contentListItem(contentAttr{}, contents...)
return listItem
}
func makeReferenceListItem(reference string, refType string, meta jmdictMetadata) any {
contents := []any{}
attr := contentAttr{}
hint := refNoteHint[LangCode{meta.language, refType}]
contents = append(contents, hint+": ")
refHeadword, senseNumber, ok := parseReference(reference)
if !ok {
contents = append(contents, "γ"+reference+"γ")
return contentListItem(attr, contents...)
}
sequence, ok := meta.referenceToSeq[reference]
if !ok {
contents = append(contents, "γ"+reference+"γ")
return contentListItem(attr, contents...)
}
targetSense := senseID{
sequence: sequence,
number: senseNumber,
}
expHash := refHeadword.ExpHash()
doDisplayReading := (len(meta.expHashToReadings[expHash]) > 1)
doDisplaySenseNumber := (meta.seqToSenseCount[targetSense.sequence] > 1)
refGlossAttr := contentAttr{
fontSize: "65%",
verticalAlign: "middle",
data: map[string]string{"content": "refGlosses"},
}
contents = append(contents, refHeadword.ToInternalLink(doDisplayReading))
if doDisplaySenseNumber {
contents = append(contents, contentSpan(refGlossAttr, " "+strconv.Itoa(targetSense.number)+". "+meta.condensedGlosses[targetSense]))
} else {
contents = append(contents, contentSpan(refGlossAttr, " "+meta.condensedGlosses[targetSense]))
}
listItem := contentListItem(attr, contents...)
return listItem
}
func makeExampleListItem(sentence jmdict.JmdictExampleSentence) any {
if sentence.Lang == "jpn" {
return contentListItem(contentAttr{}, sentence.Text)
} else {
attr := contentAttr{
lang: ISOtoHTML[sentence.Lang],
listStyleType: ISOtoFlag[sentence.Lang],
}
return contentListItem(attr, sentence.Text)
}
}
func listAttr(lang string, listStyleType string, dataContent string) contentAttr {
return contentAttr{
lang: lang,
listStyleType: listStyleType,
data: map[string]string{"content": dataContent},
}
}
func createGlossaryContent(sense jmdict.JmdictSense, meta jmdictMetadata) any {
glossaryContents := []any{}
// Add normal glosses
glossListItems := []any{}
for _, gloss := range sense.Glossary {
if glossContainsLanguage(gloss, meta.language) && gloss.Type == nil {
listItem := makeGlossListItem(gloss, meta.language)
glossListItems = append(glossListItems, listItem)
}
}
if len(glossListItems) > 0 {
attr := listAttr(ISOtoHTML[meta.language], "circle", "glossary")
list := contentUnorderedList(attr, glossListItems...)
glossaryContents = append(glossaryContents, list)
}
// Add information glosses
infoGlossListItems := []any{}
for _, gloss := range sense.Glossary {
if glossContainsLanguage(gloss, meta.language) && gloss.Type != nil {
listItem := makeInfoGlossListItem(gloss, meta.language)
infoGlossListItems = append(infoGlossListItems, listItem)
}
}
if len(infoGlossListItems) > 0 {
attr := listAttr(ISOtoHTML[meta.language], infoMarker, "infoGlossary")
list := contentUnorderedList(attr, infoGlossListItems...)
glossaryContents = append(glossaryContents, list)
}
// Add language-of-origin / loanword information
sourceLangListItems := []any{}
for _, sourceLanguage := range sense.SourceLanguages {
listItem := makeSourceLangListItem(sourceLanguage, meta.language)
sourceLangListItems = append(sourceLangListItems, listItem)
}
if len(sourceLangListItems) > 0 {
attr := listAttr(ISOtoHTML[meta.language], langMarker, "sourceLanguages")
list := contentUnorderedList(attr, sourceLangListItems...)
glossaryContents = append(glossaryContents, list)
}
// Add sense notes
noteListItems := []any{}
for _, information := range sense.Information {
listItem := contentListItem(contentAttr{}, information)
noteListItems = append(noteListItems, listItem)
}
if len(noteListItems) > 0 {
attr := listAttr(ISOtoHTML["jpn"], noteMarker, "notes") // notes often contain japanese text
list := contentUnorderedList(attr, noteListItems...)
glossaryContents = append(glossaryContents, list)
}
// Add antonyms
antonymListItems := []any{}
for _, antonym := range sense.Antonyms {
listItem := makeReferenceListItem(antonym, "ant", meta)
antonymListItems = append(antonymListItems, listItem)
}
if len(antonymListItems) > 0 {
attr := listAttr(ISOtoHTML[meta.language], antonymMarker, "antonyms")
list := contentUnorderedList(attr, antonymListItems...)
glossaryContents = append(glossaryContents, list)
}
// Add cross-references
referenceListItems := []any{}
for _, reference := range sense.References {
listItem := makeReferenceListItem(reference, "xref", meta)
referenceListItems = append(referenceListItems, listItem)
}
if len(referenceListItems) > 0 {
attr := listAttr(ISOtoHTML[meta.language], refMarker, "references")
list := contentUnorderedList(attr, referenceListItems...)
glossaryContents = append(glossaryContents, list)
}
// Add example sentences
exampleListItems := []any{}
for _, example := range sense.Examples {
for _, sentence := range example.Sentences {
listItem := makeExampleListItem(sentence)
exampleListItems = append(exampleListItems, listItem)
}
}
if len(exampleListItems) > 0 {
attr := listAttr(ISOtoHTML["jpn"], ISOtoFlag["jpn"], "examples")
list := contentUnorderedList(attr, exampleListItems...)
glossaryContents = append(glossaryContents, list)
}
return contentStructure(glossaryContents...)
}
func createGlossary(sense jmdict.JmdictSense, meta jmdictMetadata) []any {
glossary := []any{}
if meta.extraMode {
glossary = append(glossary, createGlossaryContent(sense, meta))
} else {
for _, gloss := range sense.Glossary {
if glossContainsLanguage(gloss, meta.language) {
glossary = append(glossary, gloss.Content)
}
}
}
return glossary
}