Skip to content

Commit f06520b

Browse files
committed
提高DocVectorModel健壮性
1 parent b9a3a58 commit f06520b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/main/java/com/hankcs/hanlp/mining/word2vec/DocVectorModel.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,23 @@ public Vector query(String content)
6868
{
6969
if (content == null || content.length() == 0) return null;
7070
List<Term> termList = NotionalTokenizer.segment(content);
71-
if (termList.isEmpty())
72-
{
73-
return null;
74-
}
7571
Vector result = new Vector(dimension());
72+
int n = 0;
7673
for (Term term : termList)
7774
{
7875
Vector vector = wordVectorModel.vector(term.word);
76+
if (vector == null)
77+
{
78+
continue;
79+
}
80+
++n;
7981
result.addToSelf(vector);
8082
}
81-
result.divideToSelf(termList.size());
83+
if (n == 0)
84+
{
85+
return null;
86+
}
87+
result.divideToSelf(n);
8288
return result;
8389
}
8490

0 commit comments

Comments
 (0)