1

Add title field

This commit is contained in:
Alex Yatskov 2016-11-05 17:09:23 -07:00
parent b075a2bae5
commit 4919834421
5 changed files with 24 additions and 17 deletions

View File

@ -111,7 +111,7 @@ func (kanji dbKanjiList) crush() [][]string {
return results 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 DB_VERSION = 1
const BANK_STRIDE = 50000 const BANK_STRIDE = 50000
@ -124,6 +124,7 @@ func writeDb(outputDir string, records [][]string, entities map[string]string, p
} }
var db struct { var db struct {
Title string `json:"title"`
Version int `json:"version"` Version int `json:"version"`
Banks int `json:"banks"` Banks int `json:"banks"`
Entities map[string]string `json:"entities"` Entities map[string]string `json:"entities"`
@ -131,6 +132,7 @@ func writeDb(outputDir string, records [][]string, entities map[string]string, p
recordCount := len(records) recordCount := len(records)
db.Title = title
db.Version = 0 db.Version = 0
db.Entities = entities db.Entities = entities
db.Banks = recordCount / BANK_STRIDE db.Banks = recordCount / BANK_STRIDE

View File

@ -93,7 +93,7 @@ func extractJmdictTerms(edictEntry jmdict.JmdictEntry) []dbTerm {
return terms 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) dict, entities, err := jmdict.LoadJmdictNoTransform(reader)
if err != nil { if err != nil {
return err return err
@ -106,8 +106,9 @@ func exportJmdictDb(outputDir string, reader io.Reader, flags int) error {
return writeDb( return writeDb(
outputDir, outputDir,
title,
terms.crush(), terms.crush(),
entities, entities,
flags&flagPrettyJson == flagPrettyJson, flags&flagPretty == flagPretty,
) )
} }

View File

@ -77,7 +77,7 @@ func extractJmnedictTerms(enamdictEntry jmdict.JmnedictEntry) []dbTerm {
return terms 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) dict, entities, err := jmdict.LoadJmnedictNoTransform(reader)
if err != nil { if err != nil {
return err return err
@ -90,8 +90,9 @@ func exportJmnedictDb(outputDir string, reader io.Reader, flags int) error {
return writeDb( return writeDb(
outputDir, outputDir,
title,
terms.crush(), terms.crush(),
entities, entities,
flags&flagPrettyJson == flagPrettyJson, flags&flagPretty == flagPretty,
) )
} }

View File

@ -80,7 +80,7 @@ func extractKanjidicKanji(entry jmdict.KanjidicCharacter) dbKanji {
return kanji 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) dict, err := jmdict.LoadKanjidic(reader)
if err != nil { if err != nil {
return err return err
@ -93,8 +93,9 @@ func exportKanjidicDb(outputDir string, reader io.Reader, flags int) error {
return writeDb( return writeDb(
outputDir, outputDir,
title,
kanji.crush(), kanji.crush(),
nil, nil,
flags&flagPrettyJson == flagPrettyJson, flags&flagPretty == flagPretty,
) )
} }

22
main.go
View File

@ -33,7 +33,7 @@ import (
) )
const ( const (
flagPrettyJson = 1 << iota flagPretty = 1 << iota
) )
func usage() { func usage() {
@ -42,14 +42,14 @@ func usage() {
flag.PrintDefaults() flag.PrintDefaults()
} }
func exportDb(fileFormat, inputPath, outputDir string, flags int) error { func exportDb(inputPath, outputDir, format, title string, flags int) error {
handlers := map[string]func(string, io.Reader, int) error{ handlers := map[string]func(string, string, io.Reader, int) error{
"edict": exportJmdictDb, "edict": exportJmdictDb,
"enamdict": exportJmnedictDb, "enamdict": exportJmnedictDb,
"kanjidic": exportKanjidicDb, "kanjidic": exportKanjidicDb,
} }
handler, ok := handlers[fileFormat] handler, ok := handlers[format]
if !ok { if !ok {
return errors.New("unrecognized file format") return errors.New("unrecognized file format")
} }
@ -60,22 +60,24 @@ func exportDb(fileFormat, inputPath, outputDir string, flags int) error {
} }
defer input.Close() defer input.Close()
return handler(outputDir, input, flags) return handler(outputDir, title, input, flags)
} }
func main() { 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.Usage = usage
flag.Parse() flag.Parse()
var flags int var flags int
if *prettyJson { if *pretty {
flags |= flagPrettyJson flags |= flagPretty
} }
if flag.NArg() == 3 { if flag.NArg() == 2 && len(*format) > 0 && len(*title) > 0 {
if err := exportDb(flag.Arg(0), flag.Arg(1), flag.Arg(2), flags); err != nil { if err := exportDb(flag.Arg(0), flag.Arg(1), *format, *title, flags); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {