generated from ElnuDev/go-project
Compare commits
2 commits
df8e0f8903
...
91fb5d9e3b
Author | SHA1 | Date | |
---|---|---|---|
91fb5d9e3b | |||
3d21311f12 |
2 changed files with 51 additions and 21 deletions
3
dict/.gitignore
vendored
3
dict/.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
JMdict.xml
|
JMdict.xml
|
||||||
|
dict.bin
|
69
dict/main.go
69
dict/main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/gob"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -13,18 +14,51 @@ import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dict jmdict.Jmdict
|
var dict map[string]Entry
|
||||||
|
|
||||||
func LoadDict() error {
|
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"
|
const jmdictFile = "JMdict.xml"
|
||||||
reader, err := os.Open(jmdictFile)
|
reader, err := os.Open(jmdictFile)
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +73,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 +109,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 +146,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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue