1

Work on DB integration

This commit is contained in:
Alex Yatskov 2016-08-21 19:51:43 -07:00
parent 98615708ca
commit fcee6e009f
4 changed files with 44 additions and 51 deletions

View File

@ -25,24 +25,22 @@ package main
import (
"encoding/json"
"io"
"strconv"
"strings"
)
type vocabJson struct {
Indices map[string]string `json:"i"`
Entities map[string]string `json:"e"`
Defs [][]string `json:"d"`
type termJson struct {
Entities [][]string `json:"e"`
Defs [][]interface{} `json:"d"`
}
type vocabSource struct {
type termSource struct {
Expression string
Reading string
Tags []string
Glossary []string
}
func (s *vocabSource) addTags(tags ...string) {
func (s *termSource) addTags(tags ...string) {
for _, tag := range tags {
if !hasString(tag, s.Tags) {
s.Tags = append(s.Tags, tag)
@ -50,7 +48,7 @@ func (s *vocabSource) addTags(tags ...string) {
}
}
func (s *vocabSource) addTagsPri(tags ...string) {
func (s *termSource) addTagsPri(tags ...string) {
for _, tag := range tags {
switch tag {
case "news1", "ichi1", "spec1", "gai1":
@ -63,28 +61,30 @@ func (s *vocabSource) addTagsPri(tags ...string) {
}
}
func buildVocabJson(entries []vocabSource, entities map[string]string) vocabJson {
dict := vocabJson{
Indices: make(map[string]string),
Entities: entities,
func buildTermJson(entries []termSource, entities map[string]string) termJson {
var dict termJson
for key, value := range entities {
ent := []string{key, value}
dict.Entities = append(dict.Entities, ent)
}
for i, e := range entries {
entry := []string{e.Expression, e.Reading, strings.Join(e.Tags, " ")}
entry = append(entry, e.Glossary...)
dict.Defs = append(dict.Defs, entry)
appendStrIndex(dict.Indices, e.Expression, i)
if len(e.Reading) > 0 {
appendStrIndex(dict.Indices, e.Reading, i)
for _, e := range entries {
def := []interface{}{
e.Expression,
e.Reading,
strings.Join(e.Tags, " "),
e.Glossary,
}
dict.Defs = append(dict.Defs, def)
}
return dict
}
func outputVocabJson(writer io.Writer, entries []vocabSource, entities map[string]string, pretty bool) error {
dict := buildVocabJson(entries, entities)
func outputTermJson(writer io.Writer, entries []termSource, entities map[string]string, pretty bool) error {
dict := buildTermJson(entries, entities)
var (
bytes []byte
@ -105,16 +105,6 @@ func outputVocabJson(writer io.Writer, entries []vocabSource, entities map[strin
return err
}
func appendStrIndex(indices map[string]string, key string, value int) {
def, _ := indices[key]
if len(def) > 0 {
def += " "
}
def += strconv.Itoa(value)
indices[key] = def
}
func hasString(needle string, haystack []string) bool {
for _, value := range haystack {
if needle == value {

View File

@ -28,15 +28,15 @@ import (
"github.com/FooSoft/jmdict"
)
func convertEdictEntry(edictEntry jmdict.JmdictEntry) []vocabSource {
var entries []vocabSource
func convertEdictEntry(edictEntry jmdict.JmdictEntry) []termSource {
var entries []termSource
convert := func(reading jmdict.JmdictReading, kanji *jmdict.JmdictKanji) {
if kanji != nil && hasString(kanji.Expression, reading.Restrictions) {
return
}
var entry vocabSource
var entry termSource
if kanji == nil {
entry.Expression = reading.Reading
} else {
@ -93,10 +93,10 @@ func outputEdictJson(writer io.Writer, reader io.Reader, flags int) error {
return err
}
var entries []vocabSource
var entries []termSource
for _, e := range dict.Entries {
entries = append(entries, convertEdictEntry(e)...)
}
return outputVocabJson(writer, entries, entities, flags&flagPrettyJson == flagPrettyJson)
return outputTermJson(writer, entries, entities, flags&flagPrettyJson == flagPrettyJson)
}

View File

@ -28,15 +28,15 @@ import (
"github.com/FooSoft/jmdict"
)
func convertJmnedictEntry(enamdictEntry jmdict.JmnedictEntry) []vocabSource {
var entries []vocabSource
func convertJmnedictEntry(enamdictEntry jmdict.JmnedictEntry) []termSource {
var entries []termSource
convert := func(reading jmdict.JmnedictReading, kanji *jmdict.JmnedictKanji) {
if kanji != nil && hasString(kanji.Expression, reading.Restrictions) {
return
}
var entry vocabSource
var entry termSource
if kanji == nil {
entry.Expression = reading.Reading
} else {
@ -79,10 +79,10 @@ func outputJmnedictJson(writer io.Writer, reader io.Reader, flags int) error {
return err
}
var entries []vocabSource
var entries []termSource
for _, e := range dict.Entries {
entries = append(entries, convertJmnedictEntry(e)...)
}
return outputVocabJson(writer, entries, entities, flags&flagPrettyJson == flagPrettyJson)
return outputTermJson(writer, entries, entities, flags&flagPrettyJson == flagPrettyJson)
}

View File

@ -33,13 +33,13 @@ import (
)
type kanjiJson struct {
Characters map[string][]string `json:"c"`
Defs [][]interface{} `json:"d"`
}
type kanjiSource struct {
Character string
Kunyomi []string
Onyomi []string
Kunyomi []string
Tags []string
Meanings []string
}
@ -53,15 +53,18 @@ func (s *kanjiSource) addTags(tags ...string) {
}
func buildKanjiJson(kanji []kanjiSource) kanjiJson {
dict := kanjiJson{make(map[string][]string)}
var dict kanjiJson
for _, k := range kanji {
var params []string
params = append(params, strings.Join(k.Onyomi, " "))
params = append(params, strings.Join(k.Kunyomi, " "))
params = append(params, strings.Join(k.Tags, " "))
params = append(params, k.Meanings...)
dict.Characters[k.Character] = params
def := []interface{}{
k.Character,
strings.Join(k.Onyomi, " "),
strings.Join(k.Kunyomi, " "),
strings.Join(k.Tags, " "),
k.Meanings,
}
dict.Defs = append(dict.Defs, def)
}
return dict