Documentation
¶
Overview ¶
Package hangulize transcribes non-Korean words into Hangul.
"Hello!" -> "헬로!"
Hangulize was inspired by Brian Jongseong Park (http://iceager.egloos.com/2610028). Based on this idea, the original Hangulize was developed in Python and went out in 2010 (https://github.com/sublee/hangulize). Since then, serving as a web application on http://hangulize.org/, it has been of great help for Korean translators.
This Go re-implementation is a reboot of Hangulize with feature improvements.
Pipeline ¶
Hangulize transcribes with 5 steps. These steps include "Normalize", "Group", "Rewrite", "Transcribe", and "Compose". To clarify these concepts, let's consider an imaginary example of "Hello!" in English into "헬로!" (actually, English is not supported yet).
First, Hangulize normalizes letter cases:
"Hello" -> "hello!"
And then, it groups letters by meanings:
"hello!" -> "hello", "!"
After that, grouped chunks are rewritten as source language-specific rules. This step is usually for minimizing the differences between pronunciation and spelling:
"hello", "!" -> "heˈlō", "!"
And it transcribes rewritten chunks into Hangul Jamo phonemes.
"heˈlō", "!" -> "ㅎㅔ-ㄹㄹㅗ", "!"
Finally, it composes Jamo phonemes to Hangul syllables and joins all groups.
"ㅎㅔ-ㄹㄹㅗ", "!" -> "헬로!"
Spec ¶
A spec is written by the HGL format which is a configuration DSL for Hangulize 2. One spec is for one language transcription system. So we need to describe about the language at the first:
lang:
id = "ita"
codes = "it", "ita" # ISO 639-1 and 3 codes
english = "Italian"
korean = "이탈리아어"
script = "roman"
Then write about yourself and the stage of this spec:
config:
author = "John Doe <john@example.com>"
stage = "draft"
We will write many patterns in rewrite/transcribe rules soon. Some expressions may appear many times annoyingly. To not repeat ourselves, we can use variables and macros.
A variable is a combination of letters. Variable in pattern will match with one of the letters. Variable "foo" can be referenced with "<foo>" in the patterns.
vars:
"vowels" = "a", "e", "i", "o", "u"
A macro expression is replaced with the target before parsing the patterns. "@" is the common macro for "<vowels>" variable:
macros:
"@" = "<vowels>"
Now we can write "rewrite" rules. There are Pattern and RPattern. Pattern matches with letters in a word. RPattern represents how the matched letters should be replaced. A replaced word by a rule would become as the input for the next rule:
rewrite:
"^gli$" -> "li"
"^gli{@}" -> "li"
"{@}gli" -> "li"
"gn{@}" -> "nJ"
Pattern is based on Regular Expression but it has it's own custom syntax. We call it "HRE" which means "Hangulize-specific Regular Expression". For the detail, see the documentation of Pattern.
"transcribe" rules are exactly same with "rewrite" rules. But it's RPatterns represent Hangul Jamo phonemes. In contrast to "rewrite", a replaced word won't become as the input for the next rules:
transcribe:
"b" -> "ㅂ"
"d" -> "ㄷ"
"f" -> "ㅍ"
"g" -> "ㄱ"
Finally, we should write expected transcription examples. They are used for unit testing. Verify your spec yourself:
test:
"allegretto" -> "알레그레토"
"gita" -> "지타"
"bisnonno" -> "비스논노"
"Pinocchio" -> "피노키오"
Example ¶
// Person names from http://iceager.egloos.com/2610028
fmt.Println(Hangulize("ron", "Cătălin Moroşanu"))
fmt.Println(Hangulize("nld", "Jerrel Venetiaan"))
fmt.Println(Hangulize("por", "Vítor Constâncio"))
Output: 커털린 모로샤누 예럴 페네티안 비토르 콘스탄시우
Index ¶
- Constants
- func ComposeHangul(word string) string
- func Hangulize(lang string, word string) string
- func ListLangs() []string
- func UnusePronouncer(id string) bool
- func UsePronouncer(p Pronouncer) bool
- type Config
- type Hangulizer
- type Language
- type Pattern
- type Pronouncer
- type RPattern
- type Rule
- type Spec
- type Trace
Examples ¶
Constants ¶
const Version = "0.2.0"
Version is the version number of Hangulize package. The version follows Semantic Versioning 2.0.0.
Variables ¶
This section is empty.
Functions ¶
func ComposeHangul ¶
ComposeHangul converts decomposed Jamo phonemes to composed Hangul syllables.
Decomposed Jamo phonemes look like "ㅎㅏ-ㄴㄱㅡ-ㄹㄹㅏㅇㅣㅈㅡ". A Jaeum after a hyphen ("-ㄴ") means that it is a Jongseong (tail).
Example (Interpolation) ¶
fmt.Println(ComposeHangul("ㅗㅈ"))
Output: 오즈
Example (Perfect) ¶
fmt.Println(ComposeHangul("ㅎㅏ-ㄴㄱㅡ-ㄹㄹㅏㅇㅣㅈㅡ"))
Output: 한글라이즈
func Hangulize ¶
Hangulize is the most simple and useful API of thie package. It transcribes a non-Korean word into Hangul, which is the Korean alphabet. For example, it will transcribe "Владивосто́к" in Russian into "블라디보스토크".
Example (Gloria) ¶
fmt.Println(Hangulize("ita", "gloria"))
Output: 글로리아
Example (Nietzsche) ¶
fmt.Println(Hangulize("deu", "Friedrich Wilhelm Nietzsche"))
Output: 프리드리히 빌헬름 니체
Example (ShinkaiMakoto) ¶
// import "github.com/hangulize/hangulize/pronounce/furigana"
// UsePronouncer(&furigana.P)
fmt.Println(Hangulize("jpn", "新海誠"))
Output: 신카이 마코토
func ListLangs ¶
func ListLangs() []string
ListLangs returns the language name list of bundled specs. The bundled spec can be loaded by LoadSpec.
Example ¶
Here're all supported languages.
for _, lang := range ListLangs() {
fmt.Println(lang)
}
Output: aze bel bul cat ces cym deu ell epo est fin grc hbs hun isl ita jpn kat-1 kat-2 lat lav lit mkd nld pol por por-br ron rus slk slv spa sqi swe tur ukr vie wlm
func UnusePronouncer ¶ added in v0.2.0
UnusePronouncer discards an imported pronouncer.
func UsePronouncer ¶ added in v0.2.0
func UsePronouncer(p Pronouncer) bool
UsePronouncer keeps a pronouncer for ready to use.
Types ¶
type Hangulizer ¶
type Hangulizer struct {
// contains filtered or unexported fields
}
Hangulizer provides the transcription logic for the underlying spec.
func NewHangulizer ¶
func NewHangulizer(spec *Spec) *Hangulizer
NewHangulizer creates a Hangulizer for a spec.
Example ¶
spec, _ := LoadSpec("nld")
h := NewHangulizer(spec)
fmt.Println(h.Hangulize("Vincent van Gogh"))
Output: 빈센트 반고흐
func (*Hangulizer) Hangulize ¶
func (h *Hangulizer) Hangulize(word string) string
Hangulize transcribes a loanword into Hangul.
func (*Hangulizer) HangulizeTrace ¶
func (h *Hangulizer) HangulizeTrace(word string) (string, []Trace)
HangulizeTrace transcribes a loanword into Hangul and returns the traced internal events too.
type Language ¶
type Language struct {
ID string // Arbitrary, but identifiable language ID.
Codes [2]string // [0]: ISO 639-1 code, [1]: ISO 639-3 code
English string // The language name in English.
Korean string // The language name in Korean.
Script string
Pronounce string
}
Language identifies a natural language.
type Pattern ¶
type Pattern struct {
// contains filtered or unexported fields
}
Pattern represents an HRE (Hangulize-specific Regular Expression) pattern.
The transcription logic includes several rewriting rules. A rule has a Pattern and an RPattern. A sub-word which is matched with the Pattern, will be rewritten by the RPattern.
rewrite:
"'" -> ""
"^gli$" -> "li"
"^glia$" -> "g.lia"
"^glioma$" -> "g.lioma"
"^gli{@}" -> "li"
"{@}gli" -> "li"
"gn{@}" -> "nJ"
"gn" -> "n"
Some expressions in Pattern have special meaning:
"^" // start of chunk
"^^" // start of string
"$" // end of chunk
"$$" // end of string
"{...}" // zero-width match
"{~...}" // zero-width negative match
"{}" // zero-width space
"<var>" // one of var values (defined in spec)
func (*Pattern) Explain ¶
Explain shows the HRE expression with the underlying standard regexp patterns.
func (*Pattern) Find ¶
Find searches up to n matches in the word. If n is -1, it will search all matches. The result is an array of submatch locations.
Example ¶
p, _ := newPattern("^he(l+o){,}", nil, nil)
fmt.Println(p.Find("hello, helo, hellllo", -1))
Output: [[0 5 2 5] [7 11 9 11]]
type Pronouncer ¶ added in v0.2.0
Pronouncer is an interface to guess pronunciation from spelling based on lexical analysis.
The lexical analysis may require large size of dictionary data. To keep Hangulize lightweight, pronouncers are implemented out of this package.
For example, there is the pronouncer for Furigana of Japanese in a separate package.
import "github.com/hangulize/hangulize"
import "github.com/hangulize/hangulize/pronounce/furigana"
hangulize.UsePronouncer(&furigana.P)
fmt.Println(hangulize.Hangulize("jpn", "日本語"))
func GetPronouncer ¶ added in v0.2.0
func GetPronouncer(id string) (Pronouncer, bool)
GetPronouncer returns the imported pronouncer by the ID.
type RPattern ¶
type RPattern struct {
// contains filtered or unexported fields
}
RPattern is a dynamic replacement pattern.
Some expressions in RPattern have special meaning:
"{}" // zero-width space
"<var>" // ...
"R" in the name means "replacement" or "right-side".
func (*RPattern) Interpolate ¶
Interpolate determines the final replacement based on the matched Pattern.
type Spec ¶
type Spec struct {
// Meta information sections
Lang Language
Config Config
// Helper setting sections
Macros map[string]string
Vars map[string][]string
Normalize map[string][]string
// Rewrite/Transcribe
Rewrite []*Rule
Transcribe []*Rule
// Test examples
Test [][2]string
// Source code
Source string
// contains filtered or unexported fields
}
Spec represents a transactiption specification for a language.
func LoadSpec ¶
LoadSpec finds a bundled spec by the given language name. Once it loads a spec, it will cache the spec.
