예제 #1
0
파일: __init__.py 프로젝트: eveem/pythainlp
    def test_tcc(self):
        self.assertEqual(tcc.tcc(None), [])
        self.assertEqual(tcc.tcc(""), [])
        self.assertEqual(tcc.tcc("ประเทศไทย"), ["ป", "ระ", "เท", "ศ", "ไท", "ย"])

        self.assertEqual(list(tcc.tcc_gen("")), [])
        self.assertEqual(tcc.tcc_pos(""), set())
예제 #2
0
    def test_tcc(self):
        self.assertEqual(tcc.segment(None), [])
        self.assertEqual(tcc.segment(""), [])
        self.assertEqual(tcc.segment("ประเทศไทย"), ["ป", "ระ", "เท", "ศ", "ไท", "ย"])

        self.assertEqual(list(tcc.tcc("")), [])
        self.assertEqual(tcc.tcc_pos(""), set())
예제 #3
0
def file_trie(data):
    '''
	ใช้สร้างไฟล์ข้อมูลสำหรับระบบที่ใช้ trie
	'''
    path = os.path.join(
        os.path.expanduser("~"),
        'pythainlp-data')  #os.path.join(, 'pthainlp_trie.data')
    if not os.path.exists(path):
        os.makedirs(path)
    if data == "newmm":
        path = os.path.join(path, 'pythainlp_trie-tcc1.data')
    elif data == "old":
        path = os.path.join(path, 'pythainlp_trie2.data')
    else:
        path = os.path.join(path, 'pythainlp_trie2.data')
    if not os.path.exists(path):
        #ถ้าไม่มีไฟล์
        if data == "newmm":
            from pythainlp.corpus.thaiword import get_data  # ข้อมูลเก่า
            data2 = get_data()
            i = 0
            while i < len(data2):
                data2[i] = tcc.tcc(data2[i], sep='#')
                if (data2[len(data2[i]) - 1] != "#"):
                    data2[i] += "#"
                i += 1
            data = data2
        elif data == 'old':
            from pythainlp.corpus.thaiword import get_data  # ข้อมูลเก่า
            data = get_data()
        else:
            from pythainlp.corpus.newthaiword import get_data  # ข้อมูลใหม่
            data = get_data()
        with open(path, 'wb') as dill_file:
            dill.dump(marisa_trie.Trie(data), dill_file)
        dill_file.close()
    with open(path, 'rb') as dill_file:
        data = dill.load(dill_file)
    dill_file.close()
    return data
예제 #4
0
def file_trie(data):
	'''
	ใช้สร้างไฟล์ข้อมูลสำหรับระบบที่ใช้ trie
	'''
	path = get_path_pythainlp_data()
	if not os.path.exists(path):
		os.makedirs(path)
	if data=="newmm":
		path = os.path.join(path, 'pythainlp_trie-tcc1.data')
	elif data=="old":
		path = os.path.join(path, 'pythainlp_trie2.data')
	else:
		path = os.path.join(path, 'pythainlp_trie2.data')
	if not os.path.exists(path):
		#ถ้าไม่มีไฟล์
		if data=="newmm":
			from pythainlp.corpus.thaiword import get_data # ข้อมูลเก่า
			data2=get_data()
			i=0
			while i<len(data2):
				data2[i]=tcc.tcc(data2[i],sep='#')
				if(data2[len(data2[i])-1]!="#"):
					data2[i]+="#"
				i+=1
			data=data2
		elif data=='old':
			from pythainlp.corpus.thaiword import get_data # ข้อมูลเก่า
			data=get_data()
		else:
			from pythainlp.corpus.newthaiword import get_data # ข้อมูลใหม่
			data=get_data()
		with open(path,'wb') as dill_file:
			dill.dump(marisa_trie.Trie(data),dill_file)
		dill_file.close()
	with open(path,'rb') as dill_file:
		data=dill.load(dill_file)
	dill_file.close()
	return data
예제 #5
0
def cut(word):
    return tcc(word, sep="ii/ii").split('ii/ii')
예제 #6
0
def tcc_api():
    txt = request.args.get('sent', 0, type=str)
    res = "~".join(tcc.tcc(txt.strip()))
    return jsonify(result=res)
예제 #7
0
def romanization(text):
    text = deletetone(text)
    text1 = word_tokenize(text, engine='mm')
    textdata = []
    #print(text1)
    for text in text1:
        #a1=etcc.etcc(text)
        a2 = tcc.tcc(text)
        text = re.sub('//', '/', a2)
        if re.search(u'เ[\w]' + 'ี' + 'ย/ว', text, re.U):
            '''
            จัดการกับ เอียว
            '''
            #print('เอียว')
            search = re.findall(u'เ[\w]' + 'ี' + 'ย/ว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'iao', text, flags=re.U)

        if re.search(u'แ[\w]' + '็' + 'ว', text, re.U):
            '''
            จัดการกับ แอ็ว
            '''
            #print('แอ็ว')
            search = re.findall(u'แ[\w]' + '็' + 'ว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'aeo', text, flags=re.U)
        if re.search(u'แ[\w]/[\w]' + '็/' + 'ว', text, re.U):
            '''
            จัดการกับ แออ็ว
            '''
            #print('แออ็ว')
            search = re.findall(u'แ[\w]/[\w]' + '็/' + 'ว', text, re.U)
            for i in search:
                text = re.sub(i,
                              list(i)[1] + list(i)[3] + 'aeo',
                              text,
                              flags=re.U)
        if re.search(u'แ[\w]/' + 'ว', text, re.U):
            '''
            จัดการกับ แอว
            '''
            #print('แอว')
            search = re.findall(u'แ[\w]/' + 'ว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'aeo', text, flags=re.U)
        if re.search(u'เ[\w]/ว', text, re.U):
            '''
            จัดการกับ เอว
            '''
            #print('เอว')
            search = re.findall(u'เ[\w]/ว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'eo', text, flags=re.U)
        if re.search(u'เ[\w]็ว', text, re.U):
            '''
            จัดการกับ เอ็ว
            '''
            #print('เอ็ว')
            search = re.findall(u'เ[\w]็ว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'eo', text, flags=re.U)
        if re.search(u'เ[\w]ียะ', text, re.U):
            '''
            จัดการกับ เอียะ
            '''
            #print('เอียะ')
            search = re.findall(u'เ[\w]ียะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ia', text, flags=re.U)
        if re.search(u'เ[\w]ีย', text, re.U):
            '''
            จัดการกับ เอีย (1)
            '''
            #print('เอีย 1')
            search = re.findall(u'เ[\w]ีย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ia', text, flags=re.U)
        if re.search(u'เ[\w]/ีย', text, re.U):
            '''
            จัดการกับ เอีย (2)
            '''
            #print('เอีย 2')
            search = re.findall(u'เ[\w]/ีย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ia', text, flags=re.U)
        if re.search(u'เ[\w]ือ/ย', text, re.U):
            '''
            จัดการกับ เอือย
            '''
            #print('เอือย')
            search = re.findall(u'เ[\w]ือ/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ueai', text, flags=re.U)
        if re.search(u'เ[\w]ือะ', text, re.U):
            '''
            จัดการกับ เอือะ
            '''
            #print('เอือะ')
            search = re.findall(u'เ[\w]ือะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'uea', text, flags=re.U)
        if re.search(u'เ[\w]ือ', text, re.U):
            '''
            จัดการกับ เอือ
            '''
            #print('เอือ')
            search = re.findall(u'เ[\w]ือ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'uea', text, flags=re.U)
        if re.search(u'โ[\w]/ย', text, re.U):
            '''
            จัดการกับ โอย
            '''
            #print('โอย')
            search = re.findall(u'โ[\w]/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'oi', text, flags=re.U)
        if re.search(u'[\w]/อ/ย', text, re.U):
            '''
            จัดการกับ ออย
            '''
            #print('ออย')
            search = re.findall(u'[\w]/อ/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'oi', text, flags=re.U)
        if re.search(u'โ[\w]ะ', text, re.U):
            '''
            จัดการกับ โอะ
            '''
            #print('โอะ')
            search = re.findall(u'โ[\w]ะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'o', text, flags=re.U)
        if re.search(u'โ[\w]', text, re.U):
            '''
            จัดการกับ โอ
            '''
            #print('โอ')
            search = re.findall(u'โ[\w]', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'o', text, flags=re.U)
        if re.search(u'เ/[\w]า/ะ/', text, re.U):
            '''
            จัดการกับ เอาะ (1)
            '''
            #print('เอาะ 1')
            search = re.findall(u'เ/[\w]า/ะ/', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[2] + 'o', text, flags=re.U)
        if re.search(u'เ[\w]าะ', text, re.U):
            '''
            จัดการกับ เอาะ (2)
            '''
            #print('เอาะ 2')
            search = re.findall(u'เ[\w]าะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'o', text, flags=re.U)
        if re.search(u'อำ', text, re.U):
            '''
            จัดการกับ อำ
            '''
            #print('อำ')
            search = re.findall(u'อำ', text, re.U)
            for i in search:
                text = re.sub(i, 'am', text, flags=re.U)
        if re.search(u'อี', text, re.U):
            '''
            จัดการกับ อี
            '''
            #print('"อี"')
            search = re.findall(u'อี', text, re.U)
            for i in search:
                text = re.sub(i, 'i', text, flags=re.U)
        # เออ
        if re.search(u'เ[\w]/อ', text, re.U):
            '''
            จัดการกับ เออ
            '''
            #print('เออ')
            search = re.findall(u'เ[\w]/อ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'oe', text, flags=re.U)
        if re.search(u'[\w]/อ', text, re.U):
            '''
            จัดการกับ ออ
            '''
            #print('ออ')
            search = re.findall(u'[\w]/อ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'o', text, flags=re.U)
        if re.search(u'[\w]ัวะ', text, re.U):
            '''
            จัดการกับ อัวะ
            '''
            #print('อัวะ')
            search = re.findall(u'[\w]ัวะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ua', text, flags=re.U)
        if re.search(u'[\w]ัว', text, re.U):
            '''
            จัดการกับ อัว
            '''
            #print('อัว')
            search = re.findall(u'[\w]ัว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ua', text, flags=re.U)
        # ใอ,อัย , อาย
        if re.search(u'ใ[\w]', text, re.U):
            '''
            จัดการกับ ใอ
            '''
            #print('ใอ')
            search = re.findall(u'ใ[\w]', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ai', text, flags=re.U)
        if re.search(u'[\w]ัย', text, re.U):
            '''
            จัดการกับ อัย
            '''
            #print('อัย')
            search = re.findall(u'[\w]ัย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ai', text, flags=re.U)
        if re.search(u'[\w]า/ย', text, re.U):
            '''
            จัดการกับ อาย
            '''
            #print('อาย')
            search = re.findall(u'[\w]า/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ai', text, flags=re.U)
        #เอา, อาว
        if re.search(u'เ[\w]า', text, re.U):
            '''
            จัดการกับ เอา
            '''
            #print('เอา')
            search = re.findall(u'เ[\w]า', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ao', text, flags=re.U)
        if re.search(u'[\w]า/ว', text, re.U):
            '''
            จัดการกับ อาว
            '''
            #print('อาว')
            search = re.findall(u'[\w]า/ว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ao', text, flags=re.U)
        #อุย
        if re.search(u'[\w]ุ/ย', text, re.U):
            '''
            จัดการกับ อุย
            '''
            #print('อุย')
            search = re.findall(u'[\w]ุ/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ui', text, flags=re.U)
        #เอย
        if re.search(u'เ[\w]/ย', text, re.U):
            '''
            จัดการกับ เอย
            '''
            #print('เอย')
            search = re.findall(u'เ[\w]/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'oei', text, flags=re.U)
        # แอะ, แอ
        if re.search(u'แ[\w]ะ', text, re.U):
            '''
            จัดการกับ แอะ
            '''
            #print('แอะ')
            search = re.findall(u'แ[\w]ะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ae', text, flags=re.U)
        if re.search(u'แ[\w]', text, re.U):
            '''
            จัดการกับ แอ
            '''
            #print('แอ')
            search = re.findall(u'แ[\w]', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ae', text, flags=re.U)
        # เอะ
        if re.search(u'เ[\w]ะ', text, re.U):
            '''
            จัดการกับ เอะ
            '''
            #print('เอะ')
            search = re.findall(u'เ[\w]ะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'e', text, flags=re.U)
        # อิว
        if re.search(u'[\w]ิ/ว', text, re.U):
            '''
            จัดการกับ อิว
            '''
            #print('อิว')
            search = re.findall(u'[\w]ิ/ว', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'io', text, flags=re.U)
        # อวย
        if re.search(u'[\w]/ว/ย', text, re.U):
            '''
            จัดการกับ อวย
            '''
            #print('อวย')
            search = re.findall(u'[\w]/ว/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'uai', text, flags=re.U)
        # -ว-
        if re.search(u'[\w]/ว/[\w]', text, re.U):
            '''
            จัดการกับ -ว-
            '''
            #print('-ว-')
            search = re.findall(u'[\w]/ว/[\w]', text, re.U)
            for i in search:
                text = re.sub(i,
                              list(i)[0] + 'ua' + list(i)[4],
                              text,
                              flags=re.U)
        # เ–็,เอ
        if re.search(u'เ[\w]' + '็', text, re.U):
            '''
            จัดการกับ เ–็
            '''
            #print('เ–็')
            search = re.findall(u'เ[\w]' + '็', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'e', text, flags=re.U)
        if re.search(u'เ[\w]/', text, re.U):
            '''
            จัดการกับ เอ
            '''
            #print('เอ')
            search = re.findall(u'เ[\w]/', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'e', text, flags=re.U)
        #ไอย
        if re.search(u'ไ[\w]/ย', text, re.U):
            '''
            จัดการกับ ไอย
            '''
            #print('ไอย')
            search = re.findall(u'ไ[\w]/ย', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ai', text, flags=re.U)
        #ไอ
        if re.search(u'ไ[\w]', text, re.U):
            '''
            จัดการกับ ไอ
            '''
            #print('ไอ')
            search = re.findall(u'ไ[\w]', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[1] + 'ai', text, flags=re.U)
        #อะ
        if re.search(u'[\w]ะ', text, re.U):
            '''
            จัดการกับ อะ
            '''
            #print('อะ')
            search = re.findall(u'[\w]ะ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'a', text, flags=re.U)
        # –ั
        if re.search(u'[\w]ั', text, re.U):
            '''
            จัดการกับ –ั 
            '''
            #print('–ั ')
            search = re.findall(u'[\w]ั', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'a', text, flags=re.U)
        # รร
        if re.search(u'[\w]/ร/ร/[\w][^ก-ฮ]', text, re.U):
            '''
            จัดการกับ -รร-
            '''
            #print('-รร- 1')
            search = re.findall(u'[\w]/ร/ร/[\w][^ก-ฮ]', text, re.U)
            for i in search:
                text = re.sub(i,
                              list(i)[0] + 'an' + list(i)[6] + list(i)[7],
                              text,
                              flags=re.U)
        if re.search(u'[\w]/ร/ร/', text, re.U):
            '''
            จัดการกับ -รร-
            '''
            #print('-รร- 2')
            search = re.findall(u'[\w]/ร/ร/', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'a', text, flags=re.U)
        #อา
        if re.search(u'อา', text, re.U):
            '''
            จัดการกับ อา 1
            '''
            #print('อา 1')
            search = re.findall(u'อา', text, re.U)
            for i in search:
                text = re.sub(i, 'a', text, flags=re.U)
        if re.search(u'[\w]า', text, re.U):
            '''
            จัดการกับ อา 2
            '''
            #print('อา 2')
            search = re.findall(u'[\w]า', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'a', text, flags=re.U)
                #อำ
        if re.search(u'[\w]ำ', text, re.U):
            '''
            จัดการกับ อำ 1
            '''
            #print('อำ 1')
            search = re.findall(u'[\w]ำ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'am', text, flags=re.U)
        #อิ , อี
        if re.search(u'[\w]ิ', text, re.U):
            '''
            จัดการกับ อิ 
            '''
            #print('อิ')
            search = re.findall(u'[\w]ิ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'i' + '/', text, flags=re.U)
        if re.search(u'[\w]ี', text, re.U):
            '''
            จัดการกับ อี
            '''
            #print('อี')
            search = re.findall(u'[\w]ี', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'i' + '/', text, flags=re.U)
        #อึ , อื
        if re.search(u'[\w]ึ', text, re.U):
            '''
            จัดการกับ อึ
            '''
            #print('อึ')
            search = re.findall(u'[\w]ึ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ue' + '/', text, flags=re.U)
        if re.search(u'[\w]ื', text, re.U):
            '''
            จัดการกับ อื
            '''
            #print('อื')
            search = re.findall(u'[\w]ื', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'ue' + '/', text, flags=re.U)
        #อุ , อู
        if re.search(u'[\w]ุ', text, re.U):
            '''
            จัดการกับ อุ
            '''
            #print('อุ')
            search = re.findall(u'[\w]ุ', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'u' + '/', text, flags=re.U)
        if re.search(u'[\w]ู', text, re.U):
            '''
            จัดการกับ อู
            '''
            #print('อู')
            search = re.findall(u'[\w]ู', text, re.U)
            for i in search:
                text = re.sub(i, list(i)[0] + 'u' + '/', text, flags=re.U)
        if re.search(r'[^กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬฮ]', text,
                     re.U):
            '''
             ใช้ในกรณีคำนั้นมีสระด้วย จะได้เอาพยัญชนะตัวแรกไปเทียบ
            '''
            d = re.search(consonants_thai, text, re.U)
            text = re.sub(d.group(0),
                          consonants[d.group(0)][0],
                          text,
                          flags=re.U)
        listtext = list(text)
        if re.search(consonants_thai, listtext[0], re.U):
            '''
	        จัดการกับพยัญชนะต้น
	        '''
            listtext[0] = consonants[listtext[0]][0]
            two = False
            if len(listtext) == 2:
                if re.search(consonants_thai, listtext[1], re.U):
                    '''
			        จัดการกับพยัญชนะ 2 ตัว และมีแค่ 2 ตั   และมีแค่ 2 ตัวติดกันในคำ 
			        '''
                    listtext.append(consonants[listtext[1]][1])
                    listtext[1] = 'o'
                    two = True
        else:
            two = False
        i = 0
        while i < len(listtext) and two == False:
            if re.search(consonants_thai, listtext[i], re.U):
                '''
		        ถ้าหากเป็นพยัญชนะ
		        '''
                listtext[i] = consonants[listtext[i]][1]
            i += 1
        text = ''.join(listtext)  # คืนค่ากลับสู่ str
        #print(text)
        textdata.append(re.sub('/', '', text))
    return ''.join(textdata)
예제 #8
0
파일: tcc.py 프로젝트: zkan/pythainlp
# -*- coding: utf-8 -*-
from pythainlp.tokenize import tcc
print(tcc.tcc('ประเทศไทย')) # ป/ระ/เท/ศ/ไท/ย
예제 #9
0
def tcc_api():
    sent = request.args.get('sent', 0, type=str)
    txt = ""
    for i in sent.split('<br>'):
        txt += tcc.tcc(i) + "<br>"
    return jsonify(result=txt)
예제 #10
0
파일: tcc.py 프로젝트: unsuthee/pythainlp
# -*- coding: utf-8 -*-

from pythainlp.tokenize import tcc

print(tcc.tcc("ประเทศไทย"))  # ป/ระ/เท/ศ/ไท/ย

print(tcc.tcc_pos("ประเทศไทย"))  # {1, 3, 5, 6, 8, 9}

for ch in tcc.tcc_gen("ประเทศไทย"):  # ป-ระ-เท-ศ-ไท-ย-
    print(ch, end='-')
예제 #11
0
	def test_tcc(self):
		self.assertEqual(tcc.tcc('ประเทศไทย'),'ป/ระ/เท/ศ/ไท/ย')
예제 #12
0
def lenbytcc(w):
    return tcc.tcc(w, sep="|=/=|").count('|=/=|')
예제 #13
0
# -*- coding: utf-8 -*-
from pythainlp.tokenize import tcc
print(tcc.tcc('ประเทศไทย'))  # ป/ระ/เท/ศ/ไท/ย
예제 #14
0
파일: __init__.py 프로젝트: zkan/pythainlp
	def test_tcc(self):
		self.assertEqual(tcc.tcc('ประเทศไทย'),'ป/ระ/เท/ศ/ไท/ย')