generated from ElnuDev/go-project
Store list of entry keys to make consistent search result order
This commit is contained in:
parent
01e6ab46e7
commit
b20cdaf819
1 changed files with 24 additions and 4 deletions
28
dict/main.go
28
dict/main.go
|
@ -16,9 +16,15 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var words []string // since iterating over map isn't the same every time
|
||||
var dict map[string]Entry
|
||||
|
||||
func LoadDict() error {
|
||||
type binaryData struct {
|
||||
Words []string
|
||||
Dict map[string]Entry
|
||||
}
|
||||
|
||||
// Loading from binary
|
||||
const binaryFile = "dict.bin"
|
||||
file, err := os.Open(binaryFile)
|
||||
|
@ -29,7 +35,10 @@ func LoadDict() error {
|
|||
} else {
|
||||
defer file.Close()
|
||||
decoder := gob.NewDecoder(file)
|
||||
err = decoder.Decode(&dict)
|
||||
var data binaryData
|
||||
err = decoder.Decode(&data)
|
||||
words = data.Words
|
||||
dict = data.Dict
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -56,8 +65,9 @@ func LoadDict() error {
|
|||
furiganaData[params[0]] = params[2]
|
||||
}
|
||||
|
||||
words = make([]string, len(jmdict.Entries))
|
||||
dict = make(map[string]Entry)
|
||||
for _, jmdictEntry := range jmdict.Entries {
|
||||
for i, jmdictEntry := range jmdict.Entries {
|
||||
// お願い致します|おねがいいたします|1:ねが;3:いた
|
||||
var furiganaInfo *string
|
||||
if len(jmdictEntry.Kanji) > 0 {
|
||||
|
@ -67,6 +77,7 @@ func LoadDict() error {
|
|||
furiganaInfo = nil
|
||||
}
|
||||
entry := ParseEntry(&jmdictEntry, furiganaInfo)
|
||||
words[i] = entry.Kanji
|
||||
dict[entry.Kanji] = entry
|
||||
}
|
||||
|
||||
|
@ -77,7 +88,11 @@ func LoadDict() error {
|
|||
}
|
||||
defer file.Close()
|
||||
encoder := gob.NewEncoder(file)
|
||||
err = encoder.Encode(&dict)
|
||||
data := binaryData{
|
||||
Words: words,
|
||||
Dict: dict,
|
||||
}
|
||||
err = encoder.Encode(&data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -204,7 +219,7 @@ func Search(query string) queryResult {
|
|||
otherResults := make([]Entry, 0)
|
||||
truncated := false
|
||||
count := 0
|
||||
for kanji := range dict {
|
||||
for _, kanji := range words {
|
||||
exactMatch := false
|
||||
entry := dict[kanji]
|
||||
if kanji == query {
|
||||
|
@ -214,6 +229,11 @@ func Search(query string) queryResult {
|
|||
if strings.Contains(kanji, query) {
|
||||
goto match
|
||||
}
|
||||
for _, definition := range entry.Definitions {
|
||||
if strings.Contains(strings.ToLower(definition.Definition), strings.ToLower(strings.TrimSpace(query))) {
|
||||
goto match
|
||||
}
|
||||
}
|
||||
// TODO: Skip if query contains kanji
|
||||
if strings.Contains(entry.Reading, query) {
|
||||
goto match
|
||||
|
|
Reference in a new issue