You are on page 1of 2

class trie:

def __init__(_):
_.d={};_.nof_words=0

def add_word(_,w):
cd=_.d
for l in w:
if l not in cd: cd[l]={};cd=cd[l]
else: cd=cd[l]
cd["endw"]=True;_.nof_words+=1

def add_wordlist(_,wl):
for w in wl:
_.add_word(w)

def get_all(_):
r=[]
def worker(cd,w):
for k in cd.keys():
if k=="endw":r.append(w)
else: worker(cd[k],w+k)
worker(_.d,"")
return r

def __has(_,w):
cd=_.d
for l in w:
if l in cd: cd=cd[l]
else: return False
return "endw" in cd

def __contains__(_,w):
return _.__has(w)

def walk(_,w,cd=None):
if cd==None: cd=_.d
done=False
for i in range(len(w)):
if w[i] in cd: cd=cd[w[i]]
else: break
if i==len(w)-1: done=True
return {
"here": cd,
"endw":"endw" in cd and done,
"preffix": None if done else w[:i],
"done": done
}

def __small_test():
# had to put this in a method, previously this code was run at module import
# move this code into a method somewhere, for archival
dct=trie()

lw=["what","where","warrior","war","in","","mark","marketing","marketerer"]
for w in lw:
dct.add_word(w)
print(dct.nof_words,len(lw))
dctall=dct.get_all()
print(*dctall)
print(dctall==lw)
sw=["war","wars","warrior","zebra","mar"]
for s in sw:
print(s,s in dct)

def __big_test():
from words_processing import get_all_lines
wl=get_all_lines("10kwordlist.txt",True)
dct=trie()
for w in wl:
dct.add_word(w)
print(dct.nof_words,len(wl))
cr=0
for w in wl[::-1]:
if w in dct: cr+=1
print(cr,len(wl))
dctall=dct.get_all()
print(wl==dctall)
print({*wl}=={*dctall})

You might also like