Skip to content

Commit

Permalink
Merge pull request #66 from Charliechen1/master
Browse files Browse the repository at this point in the history
change import path of utils in word2vec.py to local path
  • Loading branch information
hailiang-wang authored Sep 7, 2018
2 parents 541a448 + 1c996d7 commit 19a56d2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions synonyms/word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from absl import logging

import utils
from .utils import smart_open, to_unicode, cosine
from numpy import dot, zeros, dtype, float32 as REAL,\
double, array, vstack, fromstring, sqrt, newaxis,\
ndarray, sum as np_sum, prod, ascontiguousarray,\
Expand Down Expand Up @@ -119,14 +119,14 @@ def load_word2vec_format(
if fvocab is not None:
logging.debug("loading word counts from %s" % fvocab)
counts = {}
with utils.smart_open(fvocab) as fin:
with smart_open(fvocab) as fin:
for line in fin:
word, count = utils.to_unicode(line).strip().split()
word, count = to_unicode(line).strip().split()
counts[word] = int(count)

logging.debug("loading projection weights from %s" % fname)
with utils.smart_open(fname) as fin:
header = utils.to_unicode(fin.readline(), encoding=encoding)
with smart_open(fname) as fin:
header = to_unicode(fin.readline(), encoding=encoding)
# throws for invalid file format
vocab_size, vector_size = (int(x) for x in header.split())
if limit:
Expand Down Expand Up @@ -178,7 +178,7 @@ def add_word(word, weights):
# have)
if ch != b'\n':
word.append(ch)
word = utils.to_unicode(
word = to_unicode(
b''.join(word), encoding=encoding, errors=unicode_errors)
weights = fromstring(fin.read(binary_len), dtype=REAL)
add_word(word, weights)
Expand All @@ -188,7 +188,7 @@ def add_word(word, weights):
if line == b'':
raise EOFError(
"unexpected end of input; is count incorrect or file otherwise damaged?")
parts = utils.to_unicode(
parts = to_unicode(
line.rstrip(),
encoding=encoding,
errors=unicode_errors).split(" ")
Expand Down Expand Up @@ -245,7 +245,7 @@ def neighbours(self, word, size = 10):
for (x,y) in zip(points, distances):
w = self.index2word[x]
if w == word: s = 1.0
else: s = utils.cosine(v, self.syn0[x])
else: s = cosine(v, self.syn0[x])
if s < 0: s = abs(s)
words.append(w)
scores[w] = min(s, 1.0)
Expand Down

0 comments on commit 19a56d2

Please sign in to comment.