1

Add struct for handling index.json data

This commit is contained in:
stephenmk 2023-01-22 14:27:02 -06:00
parent 853d0b33dc
commit 56f9895967
No known key found for this signature in database
GPG Key ID: B6DA730DB06235F1
7 changed files with 86 additions and 43 deletions

View File

@ -19,8 +19,6 @@ const (
DefaultTitle = "" DefaultTitle = ""
) )
const databaseFormat = 3
type dbRecord []any type dbRecord []any
type dbRecordList []dbRecord type dbRecordList []dbRecord
@ -142,11 +140,34 @@ func (kanji dbKanjiList) crush() dbRecordList {
return results return results
} }
func writeDb(outputPath, title, revision string, sequenced bool, recordData map[string]dbRecordList, stride int, pretty bool) error { type dbIndex struct {
Title string `json:"title"`
Format int `json:"format"`
Revision string `json:"revision"`
Sequenced bool `json:"sequenced"`
Author string `json:"author"`
Url string `json:"url"`
Description string `json:"description"`
Attribution string `json:"attribution"`
}
func (index *dbIndex) setDefaults() {
if index.Format == 0 {
index.Format = 3
}
if index.Author == "" {
index.Author = "yomichan-import"
}
if index.Url == "" {
index.Url = "https://github.com/FooSoft/yomichan-import"
}
}
func writeDb(outputPath string, index dbIndex, recordData map[string]dbRecordList, stride int, pretty bool) error {
var zbuff bytes.Buffer var zbuff bytes.Buffer
zip := zip.NewWriter(&zbuff) zip := zip.NewWriter(&zbuff)
marshalJSON := func(obj interface{}, pretty bool) ([]byte, error) { marshalJSON := func(obj any, pretty bool) ([]byte, error) {
if pretty { if pretty {
return json.MarshalIndent(obj, "", " ") return json.MarshalIndent(obj, "", " ")
} }
@ -186,17 +207,6 @@ func writeDb(outputPath, title, revision string, sequenced bool, recordData map[
} }
var err error var err error
var db struct {
Title string `json:"title"`
Format int `json:"format"`
Revision string `json:"revision"`
Sequenced bool `json:"sequenced"`
}
db.Title = title
db.Format = databaseFormat
db.Revision = revision
db.Sequenced = sequenced
for recordType, recordEntries := range recordData { for recordType, recordEntries := range recordData {
if _, err := writeDbRecords(recordType, recordEntries); err != nil { if _, err := writeDbRecords(recordType, recordEntries); err != nil {
@ -204,7 +214,7 @@ func writeDb(outputPath, title, revision string, sequenced bool, recordData map[
} }
} }
bytes, err := marshalJSON(db, pretty) bytes, err := marshalJSON(index, pretty)
if err != nil { if err != nil {
return err return err
} }

View File

@ -7,7 +7,7 @@ import (
"foosoft.net/projects/jmdict" "foosoft.net/projects/jmdict"
) )
const jmdictRevision = "jmdict4" const edrdgAttribution = "This publication has included material from the JMdict (EDICT, etc.) dictionary files in accordance with the licence provisions of the Electronic Dictionaries Research Group. See http://www.edrdg.org/"
func jmdictBuildRules(term *dbTerm) { func jmdictBuildRules(term *dbTerm) {
for _, tag := range term.DefinitionTags { for _, tag := range term.DefinitionTags {
@ -234,11 +234,17 @@ func jmdictExportDb(inputPath, outputPath, language, title string, stride int, p
"tag": jmdictBuildTagMeta(entities).crush(), "tag": jmdictBuildTagMeta(entities).crush(),
} }
index := dbIndex{
Title: title,
Revision: "jmdict4",
Sequenced: true,
Attribution: edrdgAttribution,
}
index.setDefaults()
return writeDb( return writeDb(
outputPath, outputPath,
title, index,
jmdictRevision,
true,
recordData, recordData,
stride, stride,
pretty, pretty,

View File

@ -6,8 +6,6 @@ import (
"foosoft.net/projects/jmdict" "foosoft.net/projects/jmdict"
) )
const jmnedictRevision = "jmnedict1"
func jmnedictBuildTagMeta(entities map[string]string) dbTagList { func jmnedictBuildTagMeta(entities map[string]string) dbTagList {
var tags dbTagList var tags dbTagList
@ -103,11 +101,18 @@ func jmnedictExportDb(inputPath, outputPath, language, title string, stride int,
"tag": jmnedictBuildTagMeta(entities).crush(), "tag": jmnedictBuildTagMeta(entities).crush(),
} }
index := dbIndex{
Title: title,
Revision: "jmnedict1",
Sequenced: true,
Description: "",
Attribution: edrdgAttribution,
}
index.setDefaults()
return writeDb( return writeDb(
outputPath, outputPath,
title, index,
jmnedictRevision,
true,
recordData, recordData,
stride, stride,
pretty, pretty,

View File

@ -101,11 +101,18 @@ func epwingExportDb(inputPath, outputPath, language, title string, stride int, p
"term": terms.crush(), "term": terms.crush(),
} }
index := dbIndex{
Title: title,
Revision: strings.Join(revisions, ";"),
Sequenced: true,
Description: "",
Attribution: "",
}
index.setDefaults()
return writeDb( return writeDb(
outputPath, outputPath,
title, index,
strings.Join(revisions, ";"),
true,
recordData, recordData,
stride, stride,
pretty, pretty,

View File

@ -7,8 +7,6 @@ import (
"strings" "strings"
) )
const frequencyRevision = "frequency1"
func frequencyTermsExportDb(inputPath, outputPath, language, title string, stride int, pretty bool) error { func frequencyTermsExportDb(inputPath, outputPath, language, title string, stride int, pretty bool) error {
return frequncyExportDb(inputPath, outputPath, language, title, stride, pretty, "term_meta") return frequncyExportDb(inputPath, outputPath, language, title, stride, pretty, "term_meta")
} }
@ -57,11 +55,18 @@ func frequncyExportDb(inputPath, outputPath, language, title string, stride int,
key: frequencies.crush(), key: frequencies.crush(),
} }
index := dbIndex{
Title: title,
Revision: "frequency1",
Sequenced: false,
Description: "",
Attribution: "",
}
index.setDefaults()
return writeDb( return writeDb(
outputPath, outputPath,
title, index,
frequencyRevision,
false,
recordData, recordData,
stride, stride,
pretty, pretty,

View File

@ -7,8 +7,6 @@ import (
"foosoft.net/projects/jmdict" "foosoft.net/projects/jmdict"
) )
const kanjidicRevision = "kanjidic2"
func kanjidicExtractKanji(entry jmdict.KanjidicCharacter, language string) *dbKanji { func kanjidicExtractKanji(entry jmdict.KanjidicCharacter, language string) *dbKanji {
if entry.ReadingMeaning == nil { if entry.ReadingMeaning == nil {
return nil return nil
@ -161,11 +159,18 @@ func kanjidicExportDb(inputPath, outputPath, language, title string, stride int,
"tag": tags.crush(), "tag": tags.crush(),
} }
index := dbIndex{
Title: title,
Revision: "kanjidic2",
Sequenced: false,
Description: "",
Attribution: edrdgAttribution,
}
index.setDefaults()
return writeDb( return writeDb(
outputPath, outputPath,
title, index,
kanjidicRevision,
false,
recordData, recordData,
stride, stride,
pretty, pretty,

View File

@ -8,8 +8,6 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
const rikaiRevision = "rikai2"
type rikaiEntry struct { type rikaiEntry struct {
kanji string kanji string
kana string kana string
@ -154,11 +152,18 @@ func rikaiExportDb(inputPath, outputPath, language, title string, stride int, pr
"tag": tags.crush(), "tag": tags.crush(),
} }
index := dbIndex{
Title: title,
Revision: "rikai2",
Sequenced: true,
Description: "",
Attribution: "",
}
index.setDefaults()
return writeDb( return writeDb(
outputPath, outputPath,
title, index,
rikaiRevision,
true,
recordData, recordData,
stride, stride,
pretty, pretty,