kanjis

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 8 Imported by: 0

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, 新字体, 新漢字). 1. Detect if the given character is a joyo kanji (常用漢字) from shinjitai (新字体・新漢字). 1. 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

func FixFileAsJoyo(input io.Reader, output io.Writer) error

FixFileAsJoyo is similar to FixRuneAsJoyo but for large data such as files.

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

func FixRuneAsJoyo(char rune) rune

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

func FixStringAsJoyo(input string) string

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

func IsJoyoKanji(char rune) bool

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

func IsKyuJitai(char rune) bool

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

func LenDict

func LenDict() int

LenDict returns the number of Joyo Kanjis registered in the dictionary.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-joyokanjis/kanjis"
)

func main() {
	fmt.Println(kanjis.LenDict())
}
Output:

2136

func ResetIgnore

func ResetIgnore()

ResetIgnore clears the ignore list.

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.
Package transform provides a convenient utility for transforming one data format to another.
Package transform provides a convenient utility for transforming one data format to another.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL