2016-08-07 01:17:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* 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 (
|
|
|
|
"encoding/json"
|
2016-08-23 03:51:30 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2016-08-07 01:17:02 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
type dbTerm struct {
|
2016-08-07 01:17:02 +00:00
|
|
|
Expression string
|
|
|
|
Reading string
|
|
|
|
Tags []string
|
|
|
|
Glossary []string
|
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
type dbTermList []dbTerm
|
|
|
|
|
|
|
|
func (term *dbTerm) addTags(tags ...string) {
|
2016-08-07 01:17:02 +00:00
|
|
|
for _, tag := range tags {
|
2016-11-05 20:13:13 +00:00
|
|
|
if !hasString(tag, term.Tags) {
|
|
|
|
term.Tags = append(term.Tags, tag)
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
func (term *dbTerm) addTagsPri(tags ...string) {
|
2016-08-07 20:24:56 +00:00
|
|
|
for _, tag := range tags {
|
|
|
|
switch tag {
|
|
|
|
case "news1", "ichi1", "spec1", "gai1":
|
2016-11-05 20:13:13 +00:00
|
|
|
term.addTags("P")
|
2016-08-07 20:24:56 +00:00
|
|
|
fallthrough
|
|
|
|
case "news2", "ichi2", "spec2", "gai2":
|
2016-11-05 20:13:13 +00:00
|
|
|
term.addTags(tag[:len(tag)-1])
|
2016-08-07 20:24:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
func (terms dbTermList) crush() [][]string {
|
|
|
|
var results [][]string
|
|
|
|
for _, t := range terms {
|
|
|
|
result := []string{
|
|
|
|
t.Expression,
|
|
|
|
t.Reading,
|
|
|
|
strings.Join(t.Tags, " "),
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, t.Glossary...)
|
|
|
|
results = append(results, result)
|
2016-08-22 02:51:43 +00:00
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
type dbKanji struct {
|
|
|
|
Character string
|
|
|
|
Onyomi []string
|
|
|
|
Kunyomi []string
|
|
|
|
Tags []string
|
|
|
|
Meanings []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type dbKanjiList []dbKanji
|
|
|
|
|
|
|
|
func (kanji *dbKanji) addTags(tags ...string) {
|
|
|
|
for _, tag := range tags {
|
|
|
|
if !hasString(tag, kanji.Tags) {
|
|
|
|
kanji.Tags = append(kanji.Tags, tag)
|
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
2016-11-05 20:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (kanji dbKanjiList) crush() [][]string {
|
|
|
|
var results [][]string
|
|
|
|
for _, k := range kanji {
|
|
|
|
result := []string{
|
|
|
|
k.Character,
|
|
|
|
strings.Join(k.Onyomi, " "),
|
|
|
|
strings.Join(k.Kunyomi, " "),
|
|
|
|
strings.Join(k.Tags, " "),
|
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
result = append(result, k.Meanings...)
|
|
|
|
results = append(results, result)
|
2016-08-23 03:51:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
return results
|
2016-08-23 03:51:30 +00:00
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-11-06 06:24:57 +00:00
|
|
|
func writeDb(outputDir, title string, termRecords [][]string, kanjiRecords [][]string, entities map[string]string, pretty bool) error {
|
2016-11-05 20:13:13 +00:00
|
|
|
const DB_VERSION = 1
|
|
|
|
const BANK_STRIDE = 50000
|
|
|
|
|
|
|
|
marshalJson := func(obj interface{}, pretty bool) ([]byte, error) {
|
|
|
|
if pretty {
|
|
|
|
return json.MarshalIndent(obj, "", " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(obj)
|
|
|
|
}
|
|
|
|
|
2016-11-06 06:24:57 +00:00
|
|
|
writeDbRecords := func(prefix string, records [][]string) (int, error) {
|
|
|
|
recordCount := len(records)
|
|
|
|
bankCount := 0
|
|
|
|
|
|
|
|
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 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fp, err := os.Create(path.Join(outputDir, fmt.Sprintf("%s_bank_%d.json", prefix, i/BANK_STRIDE+1)))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
if _, err = fp.Write(bytes); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bankCount += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return bankCount, nil
|
2016-11-05 20:13:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-06 06:24:57 +00:00
|
|
|
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
|
|
|
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"`
|
|
|
|
}
|
2016-11-05 20:13:13 +00:00
|
|
|
|
2016-11-06 00:09:23 +00:00
|
|
|
db.Title = title
|
2016-11-06 06:24:57 +00:00
|
|
|
db.Version = DB_VERSION
|
2016-11-05 20:13:13 +00:00
|
|
|
db.Entities = entities
|
2016-11-06 06:24:57 +00:00
|
|
|
|
|
|
|
if db.TermBanks, err = writeDbRecords("term", termRecords); err != nil {
|
|
|
|
return err
|
2016-11-05 20:13:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-06 06:24:57 +00:00
|
|
|
if db.KanjiBanks, err = writeDbRecords("kanji", kanjiRecords); err != nil {
|
2016-08-23 03:51:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
bytes, err := marshalJson(db, pretty)
|
2016-08-23 03:51:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
fp, err := os.Create(path.Join(outputDir, "index.json"))
|
2016-08-07 01:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-05 20:13:13 +00:00
|
|
|
defer fp.Close()
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2016-11-06 06:24:57 +00:00
|
|
|
if _, err := fp.Write(bytes); err != nil {
|
2016-08-23 03:51:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
return nil
|
2016-08-24 16:02:26 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 01:17:02 +00:00
|
|
|
func hasString(needle string, haystack []string) bool {
|
|
|
|
for _, value := range haystack {
|
|
|
|
if needle == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|