|
|
@ -13,7 +13,7 @@ import (
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var dict jmdict.Jmdict
|
|
|
|
var dict map[string]Entry
|
|
|
|
|
|
|
|
|
|
|
|
func LoadDict() error {
|
|
|
|
func LoadDict() error {
|
|
|
|
const jmdictFile = "JMdict.xml"
|
|
|
|
const jmdictFile = "JMdict.xml"
|
|
|
@ -21,10 +21,15 @@ func LoadDict() error {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dict, _, err = jmdict.LoadJmdict(reader)
|
|
|
|
jmdict, _, err := jmdict.LoadJmdict(reader)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dict = make(map[string]Entry)
|
|
|
|
|
|
|
|
for _, jmdictEntry := range jmdict.Entries {
|
|
|
|
|
|
|
|
entry := ParseEntry(&jmdictEntry)
|
|
|
|
|
|
|
|
dict[entry.Kanji] = entry
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -39,7 +44,7 @@ type Definition struct {
|
|
|
|
PartOfSpeech []string
|
|
|
|
PartOfSpeech []string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ParseEntry(entry jmdict.JmdictEntry) Entry {
|
|
|
|
func ParseEntry(entry *jmdict.JmdictEntry) Entry {
|
|
|
|
kanji := ""
|
|
|
|
kanji := ""
|
|
|
|
if len(entry.Kanji) > 0 {
|
|
|
|
if len(entry.Kanji) > 0 {
|
|
|
|
kanji = entry.Kanji[0].Expression
|
|
|
|
kanji = entry.Kanji[0].Expression
|
|
|
@ -75,26 +80,22 @@ func Search(query string) queryResult {
|
|
|
|
otherResults := make([]Entry, 0)
|
|
|
|
otherResults := make([]Entry, 0)
|
|
|
|
truncated := false
|
|
|
|
truncated := false
|
|
|
|
count := 0
|
|
|
|
count := 0
|
|
|
|
for _, jmdictEntry := range dict.Entries {
|
|
|
|
for kanji := range dict {
|
|
|
|
exactMatch := false
|
|
|
|
exactMatch := false
|
|
|
|
for _, kanji := range jmdictEntry.Kanji {
|
|
|
|
entry := dict[kanji]
|
|
|
|
if kanji.Expression == query {
|
|
|
|
if kanji == query {
|
|
|
|
exactMatch = true
|
|
|
|
exactMatch = true
|
|
|
|
goto match
|
|
|
|
goto match
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.Contains(kanji.Expression, query) {
|
|
|
|
if strings.Contains(kanji, query) {
|
|
|
|
goto match
|
|
|
|
goto match
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Skip if query contains kanji
|
|
|
|
// TODO: Skip if query contains kanji
|
|
|
|
for _, reading := range jmdictEntry.Readings {
|
|
|
|
if strings.Contains(entry.Reading, query) {
|
|
|
|
if strings.Contains(reading.Reading, query) {
|
|
|
|
goto match
|
|
|
|
goto match
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
match:
|
|
|
|
match:
|
|
|
|
entry := ParseEntry(jmdictEntry)
|
|
|
|
|
|
|
|
if exactMatch {
|
|
|
|
if exactMatch {
|
|
|
|
exactResults = append(exactResults, entry)
|
|
|
|
exactResults = append(exactResults, entry)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -116,9 +117,8 @@ func Search(query string) queryResult {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Lookup(word string) *Entry {
|
|
|
|
func Lookup(word string) *Entry {
|
|
|
|
for _, jmdictEntry := range dict.Entries {
|
|
|
|
for kanji, entry := range dict {
|
|
|
|
entry := ParseEntry(jmdictEntry)
|
|
|
|
if kanji == word {
|
|
|
|
if entry.Kanji == word {
|
|
|
|
|
|
|
|
return &entry
|
|
|
|
return &entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|