From a6ee0cfae21abaf43b6dcfaca681739da1518b8b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 2 Nov 2016 22:31:31 -0700 Subject: [PATCH] Add stub for meta generator --- main.go | 1 + meta.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 meta.go diff --git a/main.go b/main.go index b88e299..b83ed48 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,7 @@ func usage() { func outputJson(fileFormat, inputPath, outputDir string, flags int) error { handlers := map[string]func(string, io.Reader, int) error{ + "meta": outputMetaJson, "edict": outputEdictJson, "enamdict": outputJmnedictJson, "kanjidic": outputKanjidicJson, diff --git a/meta.go b/meta.go new file mode 100644 index 0000000..456faec --- /dev/null +++ b/meta.go @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package main + +import ( + "fmt" + "io" + "os" + "path" + + "github.com/FooSoft/jmdict" +) + +func outputMetaIndex(outputDir string, entries []termSource, entities map[string]string, pretty bool) error { + if err := os.MkdirAll(outputDir, 0755); err != nil { + return err + } + + outputIndex, err := os.Create(path.Join(outputDir, "index.json")) + if err != nil { + return err + } + defer outputIndex.Close() + + dict := buildTermIndex(entries, entities) + indexBytes, err := marshalJson(dict, pretty) + if err != nil { + return err + } + + if _, err = outputIndex.Write(indexBytes); err != nil { + return err + } + + defCnt := len(dict.defs) + for i := 0; i < defCnt; i += BANK_STRIDE { + outputRef, err := os.Create(path.Join(outputDir, fmt.Sprintf("bank_%d.json", i/BANK_STRIDE+1))) + if err != nil { + return err + } + defer outputRef.Close() + + indexSrc := i + indexDst := i + BANK_STRIDE + if indexDst > defCnt { + indexDst = defCnt + } + + refBytes, err := marshalJson(dict.defs[indexSrc:indexDst], pretty) + if err != nil { + return err + } + + if _, err = outputRef.Write(refBytes); err != nil { + return err + } + } + + return nil +} + +func outputMetaJson(outputDir string, reader io.Reader, flags int) error { + dict, entities, err := jmdict.LoadJmdictNoTransform(reader) + if err != nil { + return err + } + + var entries []termSource + for _, e := range dict.Entries { + entries = append(entries, convertEdictEntry(e)...) + } + + return outputMetaIndex(outputDir, entries, entities, flags&flagPrettyJson == flagPrettyJson) +}