View Source Hanyutils (hanyutils v0.3.0)
Utilities for dealing with Chinese characters (Hanzi) and Pinyin.
This module contains several functions which deal with strings containing Han characters or Pinyin. Specifically, the following functionality is present:
iex> Hanyutils.to_marked_pinyin("你好")
"nǐhǎo"
iex> Hanyutils.to_numbered_pinyin("你好")
"ni3hao3"
iex> Hanyutils.to_zhuyin("你好")
"ㄋㄧˇㄏㄠˇ"
iex> Hanyutils.characters?("你好")
true
iex> Hanyutils.mark_pinyin("ni3hao3")
"nǐhǎo"
iex> Hanyutils.number_pinyin("nǐhǎo")
"ni3hao3"
iex> Hanyutils.zhuyin_to_numbered_pinyin("ㄋㄧˇㄏㄠˇ")
"ni3hao3"
iex> Hanyutils.zhuyin_to_marked_pinyin("ㄋㄧˇㄏㄠˇ")
"nǐhǎo"
iex> Hanyutils.pinyin_to_zhuyin("ni3hǎo")
"ㄋㄧˇㄏㄠˇ"
All of these functions are built based on the functionality found in the Hanzi
, Pinyin
and
Zhuyin
modules. If this module does not contain the required functionality you need, it is
possible it can be built manually based on the abstractions in these modules. For instance, the
to_marked_pinyin
function could be implemented as follows:
def to_marked_pinyin(string) do
string
|> Hanzi.read()
|> Hanzi.to_pinyin()
|> Pinyin.marked()
end
Please refer to the documentation of the Hanzi
, Pinyin
, and Zhuyin
modules for more
information.
Link to this section Summary
Functions
Convert a string with numbered Pinyin to marked Pinyin.
Convert a string with marked Pinyin to numbered Pinyin.
Convert a string with Pinyin to Zhuyin
Convert a string containing Han characters to marked Pinyin.
Convert a string containing Han characters to numbered Pinyin.
Convert a string containing Han characters to Zhuyin.
Convert a string with Zhuyin to marked Pinyin.
Convert a string with Zhuyin to numbered Pinyin.
Link to this section Functions
See Hanzi.characters?/1
.
Convert a string with numbered Pinyin to marked Pinyin.
Parses the input using Pinyin.read!/2
and converts the result with Pinyin.marked/1
. Please
refer to the documentation of Pinyin.read/2
if you required details on how the input is
parsed. If no mode is provided, :words
mode is used by default.
examples
Examples
iex> Hanyutils.mark_pinyin("ni3hǎo")
"nǐhǎo"
Convert a string with marked Pinyin to numbered Pinyin.
Parses the input using Pinyin.read!/2
, and converts the result with Pinyin.numbered/1
.
Please refer to the documentation of Pinyin.read/2
if you require details on how the input is
parsed. It is worth noting that the Pinyin.read/2
parser is sensitive to the location of the
tone marker. Pinyin.read!/2
is called in :words
mode if no mode is provided.
examples
Examples
iex> Hanyutils.number_pinyin("ni3hǎo")
"ni3hao3"
Convert a string with Pinyin to Zhuyin
Parses the input using Pinyin.read!/2
, and converts the result with Zhuyin.from_pinyin/1
.
Please refer to the documentation of Pinyin.read/2
if you require details on how the input is
parsed. It is worth noting that the Pinyin.read/2
parser is sensitive to the location of the
tone marker. Pinyin.read!/2
is called in :words
mode if no mode is provided.
examples
Examples
iex> Hanyutils.pinyin_to_zhuyin("ni3hǎo")
"ㄋㄧˇㄏㄠˇ"
iex> Hanyutils.pinyin_to_zhuyin("zhu1yu3chen2")
"ㄓㄨㄩˇㄔㄣˊ"
to_marked_pinyin(string, converter \\ &Hanzi.common_pronunciation/1)
View Source@spec to_marked_pinyin(String.t(), (Hanzi.t() -> Pinyin.pinyin_list())) :: String.t()
Convert a string containing Han characters to marked Pinyin.
For more information about converter
, please refer to Hanzi.to_pinyin/2
.
examples
Examples
iex> Hanyutils.to_marked_pinyin("你好")
"nǐhǎo"
iex> Hanyutils.to_marked_pinyin("你好", &Hanzi.all_pronunciations/1)
"nǐ[ hǎo | hào ]"
to_numbered_pinyin(string, converter \\ &Hanzi.common_pronunciation/1)
View SourceConvert a string containing Han characters to numbered Pinyin.
For more information about converter
, please refer to Hanzi.to_pinyin/2
.
examples
Examples
iex> Hanyutils.to_numbered_pinyin("你好")
"ni3hao3"
iex> Hanyutils.to_numbered_pinyin("你好", &Hanzi.all_pronunciations/1)
"ni3[ hao3 | hao4 ]"
@spec to_zhuyin(String.t(), (Hanzi.t() -> Pinyin.pinyin_list())) :: String.t()
Convert a string containing Han characters to Zhuyin.
Because the Unihan database only provides information about pinyin pronunciations, we convert
the Han characters to pinyin (using Hanzi.to_pinyin/2
), after which we convert the generated
pinyin to zhuyin. The documentation of Hanzi.to_pinyin/2
contains additional information about
the converter
argument.
examples
Examples
iex> Hanyutils.to_zhuyin("你好")
"ㄋㄧˇㄏㄠˇ"
iex> Hanyutils.to_zhuyin("朱宇辰")
"ㄓㄨㄩˇㄔㄣˊ"
iex> Hanyutils.to_zhuyin("你好", &Hanzi.all_pronunciations/1)
"ㄋㄧˇ[ ㄏㄠˇ | ㄏㄠˋ ]"
Convert a string with Zhuyin to marked Pinyin.
Parses the input using Zhuyin.read!/2
, and converts the result with Pinyin.numbered/1
.
Please refer to the documentation of Zhuyin.read/2
if you require details on how the input is
parsed or on the mode
argument. Zhuyin.read!/2
is called in :words
mode if no mode is
provided.
examples
Examples
iex> Hanyutils.zhuyin_to_marked_pinyin("ㄋㄧˇㄏㄠˇ")
"nǐhǎo"
iex> Hanyutils.zhuyin_to_marked_pinyin("ㄓㄨㄩˇㄔㄣˊ")
"zhūyǔchén"
Convert a string with Zhuyin to numbered Pinyin.
Parses the input using Zhuyin.read!/2
, and converts the result with Pinyin.numbered/1
.
Please refer to the documentation of Zhuyin.read/2
if you require information on how the input
is parsed or on the mode
argument. Zhuyin.read!/2
is called in :words
mode if no mode is
provided.
examples
Examples
iex> Hanyutils.zhuyin_to_numbered_pinyin("ㄋㄧˇㄏㄠˇ")
"ni3hao3"
iex> Hanyutils.zhuyin_to_numbered_pinyin("ㄓㄨㄩˇㄔㄣˊ")
"zhu1yu3chen2"