generated from ElnuDev/go-project
Compare commits
No commits in common. "91fb5d9e3b5e49d2547a151290a3438362fcee57" and "df8e0f8903059952943bce2f4a5f977b6123a41c" have entirely different histories.
91fb5d9e3b
...
df8e0f8903
2 changed files with 21 additions and 51 deletions
3
dict/.gitignore
vendored
3
dict/.gitignore
vendored
|
@ -1,2 +1 @@
|
||||||
JMdict.xml
|
JMdict.xml
|
||||||
dict.bin
|
|
69
dict/main.go
69
dict/main.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -14,51 +13,18 @@ import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dict map[string]Entry
|
var dict jmdict.Jmdict
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
jmdict, _, err := jmdict.LoadJmdict(reader)
|
dict, _, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +39,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
|
||||||
|
@ -109,22 +75,26 @@ func Search(query string) queryResult {
|
||||||
otherResults := make([]Entry, 0)
|
otherResults := make([]Entry, 0)
|
||||||
truncated := false
|
truncated := false
|
||||||
count := 0
|
count := 0
|
||||||
for kanji := range dict {
|
for _, jmdictEntry := range dict.Entries {
|
||||||
exactMatch := false
|
exactMatch := false
|
||||||
entry := dict[kanji]
|
for _, kanji := range jmdictEntry.Kanji {
|
||||||
if kanji == query {
|
if kanji.Expression == query {
|
||||||
exactMatch = true
|
exactMatch = true
|
||||||
goto match
|
goto match
|
||||||
}
|
}
|
||||||
if strings.Contains(kanji, query) {
|
if strings.Contains(kanji.Expression, query) {
|
||||||
goto match
|
goto match
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// TODO: Skip if query contains kanji
|
// TODO: Skip if query contains kanji
|
||||||
if strings.Contains(entry.Reading, query) {
|
for _, reading := range jmdictEntry.Readings {
|
||||||
goto match
|
if strings.Contains(reading.Reading, query) {
|
||||||
|
goto match
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
match:
|
match:
|
||||||
|
entry := ParseEntry(jmdictEntry)
|
||||||
if exactMatch {
|
if exactMatch {
|
||||||
exactResults = append(exactResults, entry)
|
exactResults = append(exactResults, entry)
|
||||||
} else {
|
} else {
|
||||||
|
@ -146,8 +116,9 @@ func Search(query string) queryResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Lookup(word string) *Entry {
|
func Lookup(word string) *Entry {
|
||||||
for kanji, entry := range dict {
|
for _, jmdictEntry := range dict.Entries {
|
||||||
if kanji == word {
|
entry := ParseEntry(jmdictEntry)
|
||||||
|
if entry.Kanji == word {
|
||||||
return &entry
|
return &entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue