Documentation ¶
Overview ¶
Package kanjis is a set of handy function to access the singleton kanji.Dict object of the embedded dictionary.
It provides functions to:
1. Convert old kanji (kyujitai, 旧字体, 旧漢字) to new kanji (shinjitai, 新字体, 新漢字).
2. Detect if the given character is a joyo kanji (常用漢字) from shinjitai (新字体・新漢字).
3. Search for the readings (読み, yomi) of the given kanji.
Example ¶
package main import ( "fmt" "github.com/KEINOS/go-joyokanjis/kanjis" "github.com/MakeNowJust/heredoc" ) func main() { input := heredoc.Doc(` いざ、これより樂しまむ、 仕置を受くる憂なく、 遊びたのしむ時ぞ來ぬ、 時ぞ來ぬれば、いちはやく、 讀本などは投げ捨てて行く。 ――學校休暇の歌`) output := kanjis.FixStringAsJoyo(input) fmt.Println(output) }
Output: いざ、これより楽しまむ、 仕置を受くる憂なく、 遊びたのしむ時ぞ来ぬ、 時ぞ来ぬれば、いちはやく、 読本などは投げ捨てて行く。 ――学校休暇の歌
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FixFileAsJoyo ¶
FixFileAsJoyo is similar to FixRuneAsJoyo but for file.
Example ¶
package main import ( "bytes" "fmt" "log" "strings" "github.com/KEINOS/go-joyokanjis/kanjis" "github.com/MakeNowJust/heredoc" ) func main() { // File content input := strings.NewReader(heredoc.Doc(` いざ、これより樂しまむ、 仕置を受くる憂なく、 遊びたのしむ時ぞ來ぬ、 時ぞ來ぬれば、いちはやく、 讀本などは投げ捨てて行く。 ――學校休暇の歌`)) // Output buffer var output bytes.Buffer // Parse and fix to Joyo Kanji if err := kanjis.FixFileAsJoyo(input, &output); err != nil { log.Fatal(err) } fmt.Println(output.String()) }
Output: いざ、これより楽しまむ、 仕置を受くる憂なく、 遊びたのしむ時ぞ来ぬ、 時ぞ来ぬれば、いちはやく、 読本などは投げ捨てて行く。 ――学校休暇の歌
func FixRuneAsJoyo ¶
FixRuneAsJoyo returns the Joyo Kanji if the given character is a registered Kyujitai (old kanji) and has a new kanji (shinjitai) in the dictionary.
Otherwise, it returns the given character. Thus, ASCII and other non-Japanese characters are returned as is. Also note that kyujitai characters that do not have a shinjitai are returned as is as well.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis" ) func main() { for _, test := range []struct { input rune expect rune }{ {input: '漢', expect: '漢'}, {input: '漢', expect: '漢'}, {input: '巣', expect: '巣'}, {input: '巢', expect: '巣'}, {input: 'a', expect: 'a'}, {input: 'あ', expect: 'あ'}, {input: 'ア', expect: 'ア'}, } { expect := test.expect actual := kanjis.FixRuneAsJoyo(test.input) if expect != actual { log.Fatalf("ERROR: Expected %q but got %q", string(expect), string(actual)) } } fmt.Println("OK") }
Output: OK
func FixStringAsJoyo ¶
FixStringAsJoyo is similar to FixRuneAsJoyo but for string.
If the input is larger than 320 Bytes, consider using FixFileAsJoyo() instead.
Example ¶
package main import ( "fmt" "github.com/KEINOS/go-joyokanjis/kanjis" ) func main() { input := "これは舊漢字です。" output := kanjis.FixStringAsJoyo(input) fmt.Println(output) }
Output: これは旧漢字です。
func Ignore ¶
func Ignore(char ...rune)
Ignore adds the given characters to the ignore list. These characters will be ignored when converting old kanji (kyujitai) to new kanji (shinjitai).
Example ¶
package main import ( "fmt" "github.com/KEINOS/go-joyokanjis/kanjis" ) func main() { const input = "私は渡邉です。" { // Add '邉' and '邊' to be ignored when fixing. kanjis.Ignore('邉', '邊') fmt.Println("Fix with Ignore:", kanjis.FixStringAsJoyo(input)) } { // Clear the ignore list. kanjis.ResetIgnore() fmt.Println("Fix with no-ignore:", kanjis.FixStringAsJoyo(input)) } }
Output: Fix with Ignore: 私は渡邉です。 Fix with no-ignore: 私は渡辺です。
func IsJoyoKanji ¶
IsJoyoKanji returns true if the given rune is a Joyo Kanji character.
Example ¶
package main import ( "fmt" "github.com/KEINOS/go-joyokanjis/kanjis" ) func main() { newKanji := '漢' if kanjis.IsJoyoKanji(newKanji) { fmt.Printf("%s (0x%x) is Joyo Kanji\n", string(newKanji), newKanji) } oldKanji := '漢' if !kanjis.IsJoyoKanji(oldKanji) { fmt.Printf("%s (0x%x) is not a Joyo Kanji\n", string(oldKanji), oldKanji) } }
Output: 漢 (0x6f22) is Joyo Kanji 漢 (0xfa47) is not a Joyo Kanji
func IsKyuJitai ¶
IsKyuJitai returns true if the given rune is a registered Kyujitai (old kanji) character which contains a new kanji (shinjitai) in the dictionary.
Example ¶
package main import ( "fmt" "log" "github.com/KEINOS/go-joyokanjis/kanjis" ) func main() { for index, test := range []struct { input rune expect bool }{ {input: '漢', expect: false}, // New kanji {input: '漢', expect: true}, // Old kanji {input: '亙', expect: true}, // Old kanji but not in Joyo Kanji list {input: 'a', expect: false}, // Not a kanji } { expect := test.expect actual := kanjis.IsKyuJitai(test.input) if expect != actual { log.Fatalf("test #%d failed: IsKyuJitai('%s') expected to be %t but got %t", index, string(test.input), expect, actual) } } fmt.Println("OK") }
Output: OK
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
This package generates the Joyo-kanji dictionary to be embedded in the package.
|
This package generates the Joyo-kanji dictionary to be embedded in the package. |
Package kana provides a type for Kana characters and functions for katakana and hiragana conversion.
|
Package kana provides a type for Kana characters and functions for katakana and hiragana conversion. |