Combined dictionary export
This commit is contained in:
parent
4919834421
commit
35d230b565
87
common.go
87
common.go
@ -111,7 +111,7 @@ func (kanji dbKanjiList) crush() [][]string {
|
|||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeDb(outputDir, title string, records [][]string, entities map[string]string, pretty bool) error {
|
func writeDb(outputDir, title string, termRecords [][]string, kanjiRecords [][]string, entities map[string]string, pretty bool) error {
|
||||||
const DB_VERSION = 1
|
const DB_VERSION = 1
|
||||||
const BANK_STRIDE = 50000
|
const BANK_STRIDE = 50000
|
||||||
|
|
||||||
@ -123,27 +123,63 @@ func writeDb(outputDir, title string, records [][]string, entities map[string]st
|
|||||||
return json.Marshal(obj)
|
return json.Marshal(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
var db struct {
|
writeDbRecords := func(prefix string, records [][]string) (int, error) {
|
||||||
Title string `json:"title"`
|
recordCount := len(records)
|
||||||
Version int `json:"version"`
|
bankCount := 0
|
||||||
Banks int `json:"banks"`
|
|
||||||
Entities map[string]string `json:"entities"`
|
for i := 0; i < recordCount; i += BANK_STRIDE {
|
||||||
|
indexSrc := i
|
||||||
|
indexDst := i + BANK_STRIDE
|
||||||
|
if indexDst > recordCount {
|
||||||
|
indexDst = recordCount
|
||||||
}
|
}
|
||||||
|
|
||||||
recordCount := len(records)
|
bytes, err := marshalJson(records[indexSrc:indexDst], pretty)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
db.Title = title
|
fp, err := os.Create(path.Join(outputDir, fmt.Sprintf("%s_bank_%d.json", prefix, i/BANK_STRIDE+1)))
|
||||||
db.Version = 0
|
if err != nil {
|
||||||
db.Entities = entities
|
return 0, err
|
||||||
db.Banks = recordCount / BANK_STRIDE
|
}
|
||||||
if recordCount%BANK_STRIDE > 0 {
|
defer fp.Close()
|
||||||
db.Banks += 1
|
|
||||||
|
if _, err = fp.Write(bytes); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bankCount += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return bankCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var db struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Version int `json:"version"`
|
||||||
|
Entities map[string]string `json:"entities"`
|
||||||
|
TermBanks int `json:"termBanks"`
|
||||||
|
KanjiBanks int `json:"kanjiBanks"`
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Title = title
|
||||||
|
db.Version = DB_VERSION
|
||||||
|
db.Entities = entities
|
||||||
|
|
||||||
|
if db.TermBanks, err = writeDbRecords("term", termRecords); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.KanjiBanks, err = writeDbRecords("kanji", kanjiRecords); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
bytes, err := marshalJson(db, pretty)
|
bytes, err := marshalJson(db, pretty)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -155,33 +191,10 @@ func writeDb(outputDir, title string, records [][]string, entities map[string]st
|
|||||||
}
|
}
|
||||||
defer fp.Close()
|
defer fp.Close()
|
||||||
|
|
||||||
if _, err = fp.Write(bytes); err != nil {
|
if _, err := fp.Write(bytes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < recordCount; i += BANK_STRIDE {
|
|
||||||
indexSrc := i
|
|
||||||
indexDst := i + BANK_STRIDE
|
|
||||||
if indexDst > recordCount {
|
|
||||||
indexDst = recordCount
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes, err := marshalJson(records[indexSrc:indexDst], pretty)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fp, err := os.Create(path.Join(outputDir, fmt.Sprintf("bank_%d.json", i/BANK_STRIDE+1)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
if _, err = fp.Write(bytes); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
edict.go
1
edict.go
@ -108,6 +108,7 @@ func exportJmdictDb(outputDir, title string, reader io.Reader, flags int) error
|
|||||||
outputDir,
|
outputDir,
|
||||||
title,
|
title,
|
||||||
terms.crush(),
|
terms.crush(),
|
||||||
|
nil,
|
||||||
entities,
|
entities,
|
||||||
flags&flagPretty == flagPretty,
|
flags&flagPretty == flagPretty,
|
||||||
)
|
)
|
||||||
|
@ -92,6 +92,7 @@ func exportJmnedictDb(outputDir, title string, reader io.Reader, flags int) erro
|
|||||||
outputDir,
|
outputDir,
|
||||||
title,
|
title,
|
||||||
terms.crush(),
|
terms.crush(),
|
||||||
|
nil,
|
||||||
entities,
|
entities,
|
||||||
flags&flagPretty == flagPretty,
|
flags&flagPretty == flagPretty,
|
||||||
)
|
)
|
||||||
|
@ -94,6 +94,7 @@ func exportKanjidicDb(outputDir, title string, reader io.Reader, flags int) erro
|
|||||||
return writeDb(
|
return writeDb(
|
||||||
outputDir,
|
outputDir,
|
||||||
title,
|
title,
|
||||||
|
nil,
|
||||||
kanji.crush(),
|
kanji.crush(),
|
||||||
nil,
|
nil,
|
||||||
flags&flagPretty == flagPretty,
|
flags&flagPretty == flagPretty,
|
||||||
|
Loading…
Reference in New Issue
Block a user