<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>polib &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://wordpress.com/tag/polib/</link>
	<description>Feed of posts on WordPress.com tagged "polib"</description>
	<pubDate>Sun, 12 Oct 2008 23:37:24 +0000</pubDate>

	<generator>http://wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[เตรียม parallel corpus สำหรับ word alignment จาก .po]]></title>
<link>http://openil.wordpress.com/?p=145</link>
<pubDate>Tue, 04 Mar 2008 10:08:08 +0000</pubDate>
<dc:creator>वीर</dc:creator>
<guid>http://blog.vee-u.com/2008/03/04/po_giza/</guid>
<description><![CDATA[วิธีที่ก็ใช้ polib อ่าน .po ของ GNOME มาแล้วก็]]></description>
<content:encoded><![CDATA[<p>วิธีที่ก็ใช้ polib อ่าน .po ของ GNOME มาแล้วก็พยายาม ตัดเครื่องหมายทิ้ง. แม้แต่ string ที่มีหลายบรรทัดก็ตัดทิ้ง.</p>
<p>ขั้นแรกเลยคือเข้าไปที่ web L10N ของ GNOME <a href="http://l10n.gnome.org/languages/th/gnome-2-22">http://l10n.gnome.org/languages/th/gnome-2-22</a> หลังจาก copy &#38; paste และแก้ด้วยความถึกนิดหน่อย ผมก็ได้รายการของ .po มา.</p>
<p>Download po file pessulus       100% (30/0/0)<br />
Download po file Sabayon        100% (230/0/0)<br />
GNOME developer platform (66% translated)<br />
Download po file atk    95% (117/0/6)<br />
Download po file gail   100% (103/0/0)<br />
...</p>
<p>...</p>
<p>พอได้แบบนี้มาแล้วก็เขียนโปรแกรมมาตัดๆ หน่อย</p>
<pre>get_pkg.rb
[sourcecode language='ruby']

while gets
    if $_ =~ /Download po file ([^s]+)/
        print "wget  http://l10n.gnome.org/POT/#{$1}.HEAD/#{$1}.HEAD.th.pon"
    end
end[/sourcecode]
ก็ได้ script ที่ load .po ออกมา

$ ruby get_pkg.rb &#62; download.sh &#38;&#38; sh download.sh

พอได้แบบนี้มาแล้วผมก็เขียนโปรแกรมมา extract ของใน .po มาชื่อ ext_po.py

[sourcecode language='python']

#-*- coding: UTF-8 -*-
import sys
import polib
import getopt
import re

class Params:
    def __init__(self, o_eng_path, o_tha_path, i_po_paths):
        self.o_eng_path = o_eng_path
        self.o_tha_path = o_tha_path
        self.i_po_paths = i_po_paths

    def __str__(self):
        return str(self.__dict__)

def usage():
    print "Usage: " + sys.argv[0] + " -e  -t   ..."

def usage_and_exit():
    usage()
    sys.exit(2)

def get_params():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "e:t:")
        keys = [o[0] for o in opts]
        if not "-e" in keys:
            print "Require English output filename"
            usage_and_exit()
        if not "-e" in keys:
            print "Require Thai output filename"
            usage_and_exit()
        if len(args) < 1:
            print "Require po filename"
            usage_and_exit()
        return Params(o_eng_path = filter(lambda o: o[0] == "-e", opts)[0][1],
                      o_tha_path = filter(lambda o: o[0] == "-t", opts)[0][1],
                      i_po_paths = args)
    except getopt.GetoptError, err:
        print str(err)
        usage_and_exit()

def entry_constraints(entry):
    return not entry.translated() and entry.msgstr != ""
        and entry.msgid != ""

def remove(txt, sym):
    return re.sub(sym, " ", txt)

def convert_text(txt):
    syms = ["_", "...", ":", "&#124;", "-+", "/+", "%w", "",
            """, "©", "~", "(", ")"]
    return reduce(remove, syms, txt)

def split_line(txt):
    return filter(lambda tok: not re.match("^ *$", tok),
                  re.split("n", txt))

def split_line_in_entry(ans, entry):
    e_lines = split_line(entry[0])
    t_lines = split_line(entry[1])
    if len(e_lines) == 1 and len(t_lines) == 1:
        return ans + [(e_lines[i], t_lines[i]) for i in range(len(e_lines))]
    else:
        return ans

def ext_po_file(o_eng_file, o_tha_file, po):
    entries = filter(entry_constraints, list(po))
    entries = [(entry.msgid, entry.msgstr) for entry in entries]
    entries = [(convert_text(e[0]), convert_text(e[1])) for e in entries]
    entries = reduce(split_line_in_entry, entries, [])
    for entry in entries:
        o_eng_file.write(entry[0] + "n")
        o_tha_file.write(entry[1] + "n")

def ext_with_ofiles(o_eng_file, o_tha_file, i_po_paths):
    for i_po_path in i_po_paths:
        po = polib.pofile(i_po_path)
        ext_po_file(o_eng_file, o_tha_file, po)    

def ext(o_eng_path, o_tha_path, i_po_paths):
    o_tha_file = open(o_tha_path, 'w')
    o_eng_file = open(o_eng_path, 'w')
    ext_with_ofiles(o_eng_file, o_tha_file, i_po_paths)
    o_eng_file.close()
    o_tha_file.close()

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

def main():
    init_charset()
    params = get_params()
    ext(params.o_eng_path, params.o_tha_path, params.i_po_paths)

if __name__ == '__main__':
	main()
[/sourcecode]</pre>
<p>จากนั้นก็สั่ง (โดยสมมุติว่า .po ทั้งหมดอยู่ใน folder เดียวกัน)</p>
<p>$ python ext_po.py -e e -t t  *.po</p>
<p>cut.sh เอาไว้ตัดคำโดยใช้  <a href="http://blog.vee-u.com/2008/02/20/recent-postsuser-loginlog-in-using-openidwhat-is-openidusername-password-create-new-account-request-new-passwordlog-in-using-openidcancel-openid-login/">kucut</a> อีกที</p>
<p>#!/bin/sh<br />
iconv -f UTF-8 -t TIS-620 &#60; $1 &#62; $1.tis<br />
kucut --line=" " $1.tis<br />
iconv -f TIS-620 -t UTF-8 &#60; $1.tis.cut &#62; $1.cut</p>
<p>แล้วก็ใช้ cut.sh โดยเรียก</p>
<p>$ ./cut.sh t &#38;&#38; mv t.cut t</p>
<p>จากนั้นก็ไปเรียก <a href="blog.vee-u.com/2008/03/02/giza_pp">GIZA++ แบบที่แก้ๆ</a>ไปแล้ว ได้เลย</p>
<p>$ plain2snt e t</p>
<p>$ train-giza++ e.vcb t.vcb e_t.snt</p>
<p>เป็นอันเรียบร้อย</p>
<p>ได้ผลออกมาแบบนี้</p>
<p><i># Sentence pair (1) source length 6 target length 6 alignment score : 0.000163118<br />
รายการ เมนู ใหม่ ต้อง มี ชื่อ<br />
NULL ({ }) New ({ 3 }) menu ({ 2 }) items ({ 1 }) need ({ 4 5 }) a ({ }) name ({ 6 })<br />
# Sentence pair (2) source length 5 target length 5 alignment score : 0.00022357<br />
เมนู ใหม่ ต้อง มี ชื่อ  </i></p>
<p>ผลอ่ายากนิดนึงแบบ New ({3}) หมายถึงว่า new ตรงกับคำภาษาไทยตัวที่ 3 ใน "รายการ เมนู ใหม่ ต้อง มี ชื่อ" ก็คือใหม่นั่นเอง.</pre>
]]></content:encoded>
</item>
<item>
<title><![CDATA[แม้แต่ document ของ GNOME ก็ใช้ gettext]]></title>
<link>http://openil.wordpress.com/?p=140</link>
<pubDate>Sun, 02 Mar 2008 17:02:15 +0000</pubDate>
<dc:creator>वीर</dc:creator>
<guid>http://blog.vee-u.com/2008/03/02/gettext_gnome_doc/</guid>
<description><![CDATA[ผมลองเข้าไปดู l10.gnome.org. เห็นมี status ของ document ]]></description>
<content:encoded><![CDATA[<p>ผมลองเข้าไปดู<a href="http://l10.gnome.org"> l10.gnome.org</a>. เห็นมี status ของ document ด้วย. ก็สงสัยว่า document นี่มันยาวๆ อาจจะมี format อื่นๆ ที่ไม่ได้ใช้ gettext แต่ดูเข้าจริงๆ ก็ gettext นี่หละ. แต่ว่าไม่ได้เป็น gettext ที่ติดกับไฟล์ .c แต่เป็น .xml แทน.</p>
<p>อันนี้เป็นตัวอย่าง <a href="http://l10n.gnome.org/POT/gedit.HEAD/docs/help.HEAD.zh_CN.po">http://l10n.gnome.org/POT/gedit.HEAD/docs/help.HEAD.zh_CN.po</a></p>
<p>ก็เลยตกลงปลงใจว่าต้องทำอะไรเล่นกับ .po บ้าง แล้วเอา <a href="http://code.google.com/p/polib/">polib</a> มาเล่นก็ง่าย + สนุกดี.</p>
]]></content:encoded>
</item>

</channel>
</rss>
