Skip to content

Instantly share code, notes, and snippets.

@dehypnosis
Created November 24, 2018 10:57
Show Gist options
  • Save dehypnosis/60448cfb273232b84c139161eb4ba506 to your computer and use it in GitHub Desktop.
Save dehypnosis/60448cfb273232b84c139161eb4ba506 to your computer and use it in GitHub Desktop.
golang 한국어 조사 치환
// 은/는, 을/를, 이/가, 과/와 조사 치환 (마지막 글자의 종성이 있는 경우 첫째 조사, 종성이 없는 경우 둘째 조사 채택)
var josas = map[string][]string{
"은/는": { "은", "는" },
"을/를": { "을", "를" },
"이/가": { "이", "가" },
"과/와": { "과", "와" },
"는/은": { "은", "는" },
"를/을": { "을", "를" },
"가/이": { "이", "가" },
"와/과": { "과", "와" },
}
func replaceJosa(s string) string {
for {
var (
index int
foundJosa string
)
for josa, _ := range josas {
index = strings.Index(s, josa)
if index != -1 {
foundJosa = josa
break
}
}
if index == -1 {
break
}
lastCharacter, _ := utf8.DecodeLastRuneInString(s[:index])
replacementIndex := 1
if (lastCharacter - 0xac00) % 28 > 0 { // 종성이 있는 경우?
replacementIndex = 0
}
s = strings.Replace(s, foundJosa, josas[foundJosa][replacementIndex], 1)
}
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment