Compare commits

...

2 commits

2 changed files with 51 additions and 21 deletions

1
dict/.gitignore vendored
View file

@ -1 +1,2 @@
JMdict.xml
dict.bin

View file

@ -1,6 +1,7 @@
package main
import (
"encoding/gob"
"encoding/json"
"fmt"
"log"
@ -13,18 +14,51 @@ import (
"github.com/gorilla/mux"
)
var dict jmdict.Jmdict
var dict map[string]Entry
func LoadDict() error {
// Loading from binary
const binaryFile = "dict.bin"
file, err := os.Open(binaryFile)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
defer file.Close()
decoder := gob.NewDecoder(file)
err = decoder.Decode(&dict)
return err
}
// Loading from JMdict
const jmdictFile = "JMdict.xml"
reader, err := os.Open(jmdictFile)
if err != nil {
return err
}
dict, _, err = jmdict.LoadJmdict(reader)
jmdict, _, err := jmdict.LoadJmdict(reader)
if err != nil {
return err
}
dict = make(map[string]Entry)
for _, jmdictEntry := range jmdict.Entries {
entry := ParseEntry(&jmdictEntry)
dict[entry.Kanji] = entry
}
// Encoding to binary
file, err = os.Create(binaryFile)
if err != nil {
return err
}
defer file.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(&dict)
if err != nil {
return err
}
return nil
}
@ -39,7 +73,7 @@ type Definition struct {
PartOfSpeech []string
}
func ParseEntry(entry jmdict.JmdictEntry) Entry {
func ParseEntry(entry *jmdict.JmdictEntry) Entry {
kanji := ""
if len(entry.Kanji) > 0 {
kanji = entry.Kanji[0].Expression
@ -75,26 +109,22 @@ func Search(query string) queryResult {
otherResults := make([]Entry, 0)
truncated := false
count := 0
for _, jmdictEntry := range dict.Entries {
for kanji := range dict {
exactMatch := false
for _, kanji := range jmdictEntry.Kanji {
if kanji.Expression == query {
entry := dict[kanji]
if kanji == query {
exactMatch = true
goto match
}
if strings.Contains(kanji.Expression, query) {
if strings.Contains(kanji, query) {
goto match
}
}
// TODO: Skip if query contains kanji
for _, reading := range jmdictEntry.Readings {
if strings.Contains(reading.Reading, query) {
if strings.Contains(entry.Reading, query) {
goto match
}
}
continue
match:
entry := ParseEntry(jmdictEntry)
if exactMatch {
exactResults = append(exactResults, entry)
} else {
@ -116,9 +146,8 @@ func Search(query string) queryResult {
}
func Lookup(word string) *Entry {
for _, jmdictEntry := range dict.Entries {
entry := ParseEntry(jmdictEntry)
if entry.Kanji == word {
for kanji, entry := range dict {
if kanji == word {
return &entry
}
}