generated from ElnuDev/go-project
Handle multiple entries with same kanji
This commit is contained in:
parent
3965b59893
commit
395318b7f6
2 changed files with 29 additions and 9 deletions
36
dict/main.go
36
dict/main.go
|
@ -63,7 +63,8 @@ func LoadDict() error {
|
|||
furiganaData := make(map[string]string)
|
||||
for scanner.Scan() {
|
||||
params := strings.Split(scanner.Text(), "|")
|
||||
furiganaData[params[0]] = params[2]
|
||||
// We need to include the reading as well since some words have the same kanji
|
||||
furiganaData[fmt.Sprintf("%s|%s", params[0], params[1])] = params[2]
|
||||
}
|
||||
|
||||
words = make([]string, len(jmdict.Entries))
|
||||
|
@ -72,14 +73,32 @@ func LoadDict() error {
|
|||
// お願い致します|おねがいいたします|1:ねが;3:いた
|
||||
var furiganaInfo *string
|
||||
if len(jmdictEntry.Kanji) > 0 {
|
||||
data := furiganaData[jmdictEntry.Kanji[0].Expression]
|
||||
data := furiganaData[fmt.Sprintf("%s|%s", jmdictEntry.Kanji[0].Expression, jmdictEntry.Readings[0].Reading)]
|
||||
furiganaInfo = &data
|
||||
} else {
|
||||
furiganaInfo = nil
|
||||
}
|
||||
entry := ParseEntry(&jmdictEntry, furiganaInfo)
|
||||
words[i] = entry.Kanji
|
||||
dict[entry.Kanji] = entry
|
||||
offset := 0
|
||||
getKey := func() string {
|
||||
if offset == 0 {
|
||||
// unique
|
||||
return entry.Kanji
|
||||
} else {
|
||||
return fmt.Sprintf("%s-%d", entry.Kanji, offset)
|
||||
}
|
||||
}
|
||||
for {
|
||||
if _, ok := dict[getKey()]; ok {
|
||||
offset++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
key := getKey()
|
||||
entry.Key = key
|
||||
words[i] = key
|
||||
dict[key] = entry
|
||||
}
|
||||
|
||||
// Encoding to binary
|
||||
|
@ -107,6 +126,7 @@ type Furigana struct {
|
|||
}
|
||||
|
||||
type Entry struct {
|
||||
Key string
|
||||
Kanji string
|
||||
// Mapping of character index to furigana
|
||||
Furigana []Furigana
|
||||
|
@ -227,14 +247,14 @@ func Search(query string) queryResult {
|
|||
otherResults := make([]Entry, 0)
|
||||
truncated := false
|
||||
count := 0
|
||||
for _, kanji := range words {
|
||||
for _, key := range words {
|
||||
exactMatch := false
|
||||
entry := dict[kanji]
|
||||
if kanji == query {
|
||||
entry := dict[key]
|
||||
if entry.Kanji == query {
|
||||
exactMatch = true
|
||||
goto match
|
||||
}
|
||||
if strings.Contains(kanji, query) {
|
||||
if strings.Contains(entry.Kanji, query) {
|
||||
goto match
|
||||
}
|
||||
for _, definition := range entry.Definitions {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{{ define "entry" }}
|
||||
<div class="box">
|
||||
<h3 lang="ja">
|
||||
<a href="/word/{{ .Kanji }}">
|
||||
<a href="/word/{{ .Key }}">
|
||||
{{- range .Furigana -}}
|
||||
{{- if .Furigana -}}
|
||||
<ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Furigana -}}</rt><rp>)</rp></ruby>
|
||||
|
|
Reference in a new issue