diff --git a/common.go b/common.go index edf85d0..a2e6a94 100644 --- a/common.go +++ b/common.go @@ -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 { diff --git a/edict.go b/edict.go index 0fd3945..dabda16 100644 --- a/edict.go +++ b/edict.go @@ -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) } diff --git a/enamdict.go b/enamdict.go index 71401f2..0912452 100644 --- a/enamdict.go +++ b/enamdict.go @@ -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) } diff --git a/kanjidic.go b/kanjidic.go index 793f153..729fcc0 100644 --- a/kanjidic.go +++ b/kanjidic.go @@ -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