pyicu

python: ตัวอย่างการใช้งาน PyICU

Tags:

ดัดแปลงโค๊ดมาจาก programming is hard: isThai and ThaiWarp function

import PyICU

def isThai(chr):
    cVal = ord(chr)
    if(cVal >= 3584 and cVal <= 3711):
        return True

    return False

def wrap(txt):
  txt = PyICU.UnicodeString(txt)
  bd = PyICU.BreakIterator.createWordInstance(PyICU.Locale("th"))
  bd.setText(txt)   
  lastPos = bd.first()
  retTxt = PyICU.UnicodeString("")
  txt_list = []
  try:
    while(1):
      currentPos = bd.next()
      retTxt += txt[lastPos:currentPos]
      #
      txt_list.append(txt[lastPos:currentPos])
      #Only thai language evaluated
      if(isThai(txt[currentPos-1])):
        if(currentPos < len(txt)):
          if(isThai(txt[currentPos])):
            #This is dummy word seperator   
            #retTxt += PyICU.UnicodeString("|||")
            #
            pass
 

debian: ติดตั้ง PyICU

Tags:

บันทึกติดตั้ง PyICU บนเดเบียน Etch ใช้ Python รุ่น 2.4

จะลองเอามาทำตัวตัดคำไทย เพื่อจัดเก็บคำในฐานข้อมูล
ครั้งแรกที่ทดลอง ได้นำเอาไบนารีมาติดตั้ง แต่รันไม่ได้เพราะเกิดข้อผิดพลาดเกี่ยวกับยูนิโค๊ด จึงต้องเอาซอร์สมาคอมไพล์เอง

ติดตั้งแพกเกจที่จำเป็น
# aptitude install python2.4-dev libicu36 libicu36-dev

ดาวน์โหลดซอร์ส และแตกไฟล์
# cd /usr/src
# wget http://downloads.osafoundation.org/PyICU/src/PyICU-src-0.6.tar.gz

 
Syndicate content