From e84763a8a6390dd1a0aff61221a7293f08e2cec6 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 7 Aug 2016 19:55:46 -0700 Subject: [PATCH] Add stroke count --- common.go | 12 ++++++------ kanjidic.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/common.go b/common.go index c94ead2..edf85d0 100644 --- a/common.go +++ b/common.go @@ -42,22 +42,22 @@ type vocabSource struct { Glossary []string } -func (d *vocabSource) addTags(tags ...string) { +func (s *vocabSource) addTags(tags ...string) { for _, tag := range tags { - if !hasString(tag, d.Tags) { - d.Tags = append(d.Tags, tag) + if !hasString(tag, s.Tags) { + s.Tags = append(s.Tags, tag) } } } -func (d *vocabSource) addTagsPri(tags ...string) { +func (s *vocabSource) addTagsPri(tags ...string) { for _, tag := range tags { switch tag { case "news1", "ichi1", "spec1", "gai1": - d.addTags("P") + s.addTags("P") fallthrough case "news2", "ichi2", "spec2", "gai2": - d.addTags(tag[:len(tag)-1]) + s.addTags(tag[:len(tag)-1]) break } } diff --git a/kanjidic.go b/kanjidic.go index 4f4d799..f2e93ce 100644 --- a/kanjidic.go +++ b/kanjidic.go @@ -24,7 +24,9 @@ package main import ( "encoding/json" + "fmt" "io" + "strconv" "strings" "github.com/FooSoft/jmdict" @@ -38,9 +40,18 @@ type kanjiSource struct { Character string Kunyomi []string Onyomi []string + Tags []string Meanings []string } +func (s *kanjiSource) addTags(tags ...string) { + for _, tag := range tags { + if !hasString(tag, s.Tags) { + s.Tags = append(s.Tags, tag) + } + } +} + func buildKanjiJson(kanji []kanjiSource) kanjiJson { dict := kanjiJson{make(map[string][]string)} @@ -48,6 +59,7 @@ func buildKanjiJson(kanji []kanjiSource) kanjiJson { var params []string params = append(params, strings.Join(k.Onyomi, " ")) params = append(params, strings.Join(k.Kunyomi, " ")) + params = append(params, strings.Join(k.Tags, " ")) params = append(params, k.Meanings...) dict.Characters[k.Character] = params } @@ -80,6 +92,31 @@ func outputKanjiJson(writer io.Writer, kanji []kanjiSource, pretty bool) error { func convertKanjidicCharacter(kanjidicCharacter jmdict.KanjidicCharacter) kanjiSource { character := kanjiSource{Character: kanjidicCharacter.Literal} + if level := kanjidicCharacter.Misc.JlptLevel; level != nil { + character.addTags(fmt.Sprintf("jlpt:%s", *level)) + } + + if grade := kanjidicCharacter.Misc.Grade; grade != nil { + character.addTags(fmt.Sprintf("grade:%s", *grade)) + if gradeInt, err := strconv.Atoi(*grade); err == nil { + if gradeInt >= 1 && gradeInt <= 8 { + character.addTags("jouyou") + } else if gradeInt >= 9 && gradeInt <= 10 { + character.addTags("jinmeiyou") + } + } + } + + for _, number := range kanjidicCharacter.DictionaryNumbers { + if number.Type == "heisig" { + character.addTags(fmt.Sprintf("heisig:%s", number.Value)) + } + } + + if counts := kanjidicCharacter.Misc.StrokeCounts; len(counts) > 0 { + character.addTags(fmt.Sprintf("strokes:%s", counts[0])) + } + if kanjidicCharacter.ReadingMeaning != nil { for _, m := range kanjidicCharacter.ReadingMeaning.Meanings { if m.Language == nil || *m.Language == "en" {