वीर

19 เมษายน 2008

template สำหรับเริ่มเขียน Python

Filed under: Uncategorized — वीर @ 10:27 am
Tags: , , , ,

ผมมักจะมีปัญหาเรื่อง encoding บ่อยๆ เวลาผมจะเขียน Python เลยมักจะเริ่มด้วย code แบบข้างล่าง


# template
import sys

def main():
    reload(sys)
    sys.setdefaultencoding('utf-8') 

if __name__ == '__main__':
    main()

ขอบคณคุณ cwt

13 มีนาคม 2008

มีงาน Perl/Python อีกแล้ว

Filed under: Uncategorized — वीर @ 4:10 am
Tags: , , ,

หลายๆคนมักจะถามว่าเขียน Perl/Python แล้วจะมีงานทำหรือเปล่า? (ผมก็ไม่รู้หรอก) แต่เห็นป้ายรับสมัครมาก็เลยเอามาให้ดู

00030.jpg

ป.ล. แต่ก็มี Java ด้วย

4 มีนาคม 2008

มีประกาศรับคนเขียน Python และ Ruby ด้วย

Filed under: Uncategorized — वीर @ 11:54 am
Tags: , , , , ,

มีประกาศรับคนเขียน Python และ Ruby ด้วย เห็นป้ายมา

xsidekick2.jpg

แล้วก็ตามไปดูเว็บ

xsidekick.png

แล้วก็ไม่ได้เข้าใจอะไร :-P

3 มีนาคม 2008

ตัดคำโดยใช้ Python และ ICU

Filed under: Uncategorized — वीर @ 3:14 pm
Tags: , , , , ,

ผมดูตัวอย่างมาจาก 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 ไว้อยู่แล้วนะครับ)

20 กุมภาพันธ์ 2008

KUCut - โปรแกรมตัดคำ ที่เขียนด้วย Python

Filed under: Uncategorized — वीर @ 10:49 am
Tags: , , , ,

หลายคนแม้แต่ในหน่วยวิจัยเดียวกันมักจะถามผมว่าใช้โปรแกรมตัดคำตัวไหนอยู่. โปรแกรมตัดคำตัวที่ผมใช้เป็นหลักชื่อว่า KUCut จาก NAiST @KUมี license แบบ GPL และ download ได้จาก http://naist.cpe.ku.ac.th/pkg/kucut-1.2.2_python25_fix.zip. โปรแกรมตัดคำตัวนี้เขียนด้วยภาษา Python. หลักการที่แปลกจากตัวอื่นคือใช้เทคนิค unsupervised machine learning ด้วย อ่านเพิ่มได้จาก “การตัดคําภาษาไทยดวยเทคนิคการเรียนรูแบบไมใชตัวอยาง”.

ส่วน Python sdk และ runtime (ภาษาเรียกแบบ Java :-P) สามารถ download ได้จาก http://www.python.org/download/

17 กุมภาพันธ์ 2008

Python: file object กับ file descriptor

พอดีมีคนถามที่ codenone (http://www.codenone.com/node/687 ว่า file object ของ Python ต่างจาก file descriptor อย่างไร. ผมพยายามจะตอบจากการไปอ่าน code ของ Python 2.5.1 ก็ได้คำตอบมาแบบนี้.

เราสามารถขอ file descriptor ซึ่งเป็นเลขจำนวนเต็มธรรมดาๆ จาก file object ได้โดยใช้ method ชื่อ fileno() ตามตัวอย่างด้านล่าง

fp = open("foo.txt", "a+")
print fp.fileno()

เลขที่ print ออกมาคือ file descriptor. file object ที่หุ้ม file descriptor (fd; ต่อไปขอย่อว่า fd นะ) ไว้อีกที.จริงๆแล้ว file object ของ python ก็ไม่ได้ไปเก็บ fd ไว้ตรงๆ. แต่เก็บ FILE (ของ libc) ไว้. และใน FILE ก็มี fd อีกที.มันเป็น layer ประมาณนี้ (Python)File object -> FILE (libc) -> File descriptor (unix)

ถ้าเราสั่ง open(”foo.txt”) ใน Python. ข้างในมันก็จะไปเรียก

  1. fopen(”foo.txt”, “r”) ในภาษาซีให้อีก.
  2. แล้วใน fopen ก็ไปเรียก open(”foo.txt”, flags, mode); ของ unix อีกที (มั้ง ไม่ได้ check code ดู แค่เดาๆ เอา).
  3. แล้ว open ของ unix นี่หละ return fd ออกมา.
  4. พอ fopen ได้ fd มาก็จะเอาไปเก็บไว้ แล้ว return file pointer (FILE *) ออกมา ซึ่งข้างในเก็บ fd ไว้.
  5. open ของ Python ก็ return file object ที่เก็บ file pointer (FILE *) ไว้อีกที.

ผมดูแต่ code ของ Python. ไม่ได้ดูใน code ของ libc เท่าไหร่ อาจจะมีที่ผิดๆมั่วอยู่ได้ อย่างไรก็ตามก็น่าจะพอทำให้เข้าใจคร่าวๆ

ป.ล. สำหรับท่าใส่ source code ใน wordpress เอามาจาก http://wuhrr.wordpress.com/2007/11/09/colorize-your-code-for-free-online/ ขอบใจหลายๆ. ต้อง credit Google ด้วยมั้ง เพราะไม่มี google ก็คงหาไม่เจอ แต่ถ้าไม่มี Internet ก็ไม่มี Google นะ …. พอก่อนละกัน.

ป.ล. (2) แหมอยากจะทำ animation ให้ดูเลย แต่สงสัยเว่อร์ไป

15 ตุลาคม 2007

Junit4 @expect in Python

Filed under: Uncategorized — वीर @ 1:22 pm
Tags: , , , , , , ,

เวลาจะ test ว่าอยากให้ function ที่เราเรียนมัน raise exception  ตามที่เราต้องการหรือเปล่า ถ้าเป็น JUnit เราก็ใส่อะไรประมาณข้างล่างได้ (แค่ประมาณอาจจะเขียนไม่ถูก)

@expect(IOException)

void testFoo() {

….

}

แต่ว่าพอมาเป็น Python จะทำอย่างไร จากคำแนะนำจากคุณสุกรีที่ codenone ก็ได้แบบนี้ออกมา

def expect(expr):
    def decorator(f):
        def wrapper(*__args, **__kw):
            try:
                f()
                assert False
            except Exception, (e):
                assert type(e) != type(expr)

        return wrapper
    return decorator

@expect(RuntimeError)
def func1():
    raise Exception

func1()

เขียนแยกๆ ดิบดี พอเอาไปใช้กับ nose จริงๆ เจ๊ง -_-!.

หน้าต่อไป »

บลอกที่ WordPress.com .