Created
August 2, 2016 18:46
-
-
Save danqing/68727a0f4edac2f0835c2fba3ade47a0 to your computer and use it in GitHub Desktop.
Some scratch of w2v experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
from numpy.linalg import norm | |
from nltk.corpus import stopwords | |
from nltk import download | |
download('stopwords') | |
stop_words = stopwords.words('english') | |
sset = [ | |
'whats the weather like', | |
'its rainy today', | |
'i feel sad', | |
'i feel happy', | |
'you suck', | |
'are you a machine', | |
'can i ask a question', | |
'thanks', | |
'you are so cute', | |
'can you sing a song' | |
] | |
def build(strings): | |
vectors = [] | |
for string in strings: | |
vector = None | |
for w in string.split(): | |
if w in stop_words: | |
continue | |
vector = numpy.add(vector, gb[w]) if vector is not None else gb[w] | |
vectors.append(vector) | |
return vectors | |
def closest(string, target): | |
value = None | |
for w in string.split(): | |
if w in stop_words: | |
continue | |
value = numpy.add(value, gb[w]) if value is not None else gb[w] | |
minid = -1 | |
dist = 99999999999 | |
for i in range(len(target)): | |
d = norm(value - target[i]) | |
if d < dist: | |
minid = i | |
dist = d | |
return sset[minid] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment