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
}
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

View File

@ -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,
)
}

View File

@ -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,
)
}

View File

@ -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,
)
}

22
main.go
View File

@ -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 {