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 (
|
2016-12-19 01:46:40 +00:00
|
|
|
"os"
|
2016-08-08 02:55:46 +00:00
|
|
|
"strconv"
|
2016-08-07 01:17:02 +00:00
|
|
|
|
|
|
|
"github.com/FooSoft/jmdict"
|
|
|
|
)
|
|
|
|
|
2017-09-10 20:25:11 +00:00
|
|
|
const kanjidicRevision = "kanjidic1"
|
2016-12-24 05:52:49 +00:00
|
|
|
|
2017-06-11 00:35:58 +00:00
|
|
|
func kanjidicExtractKanji(entry jmdict.KanjidicCharacter, language string) *dbKanji {
|
|
|
|
if entry.ReadingMeaning == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-16 20:54:28 +00:00
|
|
|
kanji := dbKanji{
|
|
|
|
Character: entry.Literal,
|
|
|
|
Indices: make(map[string]string),
|
|
|
|
Stats: make(map[string]string),
|
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2017-06-11 00:35:58 +00:00
|
|
|
for _, m := range entry.ReadingMeaning.Meanings {
|
|
|
|
if m.Language == nil && language == "" || m.Language != nil && language == *m.Language {
|
|
|
|
kanji.Meanings = append(kanji.Meanings, m.Meaning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(kanji.Meanings) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-16 20:54:28 +00:00
|
|
|
for _, number := range entry.DictionaryNumbers {
|
|
|
|
kanji.Indices[number.Type] = number.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
if frequency := entry.Misc.Frequency; frequency != nil {
|
|
|
|
kanji.Stats["Frequency"] = *frequency
|
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
if level := entry.Misc.JlptLevel; level != nil {
|
2017-09-16 20:54:28 +00:00
|
|
|
kanji.Stats["JLPT Level"] = *level
|
|
|
|
}
|
|
|
|
|
|
|
|
if counts := entry.Misc.StrokeCounts; len(counts) > 0 {
|
|
|
|
kanji.Stats["Strokes"] = counts[0]
|
2016-08-08 02:55:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
if grade := entry.Misc.Grade; grade != nil {
|
2017-09-16 20:54:28 +00:00
|
|
|
kanji.Stats["Grade"] = *grade
|
2016-08-08 02:55:46 +00:00
|
|
|
if gradeInt, err := strconv.Atoi(*grade); err == nil {
|
|
|
|
if gradeInt >= 1 && gradeInt <= 8 {
|
2016-11-05 20:13:13 +00:00
|
|
|
kanji.addTags("jouyou")
|
2016-08-08 02:55:46 +00:00
|
|
|
} else if gradeInt >= 9 && gradeInt <= 10 {
|
2016-11-05 20:13:13 +00:00
|
|
|
kanji.addTags("jinmeiyou")
|
2016-08-08 02:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 00:35:58 +00:00
|
|
|
for _, r := range entry.ReadingMeaning.Readings {
|
|
|
|
switch r.Type {
|
|
|
|
case "ja_on":
|
|
|
|
kanji.Onyomi = append(kanji.Onyomi, r.Value)
|
|
|
|
case "ja_kun":
|
|
|
|
kanji.Kunyomi = append(kanji.Kunyomi, r.Value)
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 00:35:58 +00:00
|
|
|
return &kanji
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 00:22:17 +00:00
|
|
|
func kanjidicExportDb(inputPath, outputPath, language, title string, stride int, pretty bool) error {
|
2016-12-19 01:46:40 +00:00
|
|
|
reader, err := os.Open(inputPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
2016-08-24 16:02:26 +00:00
|
|
|
dict, err := jmdict.LoadKanjidic(reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-23 03:51:30 +00:00
|
|
|
|
2017-06-11 00:35:58 +00:00
|
|
|
var langTag string
|
|
|
|
switch language {
|
|
|
|
case "french":
|
|
|
|
langTag = "fr"
|
|
|
|
case "spanish":
|
|
|
|
langTag = "es"
|
|
|
|
case "portuguese":
|
|
|
|
langTag = "pt"
|
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
var kanji dbKanjiList
|
|
|
|
for _, entry := range dict.Characters {
|
2017-06-11 00:35:58 +00:00
|
|
|
kanjiCurr := kanjidicExtractKanji(entry, langTag)
|
|
|
|
if kanjiCurr != nil {
|
|
|
|
kanji = append(kanji, *kanjiCurr)
|
|
|
|
}
|
2016-08-24 16:02:26 +00:00
|
|
|
}
|
2016-08-07 01:17:02 +00:00
|
|
|
|
2017-09-10 20:37:40 +00:00
|
|
|
if title == "" {
|
|
|
|
title = "KANJIDIC2"
|
|
|
|
}
|
|
|
|
|
2017-09-10 20:25:11 +00:00
|
|
|
tags := dbTagList{
|
|
|
|
dbTag{Name: "jouyou", Notes: "included in list of regular-use characters", Category: "frequent", Order: -5},
|
|
|
|
dbTag{Name: "jinmeiyou", Notes: "included in list of characters for use in personal names", Category: "frequent", Order: -5},
|
2016-12-17 23:48:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 20:25:11 +00:00
|
|
|
recordData := map[string]dbRecordList{
|
|
|
|
"kanji": kanji.crush(),
|
2017-09-10 20:45:06 +00:00
|
|
|
"tag": tags.crush(),
|
2017-09-10 20:25:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 20:13:13 +00:00
|
|
|
return writeDb(
|
2017-06-26 00:22:17 +00:00
|
|
|
outputPath,
|
2016-11-06 00:09:23 +00:00
|
|
|
title,
|
2017-09-10 20:25:11 +00:00
|
|
|
kanjidicRevision,
|
|
|
|
recordData,
|
2016-12-29 01:45:33 +00:00
|
|
|
stride,
|
2016-12-19 01:31:27 +00:00
|
|
|
pretty,
|
2016-11-05 20:13:13 +00:00
|
|
|
)
|
2016-08-07 01:17:02 +00:00
|
|
|
}
|