组合或构造韩文字母

huangapple go评论87阅读模式
英文:

Combine or construct korean letters

问题

有没有办法将这些韩文辅音和元音组合成完整的字符呢?

例如,如果我有:

  ㄱㅏㅁㅅㅏㅎㅏㅂㄴㅣㄷㅏ

它应该是:

  감사합니다

有没有办法做到这一点?
我只能想到暴力破解每种情况,这样会有很多很多的情况要考虑。

以下是我的尝试。不是最优的,而且耗时太长。

if strings.Contains(input_str, "ㅏㄴㅈ") {
    input_str = strings.Replace(input_str, "ㅇㅏㄴㅈ", "앉", -1)
}

if strings.Contains(input_str, "ㅏㄹㅂ") {
    input_str = strings.Replace(input_str, "ㅂㅏㄹㅂ", "밟", -1)
}

if strings.Contains(input_str, "ㅏㅂㅅ") {
    input_str = strings.Replace(input_str, "ㄱㅏㅂㅅ", "값", -1)
}
英文:

Is there anyway that I can combine this Korean consonants and vowels into a complete character.

For example if I have

  ㄱㅏㅁㅅㅏㅎㅏㅂㄴㅣㄷㅏ

It would be

  감사합니다

Is there any way to do this?
I can only think of bruteforcing every case that would have to count many many cases.

Below is my try. Not optimal and take too much time.

if strings.Contains(input_str, "ㅏㄴㅈ") {
	input_str = strings.Replace(input_str, "ㅇㅏㄴㅈ", "앉", -1)
}

if strings.Contains(input_str, "ㅏㄹㅂ") {
	input_str = strings.Replace(input_str, "ㅂㅏㄹㅂ", "밟", -1)
}

if strings.Contains(input_str, "ㅏㅂㅅ") {
	input_str = strings.Replace(input_str, "ㄱㅏㅂㅅ", "값", -1)
}

答案1

得分: 7

你想要的是Unicode规范化来组合韩文字母。
Go语言支持这一点,但(目前)标准库中还没有。请参阅相关问题

要导入外部的go.text/unicode/norm包,请使用:

go get -u golang.org/x/text/unicode/norm

你可能想使用NFC,它执行以下操作:

  • 规范分解
  • 紧接着进行规范组合(这是你想要的)

NFD(分解)的示例:

// 앉 -> 앉
fmt.Println(string(norm.NFD.AppendString(nil, "앉")))

NFC(组合)的示例:

// 앉 -> 앉
fmt.Println(string(norm.NFC.AppendString(nil, "앉")))
英文:

What you want is Unicode normalization to compose hangul jamo.
Go supports this but not (yet) in the standard library. See this related issue.

To import the external go.text/unicode/norm package use:

go get -u golang.org/x/text/unicode/norm

You probably want to use NFC which does a

  • Canonical Decomposition,
  • followed by Canonical Composition (which is what you want)

Example for NFD (decomposition):

// 앉 -> 앉
fmt.Println( string( norm.NFD.AppendString(nil, "앉") ) )

Example for NFC (composition):

// 앉 -> 앉
fmt.Println( string( norm.NFC.AppendString(nil, "앉") ) )

答案2

得分: 2

最近有人对韩文中的Unicode进行了撰写:http://www.programminginkorean.com/programming/hangul-in-unicode/composing-syllables-in-unicode/

英文:

Someone recently did a write-up on unicode in Korean: http://www.programminginkorean.com/programming/hangul-in-unicode/composing-syllables-in-unicode/

huangapple
  • 本文由 发表于 2013年11月6日 22:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/19814497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定