Skip to content

Commit

Permalink
fix zero bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
shibing624 committed Feb 14, 2020
1 parent 0c90c9e commit 0f9e464
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/emb_w2v_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
from text2vec.embeddings.word_embedding import WordEmbedding

if __name__ == '__main__':
import text2vec

char = ','
result = text2vec.encode(char)
print(type(result))
print(char, result)


b = WordEmbedding()
data1 = '你 好 啊'.split(' ')
r = b.embed([data1], True)

print(r)
print(r.shape)


5 changes: 4 additions & 1 deletion text2vec/embeddings/word_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ def embed(self,
emb.append(self.w2v[word])
count += 1
tensor_x = np.array(emb).sum(axis=0) # 纵轴相加
avg_tensor_x = np.divide(tensor_x, count)
if count > 0:
avg_tensor_x = np.divide(tensor_x, count)
else:
avg_tensor_x = 0.0
embeds.append(avg_tensor_x)
embeds = np.array(embeds)
if debug:
Expand Down

0 comments on commit 0f9e464

Please sign in to comment.