In [40]:
from imp import reload
import numpy as np
import menu, preprocess, models
reload(menu)
Out[40]:
In [31]:
menuitems = [('1', 'choice 1', lambda: 'you chose 1'),
('2', 'choice 2', lambda: 'you chose 2')
]
m = menu.Menu('00', 'main menu', menuitems)
In [32]:
x = menu.Choice(menuitems[0])
In [33]:
res = m()
In [29]:
res
Out[29]:
In [34]:
'~' * 3
Out[34]:
In [35]:
input('enter')
Out[35]:
In [37]:
reload(preprocess)
reload(models)
ve = preprocess.BabiVectorizer()
ve.vectorize_query('Where is John?', verbose=True)
Out[37]:
In [50]:
def charvectorize(word, lower=True):
if lower:
word = word.lower()
idxs = [ord(c) for c in word]
vec = np.zeros(128, int)
for c in word:
vec[ord(c)] = 1
return vec
def dist(v1, v2):
dv = v2 - v1
dv = dv**2
dv = np.sum(dv, axis=-1)
return dv**0.5
def softdist(word1, word2, lower=True):
v1 = charvectorize(word1, lower)
v2 = charvectorize(word2, lower)
return dist(v1, v2)
def matchnocase(word, vocab):
lword = word.lower()
listvocab = list(vocab)
lvocab = [w.lower() for w in listvocab]
if lword in lvocab:
return listvocab[lvocab.index(lword)]
return None
def softmatch(word, vocab, cutoff=2.):
"""Try to soft-match to catch various typos. """
vw = charvectorize(word)
vecs = np.array([charvectorize(w) for w in vocab])
print(vecs.shape)
distances = dist(vw, vecs)
idx = np.argmin(distances)
confidence = distances[idx]
if confidence < cutoff:
return vocab[idx]
return None
softmatch('john?', list(ve.word_idx))
# matchnocase('MAry', ve.word_idx)
Out[50]:
In [51]:
import os
In [58]:
os.path.normpath()
Out[58]:
In [57]:
os.sep
Out[57]:
In [59]:
ll
In [65]:
fname = 'foo/bar//spam.txt'
os.makedirs(os.path.dirname(fname), exist_ok=True)
In [66]:
os.path.normpath(fname)
Out[66]:
In [ ]: