我们如何从文本中对实体之间的分类关系做出一些通用的推断?在en_core_web_lg的word2vec模型中寻找接近“type of”的词,它们似乎都是无关的。然而,“type”附近的单词更类似于它。但是,如何在我的文本中使用常见短语,并应用一些类属相似性来从SVO三元组等推断分类法?我可以做Sense2Vec类型的方法,但是想知道现有的东西是否可以在没有新的训练的情况下使用。
以下代码的输出:
['eradicate', 'wade', 'equator', 'educated', 'lcd', 'byproducts', 'two', 'propensity', 'rhinos', 'procrastinate'] def get_related(word):
filtered_words = [w for w in word.vocab if w.is_lower == word.is_lower and w.prob >= -15]
similarity = sorted(filtered_words, key=lambda w: word.similarity(w), reverse=True)
return similarity[:10]
print ([w.lower_ for w in get_related(nlp.vocab[u'type_of'])])发布于 2018-10-29 21:16:51
您的代码检索到的所有相似性都是0.0,因此对列表进行排序没有任何效果。
您将"type_of“视为word类型(更准确地说,是一种类型),并假设spaCy会将其理解为短语类型” of“。请注意,第一个有下划线,而第二个没有;但是,即使没有下划线,它也不是模型词汇表中的词位。由于该模型没有足够的"type_of“数据来表示相似度分数,因此您与之比较的每个单词的分数都是0.0。
相反,您可以创建单词“Span of”的类型并对其调用similarity()。这只需要对您的代码进行很小的更改:
import spacy
def get_related(span): # this now expects a Span instead of a Lexeme
filtered_words = [w for w in span.vocab if
w.is_lower == span.text.islower()
and w.prob >= -15] # filter by probability and case
# (use the lowercase words if and only if the whole Span is in lowercase)
similarity = sorted(filtered_words,
key=lambda w: span.similarity(w),
reverse=True) # sort by the similarity of each word to the whole Span
return similarity[:10] # return the 10 most similar words
nlp = spacy.load('en_core_web_lg') # load the model
print([w.lower_ for w in get_related(nlp(u'type')[:])]) # print related words for "type"
print([w.lower_ for w in get_related(nlp(u'type of')[:])]) # print related words for "type of"输出:
类型‘
’,'types','kind','sort','specific','example',‘specific’,'similar','different','style‘
正如您所看到的,所有的单词在某种程度上都与输入相关,"type“和"type of”的输出是相似的,但并不相同。
https://stackoverflow.com/questions/53036318
复制相似问题