ผมดูตัวอย่างมาจาก http://www.thaitux.info/node/194. ผมเลยมาเขียน version ของผมบ้าง เพื่อที่จะเอามาเตรียม corpus สำหรับ bilingual word matching (alignment).
#-*- coding: UTF-8 -*-
import sys
import PyICU
# split_words was inspired by an example in http://www.thaitux.info/node/194
def is_tha(c):
return ord(c) >= 0x0E00 and ord(c) <= 0x0E7F
def merge(ans, tok):
if is_tha(tok[0]) or len(ans[:-1]) == 0 or is_tha(ans[-1][0]):
return ans + [tok]
else:
return ans[:-1] + [ans[-1] + tok]
def split_words(txt):
icu_txt = PyICU.UnicodeString(txt)
brk_iter = PyICU.BreakIterator.createWordInstance(PyICU.Locale("th"))
brk_iter.setText(icu_txt)
brk_e = [i for i in brk_iter]
brk_s = [0] + brk_e[:-1]
ans = [unicode(icu_txt[i[0]:i[1]]) for i in zip(brk_s, brk_e)]
return filter(lambda t: t != "", reduce(merge, ans, []))
def main():
reload(sys)
sys.setdefaultencoding('utf-8')
print split_words("ทดลองทดลอง 5%")
if __name__ == '__main__':
main()
ผลออกมาได้ [u'u0e17u0e14u0e25u0e2du0e07', u'u0e17u0e14u0e25u0e2du0e07', u' 5%'] ถ้า print ดีหน่อยๆ มันก้ประมาณว่า ทดลอง|ทดลอง| 5%.ก่อนจะใช้ได้ก็ลง libicu ก่อนโดยใช้ aptitude install libicu-dev แล้วตามด้วย easy_install pyicu (สมมุติว่าลง easy_install กับ python ไว้อยู่แล้วนะครับ)
ง่ะ
ความเห็น โดย bact' — 3 มีนาคม 2008 @ 16:50
?
ความเห็น โดย वीर — 3 มีนาคม 2008 @ 17:08