testing scratch pad and stuff - ignore this


In [40]:
from imp import reload
import numpy as np
import menu, preprocess, models
reload(menu)


Out[40]:
<module 'menu' from '/home/mike/py/siraj/Siraj_Chatbot_Challenge/menu.py'>

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()


debug: calling show_menumain menu
main menu
 q: Quit
 1: choice 1
 2: choice 2
Enter menu selection: m
Invalid entry!
Quitting

In [29]:
res


Out[29]:
'you chose 1'

In [34]:
'~' * 3


Out[34]:
'~~~'

In [35]:
input('enter')


enteraoeu
Out[35]:
'aoeu'

In [37]:
reload(preprocess)
reload(models)
ve = preprocess.BabiVectorizer()
ve.vectorize_query('Where is John?', verbose=True)


/home/mike/ve/keras/lib/python3.5/re.py:203: FutureWarning: split() requires a non-empty pattern match.
  return _compile(pattern, flags).split(string, maxsplit)
<debug> Where is John?
<v>Vectorize_query(): [7, 13, 4, 2]
Out[37]:
array([[ 7, 13,  4,  2]], dtype=int32)

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)


(21, 128)
Out[50]:
'John'

In [51]:
import os

In [58]:
os.path.normpath()


Out[58]:
'foo/bar/spam.txt'

In [57]:
os.sep


Out[57]:
'/'

In [59]:
ll


total 1808
-rw-rw-r-- 1 mike 234560 Apr  4 17:02 c1_dmn00.hdf5
-rw-rw-r-- 1 mike 748816 Apr  4 16:17 c2_dmn00.hdf5
-rw-rw-r-- 1 mike 234560 Apr  4 13:06 dmn00.hdf5
-rw-rw-r-- 1 mike 234560 Apr  4 15:38 dmn01.hdf5
-rw-rw-r-- 1 mike 234560 Apr  4 16:00 foo.hdf5
-rw-rw-r-- 1 mike  11357 Apr  3 17:21 LICENSE
-rw-rw-r-- 1 mike   5038 Apr  4 17:15 main.py
-rw-rw-r-- 1 mike   5957 Apr  4 17:15 menu.py
-rw-rw-r-- 1 mike   3513 Apr  4 14:02 models.py
-rw-rw-r-- 1 mike  10618 Apr  4 15:10 preprocess.py
drwxrwxr-x 2 mike   4096 Apr  4 17:15 __pycache__/
-rw-rw-r-- 1 mike    785 Apr  4 17:12 README.md
-rw-rw-r-- 1 mike   6899 Apr  4 17:20 test_menu.ipynb
-rw-rw-r-- 1 mike  88279 Apr  4 16:04 training.ipynb

In [65]:
fname = 'foo/bar//spam.txt'
os.makedirs(os.path.dirname(fname), exist_ok=True)

In [66]:
os.path.normpath(fname)


Out[66]:
'foo/bar/spam.txt'

In [ ]: