您的函数获取共享的单词类型的百分比,而不是标记。您正在使用一组单词,而对它们的计数不敏感。
如果您想要标记的计数,我希望以下内容会非常快,只要您加载了词汇文件(默认情况下,如果您安装了数据,它将是):
from spacy.attrs import ORTH
def symm_similarity_types(nlp, textA,textB):
docA = nlp.make_doc(textA)
docB = nlp.make_doc(textB)
countsA = Counter(docA.count_by(ORTH))
countsB = Counter(docB.count_by(ORTH)
diff = sum(abs(val) for val in (countsA - countsB).values())
return diff / (len(docA) + len(docB))
如果您想计算与上面的代码完全相同的东西,这里是 spaCy 等价物。该Doc
对象允许您迭代Token
对象。然后,您应该根据属性进行计数,该token.orth
属性是字符串的整数 ID。我希望使用整数会比字符串集快一点:
def symm_similarity_types(nlp, textA,textB):
docA = set(w.orth for w in nlp(textA)
docB = set(w.orth for w in nlp(textB)
intersection = len(textA.intersection(textB))
difference = len(textA.symmetric_difference(textB))
return intersection/float(intersection+difference)
这应该比 NLTK 版本更有效,因为您使用的是整数集,而不是字符串。
如果你真的关心效率,那么只在 Cython 中工作通常更方便,而不是试图猜测 Python 在做什么。这是基本循环:
# cython: infer_types=True
for token in doc.c[:doc.length]
orth = token.lex.orth
doc.c
is a TokenC*
,因此您正在遍历连续内存并取消引用单个指针(token.lex
is a const LexemeC*
)