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

View File

@ -28,15 +28,15 @@ import (
"github.com/FooSoft/jmdict" "github.com/FooSoft/jmdict"
) )
func convertEdictEntry(edictEntry jmdict.JmdictEntry) []vocabSource { func convertEdictEntry(edictEntry jmdict.JmdictEntry) []termSource {
var entries []vocabSource var entries []termSource
convert := func(reading jmdict.JmdictReading, kanji *jmdict.JmdictKanji) { convert := func(reading jmdict.JmdictReading, kanji *jmdict.JmdictKanji) {
if kanji != nil && hasString(kanji.Expression, reading.Restrictions) { if kanji != nil && hasString(kanji.Expression, reading.Restrictions) {
return return
} }
var entry vocabSource var entry termSource
if kanji == nil { if kanji == nil {
entry.Expression = reading.Reading entry.Expression = reading.Reading
} else { } else {
@ -93,10 +93,10 @@ func outputEdictJson(writer io.Writer, reader io.Reader, flags int) error {
return err return err
} }
var entries []vocabSource var entries []termSource
for _, e := range dict.Entries { for _, e := range dict.Entries {
entries = append(entries, convertEdictEntry(e)...) 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" "github.com/FooSoft/jmdict"
) )
func convertJmnedictEntry(enamdictEntry jmdict.JmnedictEntry) []vocabSource { func convertJmnedictEntry(enamdictEntry jmdict.JmnedictEntry) []termSource {
var entries []vocabSource var entries []termSource
convert := func(reading jmdict.JmnedictReading, kanji *jmdict.JmnedictKanji) { convert := func(reading jmdict.JmnedictReading, kanji *jmdict.JmnedictKanji) {
if kanji != nil && hasString(kanji.Expression, reading.Restrictions) { if kanji != nil && hasString(kanji.Expression, reading.Restrictions) {
return return
} }
var entry vocabSource var entry termSource
if kanji == nil { if kanji == nil {
entry.Expression = reading.Reading entry.Expression = reading.Reading
} else { } else {
@ -79,10 +79,10 @@ func outputJmnedictJson(writer io.Writer, reader io.Reader, flags int) error {
return err return err
} }
var entries []vocabSource var entries []termSource
for _, e := range dict.Entries { for _, e := range dict.Entries {
entries = append(entries, convertJmnedictEntry(e)...) 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 { type kanjiJson struct {
Characters map[string][]string `json:"c"` Defs [][]interface{} `json:"d"`
} }
type kanjiSource struct { type kanjiSource struct {
Character string Character string
Kunyomi []string
Onyomi []string Onyomi []string
Kunyomi []string
Tags []string Tags []string
Meanings []string Meanings []string
} }
@ -53,15 +53,18 @@ func (s *kanjiSource) addTags(tags ...string) {
} }
func buildKanjiJson(kanji []kanjiSource) kanjiJson { func buildKanjiJson(kanji []kanjiSource) kanjiJson {
dict := kanjiJson{make(map[string][]string)} var dict kanjiJson
for _, k := range kanji { for _, k := range kanji {
var params []string def := []interface{}{
params = append(params, strings.Join(k.Onyomi, " ")) k.Character,
params = append(params, strings.Join(k.Kunyomi, " ")) strings.Join(k.Onyomi, " "),
params = append(params, strings.Join(k.Tags, " ")) strings.Join(k.Kunyomi, " "),
params = append(params, k.Meanings...) strings.Join(k.Tags, " "),
dict.Characters[k.Character] = params k.Meanings,
}
dict.Defs = append(dict.Defs, def)
} }
return dict return dict