Skip to content

Instantly share code, notes, and snippets.

@danqing
Created August 2, 2016 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danqing/68727a0f4edac2f0835c2fba3ade47a0 to your computer and use it in GitHub Desktop.
Save danqing/68727a0f4edac2f0835c2fba3ade47a0 to your computer and use it in GitHub Desktop.
Some scratch of w2v experiments
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