3

对于 nltk 它会是这样的:

def symm_similarity(textA,textB):
    textA = set(word_tokenize(textA))
    textB = set(word_tokenize(textB))    
    intersection = len(textA.intersection(textB))
    difference = len(textA.symmetric_difference(textB))
    return intersection/float(intersection+difference) 

由于 spacy 更快,我试图在 spacy 中做到这一点,但令牌对象似乎没有为此提供快速解决方案。有任何想法吗?

谢谢大家。

4

1 回答 1

2

您的函数获取共享的单词类型的百分比,而不是标记。您正在使用一组单词,而对它们的计数不敏感。

如果您想要标记的计数,我希望以下内容会非常快,只要您加载了词汇文件(默认情况下,如果您安装了数据,它将是):

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.cis a TokenC*,因此您正在遍历连续内存并取消引用单个指针(token.lexis a const LexemeC*

于 2017-01-04T18:55:54.017 回答