'''
ตัดคำภาษาไทยโดยใช้ Maximum Matching algorithm
เดติดโค้ดต้นฉบับ คุณ Korakot Chaovavanich
จาก https://www.facebook.com/groups/408004796247683/permalink/431283740586455/ 
'''
from marisa_trie import Trie
from collections import Counter, defaultdict
from pythainlp.corpus.thaiword import get_data
class LatticeString(str):
    def __new__(cls, value, multi=None): 
        ''' Return a string instance 
        ''' 
        return str.__new__(cls, value)
    
    def __init__(self, value, multi=None):
        self.unique = True
        if multi:
            self.multi = list(multi)
            if len(self.multi) > 1:
                self.unique = False
        else:
            self.multi = [value]
            
        self.in_dict = True   # บอกว่าเป็นคำมีในดิกหรือเปล่า

    def suggest(self):
        return []
def serialize(p, p2):
    for w in words_at[p]:
        p_ = p + len(w)
        if p_== p2:
            yield w
        elif p_ < p2:
            for path in serialize(p_, p2):
                yield w+'/'+path
# มี jigsaw พร้อมแล้ว  ต่อไปก็ลองเขียน tcut ใหม่
def tcut(text):
    #global last_p, i, q, ww   # for debug
    trie = Trie(get_data())
    words_at = defaultdict(list)  # main data structure
    
    def serialize(p, p2):    # helper function
        for w in words_at[p]:
            p_ = p + len(w)
            if p_== p2:
                yield w
            elif p_ < p2:
                for path in serialize(p_, p2):
                    yield w+'/'+path
                    
    q = {0}
    last_p = 0   # last position for yield
    while min(q) < len(text):
        p = min(q)
        q -= {p}  # q.pop, but for set
        
        for w in trie.prefixes(text[p:]):
            words_at[p].append(w)
            q.add(p+len(w))   
            
        if len(q)==1:
            q0 = min(q)
            yield LatticeString(text[last_p:q0], serialize(last_p, q0))
            last_p = q0
            
        # กรณี len(q) == 0  คือ ไม่มีใน dict
        if len(q)==0:
            # skip น้อยที่สุด ที่เป็นไปได้
            for i in range(p, len(text)):
                ww = trie.prefixes(text[i:])
                if ww:
                    break
            else:
                i = len(text)
            w = text[p:i]
            words_at[p].append(w)
            yield LatticeString(w, in_dict=False)
            last_p = i
            q.add(i)
def mmcut(text):
    res = []
    for w in tcut(text):
        if w.unique:
            res.append(w)
        else:
            mm = min(w.multi, key=lambda x: x.count('/'))
            res.extend(mm.split('/'))
    return res
def combine(ww):
    if ww == []:
        yield ""
    else:
        w = ww[0]
        for tail in combine(ww[1:]):
            if w.unique:
                yield w+"|"+tail
            else:
                for m in w.multi:
                    yield m.replace("/","|")+"|"+tail
                    
def listcut(text):
    ww = list(tcut(text))
    return list(combine(ww))
if __name__ == "__main__":
	text='ผมตากลมเย็นสบายดี'
	print(mmcut(text))
	print(listcut(text))
@@ -354,16 +354,30 @@
 lti=None
+, in_dict=True
 ): %0A    
@@ -507,16 +507,30 @@
 lti=None
+, in_dict=True
 ):%0A     
@@ -761,20 +761,23 @@
 _dict = 
-True
+in_dict
    # %E0%B8%9A%E0%B8%AD%E0%B8%81
@@ -2282,16 +2282,65 @@
 xt%5Bp:i%5D%0A
+            w = w.replace(' ','') # %E0%B8%A5%E0%B8%9A%E0%B8%84%E0%B9%88%E0%B8%B2%E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%A7%E0%B9%88%E0%B8%B2%E0%B8%87%0A
         
@@ -3112,16 +3112,25 @@
 %E0%B9%87%E0%B8%99%E0%B8%AA%E0%B8%9A%E0%B8%B2%E0%B8%A2%E0%B8%94%E0%B8%B5
+ok %E0%B9%80%E0%B8%82%E0%B9%89%E0%B8%B2%E0%B9%83%E0%B8%88
 '%0A%09print

fix newmm #2