From 4919834421742ce36bcc2fa542e3b0071d856ee5 Mon Sep 17 00:00:00 2001 From: Alex Yatskov <alex@foosoft.net> Date: Sat, 5 Nov 2016 17:09:23 -0700 Subject: [PATCH] Add title field --- common.go | 4 +++- edict.go | 5 +++-- enamdict.go | 5 +++-- kanjidic.go | 5 +++-- main.go | 22 ++++++++++++---------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/common.go b/common.go index ac4f886..2b3094d 100644 --- a/common.go +++ b/common.go @@ -111,7 +111,7 @@ func (kanji dbKanjiList) crush() [][]string { return results } -func writeDb(outputDir string, records [][]string, entities map[string]string, pretty bool) error { +func writeDb(outputDir, title string, records [][]string, entities map[string]string, pretty bool) error { const DB_VERSION = 1 const BANK_STRIDE = 50000 @@ -124,6 +124,7 @@ func writeDb(outputDir string, records [][]string, entities map[string]string, p } var db struct { + Title string `json:"title"` Version int `json:"version"` Banks int `json:"banks"` Entities map[string]string `json:"entities"` @@ -131,6 +132,7 @@ func writeDb(outputDir string, records [][]string, entities map[string]string, p recordCount := len(records) + db.Title = title db.Version = 0 db.Entities = entities db.Banks = recordCount / BANK_STRIDE diff --git a/edict.go b/edict.go index ddf5bb7..6d8e7c9 100644 --- a/edict.go +++ b/edict.go @@ -93,7 +93,7 @@ func extractJmdictTerms(edictEntry jmdict.JmdictEntry) []dbTerm { return terms } -func exportJmdictDb(outputDir string, reader io.Reader, flags int) error { +func exportJmdictDb(outputDir, title string, reader io.Reader, flags int) error { dict, entities, err := jmdict.LoadJmdictNoTransform(reader) if err != nil { return err @@ -106,8 +106,9 @@ func exportJmdictDb(outputDir string, reader io.Reader, flags int) error { return writeDb( outputDir, + title, terms.crush(), entities, - flags&flagPrettyJson == flagPrettyJson, + flags&flagPretty == flagPretty, ) } diff --git a/enamdict.go b/enamdict.go index d7a3e69..9cadcc3 100644 --- a/enamdict.go +++ b/enamdict.go @@ -77,7 +77,7 @@ func extractJmnedictTerms(enamdictEntry jmdict.JmnedictEntry) []dbTerm { return terms } -func exportJmnedictDb(outputDir string, reader io.Reader, flags int) error { +func exportJmnedictDb(outputDir, title string, reader io.Reader, flags int) error { dict, entities, err := jmdict.LoadJmnedictNoTransform(reader) if err != nil { return err @@ -90,8 +90,9 @@ func exportJmnedictDb(outputDir string, reader io.Reader, flags int) error { return writeDb( outputDir, + title, terms.crush(), entities, - flags&flagPrettyJson == flagPrettyJson, + flags&flagPretty == flagPretty, ) } diff --git a/kanjidic.go b/kanjidic.go index c75491c..359bc17 100644 --- a/kanjidic.go +++ b/kanjidic.go @@ -80,7 +80,7 @@ func extractKanjidicKanji(entry jmdict.KanjidicCharacter) dbKanji { return kanji } -func exportKanjidicDb(outputDir string, reader io.Reader, flags int) error { +func exportKanjidicDb(outputDir, title string, reader io.Reader, flags int) error { dict, err := jmdict.LoadKanjidic(reader) if err != nil { return err @@ -93,8 +93,9 @@ func exportKanjidicDb(outputDir string, reader io.Reader, flags int) error { return writeDb( outputDir, + title, kanji.crush(), nil, - flags&flagPrettyJson == flagPrettyJson, + flags&flagPretty == flagPretty, ) } diff --git a/main.go b/main.go index 98c0e8c..9657d23 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ import ( ) const ( - flagPrettyJson = 1 << iota + flagPretty = 1 << iota ) func usage() { @@ -42,14 +42,14 @@ func usage() { flag.PrintDefaults() } -func exportDb(fileFormat, inputPath, outputDir string, flags int) error { - handlers := map[string]func(string, io.Reader, int) error{ +func exportDb(inputPath, outputDir, format, title string, flags int) error { + handlers := map[string]func(string, string, io.Reader, int) error{ "edict": exportJmdictDb, "enamdict": exportJmnedictDb, "kanjidic": exportKanjidicDb, } - handler, ok := handlers[fileFormat] + handler, ok := handlers[format] if !ok { return errors.New("unrecognized file format") } @@ -60,22 +60,24 @@ func exportDb(fileFormat, inputPath, outputDir string, flags int) error { } defer input.Close() - return handler(outputDir, input, flags) + return handler(outputDir, title, input, flags) } func main() { - prettyJson := flag.Bool("pretty", false, "output prettified json") + pretty := flag.Bool("pretty", false, "output prettified json") + format := flag.String("format", "", "dictionary format") + title := flag.String("title", "", "dictionary title") flag.Usage = usage flag.Parse() var flags int - if *prettyJson { - flags |= flagPrettyJson + if *pretty { + flags |= flagPretty } - if flag.NArg() == 3 { - if err := exportDb(flag.Arg(0), flag.Arg(1), flag.Arg(2), flags); err != nil { + if flag.NArg() == 2 && len(*format) > 0 && len(*title) > 0 { + if err := exportDb(flag.Arg(0), flag.Arg(1), *format, *title, flags); err != nil { log.Fatal(err) } } else {