Documentation
¶
Overview ¶
Example ¶
This example describes how to parse the JSON data to the kanji.Dict type and how to use the dictionary.
For the JSON format see: https://gist.github.com/KEINOS/fb660943484008b7f5297bb627e0e1b1#format
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON data. // In this exmple only two kanji are registered. Note that "滞"(28382) // contains old kanji "滯"(28399) in "kyu_jitai" element. sampleJSON := `{ "134047": { "joyo_kanji": "𠮟", "yomi": { "on_yomi": [ "シツ" ], "kun_yomi": [ "しか" ], "example_yomi": [ "しか-る" ] }, "raw_info": "𠮟\t\t5\t7S\t2010\tシツ、しか-る" }, "28382": { "joyo_kanji": "滞", "kyu_jitai": "滯", "yomi": { "on_yomi": ["タイ"], "kun_yomi": ["とどこお"], "example_yomi": ["とどこお-る"] }, "raw_info": "滞\t滯\t13\t7S\t\tタイ、とどこお-る" } }` // Create a new dictionary by unmarshaling the JSON dictionary. // If an element contains "kyu_jitai"(old kanji), then a new element for that // old kanji is also added to the dictionary. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } // Check if "滞"(28382, new kanji) is a joyo kanji. fmt.Println("Is 滞 joyo?:", tmpDict.IsJoyoKanji('滞')) // Print the on-yomi and kun-yomi of "滞"(28382, new kanji). fmt.Println("Reading of 滞:", "On yomi:", tmpDict.OnYomi('滞'), "Kun yomi:", tmpDict.KunYomi('滞')) // Since "滞"(28382) contains an old kanji "滯"(28399), a new entry with the // old kanji is also mapped to the dictionary automatically. // // Let's check if "滯"(28399, old kanji) is a joyo kanji (expecting false). fmt.Println("Is 滯 joyo?:", tmpDict.IsJoyoKanji('滯')) // The character '亙' is not in the dictionary. But it is in the default // old-new kanji mapping list (kanji.NonJoyoOld2NewMap). // So it is considered as an old kanji. fmt.Println("Is 亙 old kanji?:", tmpDict.IsKyuJitai('亙')) // For more examples of Dict type methods, see the godoc of Dict type. }
Output: Is 滞 joyo?: true Reading of 滞: On yomi: [タイ] Kun yomi: [とどこお] Is 滯 joyo?: false Is 亙 old kanji?: true
Index ¶
- Variables
- func IsCJK(r rune) bool
- type Dict
- func (d Dict) Find(kanji rune) (Kanji, bool)
- func (d Dict) FixAsJoyo(kanji rune) rune
- func (d Dict) IsJoyoKanji(kanji rune) bool
- func (d Dict) IsKyuJitai(kanji rune) bool
- func (d Dict) KunYomi(kanji rune) []kana.Kanas
- func (d Dict) LenJoyo() int
- func (d Dict) Marshal() ([]byte, error)
- func (d Dict) MarshalIndent(prefix string, indent string) ([]byte, error)
- func (d Dict) OnYomi(kanji rune) []kana.Kanas
- type Kanji
- type KanjiChar
- type Yomi
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var NonJoyoOld2NewMap = map[rune]rune{
'亙': '亘',
'冱': '冴',
'凛': '凜',
'堯': '尭',
'巖': '巌',
'晉': '晋',
'槇': '槙',
'瑤': '瑶',
'瓣': '弁',
'祿': '禄',
'穰': '穣',
'聰': '聡',
'萠': '萌',
'藪': '薮',
'辯': '弁',
'遙': '遥',
'邉': '辺',
'鬪': '闘',
'猪': '猪',
}
NonJoyoOld2NewMap is a key-value mapping for old kanji ('kyu-jitai') to new kanji ('shin-jitai') which are not in Joyo Kanji list.
Functions ¶
func IsCJK ¶
IsCJK returns true if the given rune is in a range of CJK Unified Ideographs (including extensions upto Extension D).
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { for _, test := range []struct { input rune expect bool }{ // CJK {input: '一', expect: true}, {input: '漢', expect: true}, {input: '巣', expect: true}, {input: '巢', expect: true}, // Non-CJK {input: 'あ', expect: false}, {input: 'ア', expect: false}, {input: 'a', expect: false}, {input: '+', expect: false}, } { expect := test.expect actual := kanji.IsCJK(test.input) if expect != actual { log.Fatalf("%s expected to be %v but got %v", string(test.input), expect, actual) } } fmt.Println("OK") }
Output: OK
Types ¶
type Dict ¶
Dict is a map of Kanji objects that works as a hash table. The key is the rune (int32) that represents the Kanji.
func NewDict ¶
NewDict parses the JSON data of Joyo Kanji dictionary to kanji.Dict object.
If the registered Joyo Kanji has an old kanji (kyu jitai), an alias key will be added to the dictionary to speed up the search.
See the following URL for the format of the JSON byte array:
https://gist.github.com/KEINOS/fb660943484008b7f5297bb627e0e1b1#format
func (Dict) Find ¶
Find searches the given Kanji in the Joyo Kanji dictionary and returns the corresponding Kanji object. The returned boolean value indicates if the Kanji was found.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON dictionary. // In this example only one kanji (new and old) is registered. sampleJSON := `{ "27005": { "joyo_kanji": "楽", "kyu_jitai": "樂", "yomi": { "on_yomi": [ "ガク", "ラク" ], "kun_yomi": [ "たの" ], "example_yomi": [ "たの-しい", "たの-しむ" ] }, "raw_info": "楽\t樂\t13\t2\t\tガク、ラク、たの-しい、たの-しむ" } }` // Create a new dictionary from the JSON dictionary. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } if foundKanji, ok := tmpDict.Find('楽'); ok { fmt.Println("Is kyu-jitai?:", foundKanji.IsKyuJitai) fmt.Println("Shin jitai (new form kanji):", foundKanji.ShinJitai) fmt.Println("Kyu jitai (old form kanji):", foundKanji.KyuJitai) fmt.Println("On-reading:", foundKanji.Yomi.OnYomi) fmt.Println("Kun-reading:", foundKanji.Yomi.KunYomi) fmt.Println("Examples of the readings:", foundKanji.Yomi.ExampleYomi) } }
Output: Is kyu-jitai?: false Shin jitai (new form kanji): 楽 Kyu jitai (old form kanji): 樂 On-reading: [ガク ラク] Kun-reading: [たの] Examples of the readings: [たの-しい たの-しむ]
func (Dict) FixAsJoyo ¶
FixAsJoyo converts the given Kanji to the Joyo Kanji (regular use kanji) or Shin Jitai (new form of kanji) as much as possible.
It will search the embedded Joyo Kanji dictionary then the Non-Joyo Kanji (old-new kanji mapping) dictionary.
To add a new kanji, edit the file `non_joyo_old2new_map.go`.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON dictionary. // In this example only one kanji (new and old) is registered. sampleJSON := `{ "27005": { "joyo_kanji": "楽", "kyu_jitai": "樂", "yomi": { "on_yomi": [ "ガク", "ラク" ], "kun_yomi": [ "たの" ], "example_yomi": [ "たの-しい", "たの-しむ" ] }, "raw_info": "楽\t樂\t13\t2\t\tガク、ラク、たの-しい、たの-しむ" } }` // Create a new dictionary from the JSON dictionary. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } // Fix if rune is a registered old kanji to the new kanji. for index, test := range []struct { input rune expect rune }{ {input: '樂', expect: '楽'}, // Registered old kanji {input: '楽', expect: '楽'}, // Registered new kanji {input: 'あ', expect: 'あ'}, // Non kanji (not CJK) {input: '滯', expect: '滯'}, // Non registered kanji in the JSON dictionary {input: '亙', expect: '亘'}, // Non registered but NonJoyoOld2NewMap has the mapping } { expect := test.expect actual := tmpDict.FixAsJoyo(test.input) fmt.Printf("#%d: %s expected to be: %s, got: %s\n", index+1, string(test.input), string(expect), string(actual)) } }
Output: #1: 樂 expected to be: 楽, got: 楽 #2: 楽 expected to be: 楽, got: 楽 #3: あ expected to be: あ, got: あ #4: 滯 expected to be: 滯, got: 滯 #5: 亙 expected to be: 亘, got: 亘
func (Dict) IsJoyoKanji ¶
IsJoyoKanji returns true if the given Kanji is a Joyo Kanji.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON dictionary. // In this example only one kanji (new and old) is registered. sampleJSON := `{ "27005": { "joyo_kanji": "楽", "kyu_jitai": "樂", "yomi": { "on_yomi": [ "ガク", "ラク" ], "kun_yomi": [ "たの" ], "example_yomi": [ "たの-しい", "たの-しむ" ] }, "raw_info": "楽\t樂\t13\t2\t\tガク、ラク、たの-しい、たの-しむ" } }` // Create a new dictionary from the JSON dictionary. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } // Search the registered kanji fmt.Println("Is '楽' a joyo kanji?:", tmpDict.IsJoyoKanji('楽')) fmt.Println("Is '樂' a joyo kanji?:", tmpDict.IsJoyoKanji('樂')) // Non-registered kanji/rune returns false. fmt.Println("Is '忍' a joyo kanji?:", tmpDict.IsJoyoKanji('忍')) fmt.Println("Is '아' a joyo kanji?:", tmpDict.IsJoyoKanji('아')) }
Output: Is '楽' a joyo kanji?: true Is '樂' a joyo kanji?: false Is '忍' a joyo kanji?: false Is '아' a joyo kanji?: false
func (Dict) IsKyuJitai ¶
IsKyuJitai returns true if the given Kanji is a KyuJitai (old kanji).
Note that it only detects if the old kanji is registered in the dictionary.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON dictionary. // In this example only one kanji (new and old) is registered. sampleJSON := `{ "27005": { "joyo_kanji": "楽", "kyu_jitai": "樂", "yomi": { "on_yomi": [ "ガク", "ラク" ], "kun_yomi": [ "たの" ], "example_yomi": [ "たの-しい", "たの-しむ" ] }, "raw_info": "楽\t樂\t13\t2\t\tガク、ラク、たの-しい、たの-しむ" } }` // Create a new dictionary from the JSON dictionary. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } // Search the registered kanji fmt.Println("Is '楽' a kyu-jitai?:", tmpDict.IsKyuJitai('楽')) fmt.Println("Is '樂' a kyu-jitai?:", tmpDict.IsKyuJitai('樂')) // Non-registered kanji/rune returns false. fmt.Println("Is '忍' a kyu-jitai?:", tmpDict.IsKyuJitai('忍')) fmt.Println("Is '아' a kyu-jitai?:", tmpDict.IsJoyoKanji('아')) }
Output: Is '楽' a kyu-jitai?: false Is '樂' a kyu-jitai?: true Is '忍' a kyu-jitai?: false Is '아' a kyu-jitai?: false
func (Dict) KunYomi ¶
KunYomi returns the KunYomi reading in hiragana of the given Kanji.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON dictionary. sampleJSON := `{ "32905": { "joyo_kanji": "肉", "yomi": { "on_yomi": [ "ニク" ] }, "raw_info": "肉\t\t6\t2\t\tニク" }, "32908": { "joyo_kanji": "肌", "yomi": { "kun_yomi": [ "はだ" ] }, "raw_info": "肌\t\t6\t7S\t1981\tはだ" } }` // Create a new dictionary from the JSON dictionary. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } fmt.Println("肉:", tmpDict.KunYomi('肉')) fmt.Println("肌:", tmpDict.KunYomi('肌')) fmt.Println("忍:", tmpDict.KunYomi('忍')) // Not in the current dictionary }
Output: 肉: [] 肌: [はだ] 忍: []
func (Dict) LenJoyo ¶
LenJoyo counts and returns the number of Joyo Kanji elements registered in the dictionary.
Notes:
- It does not count the KyuJitai (old kanji) entry.
- This method is not cached and is not suitable for frequent calls.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON dictionary. // In this example only one kanji (with kyu-jitai, old kanji) is registered. sampleJSON := `{ "27005": { "joyo_kanji": "楽", "kyu_jitai": "樂", "yomi": { "on_yomi": [ "ガク", "ラク" ], "kun_yomi": [ "たの" ], "example_yomi": [ "たの-しい", "たの-しむ" ] }, "raw_info": "楽\t樂\t13\t2\t\tガク、ラク、たの-しい、たの-しむ" } }` // Create a new dictionary from the JSON dictionary. Since the registered // kanji has a kyu-jitai, it will add an additional element to the dictionary // to speed up the search. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } // Expect 1 fmt.Println("Total joyo kanji in the dictionary:", tmpDict.LenJoyo()) // Expect 2 (1 joyo kanji + 1 kyu-jitai) fmt.Println("Total elements in the dictionary:", len(*tmpDict)) }
Output: Total joyo kanji in the dictionary: 1 Total elements in the dictionary: 2
func (Dict) Marshal ¶
Marshal returns the JSON encoding of the dictionary.
It is similar to MarshalJSON implementation but it strips the additional KyuJitai (old kanji) entries for search speed.
Example ¶
package main import ( "encoding/json" "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { sampleJSON := `{ "28382": { "joyo_kanji": "滞", "kyu_jitai": "滯", "yomi": { "on_yomi": ["タイ"], "kun_yomi": ["とどこお"], "example_yomi": ["とどこお-る"] }, "raw_info": "滞\t滯\t13\t7S\t\tタイ、とどこお-る" } }` // Create the dictionary (unmarshal JSON to Dict). It will add kyujitai // entry to the Dict to speed up the search. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } // Convert/marshal the Dict obj to JSON. It trimms the additional kyujitai // entry for serchability. To pretty print the JSON, use the Dict.MarshalIndent() // method instead. jsonDictTrimmed, err := tmpDict.Marshal() if err != nil { log.Fatal(err) } fmt.Println(string(jsonDictTrimmed)) // To get the full entries (including the additional entries for searching), // use the ordinary json.Marshal() function. jsonDictRaw, err := json.Marshal(tmpDict) if err != nil { log.Fatal(err) } fmt.Println(string(jsonDictRaw)) }
Output: {"28382":{"yomi":{"on_yomi":["タイ"],"kun_yomi":["とどこお"],"example_yomi":["とどこお-る"]},"joyo_kanji":"滞","kyu_jitai":"滯"}} {"28382":{"yomi":{"on_yomi":["タイ"],"kun_yomi":["とどこお"],"example_yomi":["とどこお-る"]},"joyo_kanji":"滞","kyu_jitai":"滯"},"28399":{"yomi":{"on_yomi":["タイ"],"kun_yomi":["とどこお"],"example_yomi":["とどこお-る"]},"joyo_kanji":"滞","kyu_jitai":"滯"}}
func (Dict) MarshalIndent ¶
MarshalIndent returns the indented JSON encoding of the dictionary.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { sampleJSON := `{ "28382": { "joyo_kanji": "滞", "kyu_jitai": "滯", "yomi": { "on_yomi": ["タイ"], "kun_yomi": ["とどこお"], "example_yomi": ["とどこお-る"] }, "raw_info": "滞\t滯\t13\t7S\t\tタイ、とどこお-る" } }` // Create the dictionary (unmarshal JSON to Dict). It will add kyujitai entry // to the Dict to speed up the search. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } // It is similar to Dict.Marshal() but it pretty prints the JSON. // // Note that additional entries for searching are trimmed. For printing // the full entries, use the ordinary json.MarshalIndent() function. prefix := "" indent := " " jsonDictTrimmed, err := tmpDict.MarshalIndent(prefix, indent) if err != nil { log.Fatal(err) } fmt.Println(string(jsonDictTrimmed)) }
Output: { "28382": { "yomi": { "on_yomi": [ "タイ" ], "kun_yomi": [ "とどこお" ], "example_yomi": [ "とどこお-る" ] }, "joyo_kanji": "滞", "kyu_jitai": "滯" } }
func (Dict) OnYomi ¶
OnYomi returns the OnYomi reading in katakana of the given Kanji.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { // Sample JSON dictionary. sampleJSON := `{ "32905": { "joyo_kanji": "肉", "yomi": { "on_yomi": [ "ニク" ] }, "raw_info": "肉\t\t6\t2\t\tニク" }, "32908": { "joyo_kanji": "肌", "yomi": { "kun_yomi": [ "はだ" ] }, "raw_info": "肌\t\t6\t7S\t1981\tはだ" } }` // Create a new dictionary from the JSON dictionary. tmpDict, err := kanji.NewDict([]byte(sampleJSON)) if err != nil { log.Fatal(err) } fmt.Println("肉:", tmpDict.OnYomi('肉')) fmt.Println("肌:", tmpDict.OnYomi('肌')) fmt.Println("忍:", tmpDict.OnYomi('忍')) // Not in the current dictionary }
Output: 肉: [ニク] 肌: [] 忍: []
type Kanji ¶
type Kanji struct { // Yomi is the reading info of the Kanji. Yomi Yomi `json:"yomi,omitempty"` // ShinJitai is the Joyo Kanji in new kanji form. ShinJitai KanjiChar `json:"joyo_kanji,omitempty"` // KyuJitai is the old kanji form which is mapped to the shinjitai. KyuJitai KanjiChar `json:"kyu_jitai,omitempty"` // IsKyuJitai is true if the map key is a KyuJitai. IsKyuJitai bool `json:"-"` }
Kanji is a struct that represents a Joyo Kanji.
type KanjiChar ¶
type KanjiChar rune
KanjiChar is a rune that represents a Kanji. It is used to make rune readable in JSON format. It implements the Stringer, Marshaler and Unmarshaler interface.
Example ¶
package main import ( "encoding/json" "fmt" "log" "github.com/Code-Hex/dd" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" "github.com/MakeNowJust/heredoc" ) func main() { // Note that each field is a KanjiChar type, whcih is a rune. type SampleStruct struct { Kanji1 kanji.KanjiChar `json:"kanji_1"` Kanji2 kanji.KanjiChar `json:"kanji_2"` Kanji3 kanji.KanjiChar `json:"kanji_3"` } // Note that "kanji_2" has 2 runes. KanjiChar will only take the first rune. jsonData := heredoc.Doc(`{ "kanji_1":"忍", "kanji_2":"忍者", "kanji_3":"者" }`) var sampleObj SampleStruct // Parse JSON object and store the result to the pointer. err := json.Unmarshal([]byte(jsonData), &sampleObj) if err != nil { log.Fatal(err) } fmt.Println(dd.Dump(sampleObj)) }
Output: kanji_test.SampleStruct{ Kanji1: 24525, Kanji2: 24525, Kanji3: 64091, }
func (KanjiChar) MarshalJSON ¶
MarshalJSON returns the JSON representation of the KanjiChar. It is a Marshaler interface implementation.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { charRaw := '滞' charKanji := kanji.KanjiChar(charRaw) jsonChar, err := charKanji.MarshalJSON() if err != nil { log.Fatal(err) } fmt.Println(string(jsonChar)) }
Output: "滞"
func (KanjiChar) String ¶
String returns the string representation of the KanjiChar.
Example ¶
package main import ( "fmt" "github.com/KEINOS/go-joyokanjis/kanjis/kanji" ) func main() { charRaw := '滞' charKanji := kanji.KanjiChar(charRaw) fmt.Println("Raw:", charRaw) fmt.Println("Stringer:", charKanji) // It implements the fmt.Stringer interface fmt.Println("String:", charKanji.String()) }
Output: Raw: 28382 Stringer: 滞 String: 滞
func (*KanjiChar) UnmarshalJSON ¶
UnmarshalJSON unmarshals the JSON representation of the KanjiChar.
type Yomi ¶
type Yomi struct { // OnYomi is the list of "On" readings (on-yomi, "音読み") of the Kanji. // Which is the reading derived from the Chinese pronunciations OnYomi []kana.Kanas `json:"on_yomi,omitempty"` // KunYomi is the list of "Kun" readings (kun-yomi, "訓読み") of the Kanji. // Which is the original, indigenous Japanese readings. KunYomi []kana.Kanas `json:"kun_yomi,omitempty"` // ExampleYomi is the list of example readings of the Kanji. ExampleYomi []string `json:"example_yomi,omitempty"` }
Yomi is a struct that represents the Yomi (reading, "読み") of a Kanji.