WordNet Python 单词相似度
Posted
技术标签:
【中文标题】WordNet Python 单词相似度【英文标题】:WordNet Python words similarity 【发布时间】:2017-06-07 05:23:22 【问题描述】:我正在尝试找到一种可靠的方法来衡量 2 个术语的语义相似性。 第一个指标可能是下位词/上位词图上的路径距离(最终 2-3 个指标的线性组合可能会更好..)。
from nltk.corpus import wordnet as wn
dog = wn.synset('dog.n.01')
cat = wn.synset('cat.n.01')
print(dog.path_similarity(cat))
我仍然不明白n.01
的含义以及为什么它是必要的。
有没有一种方法可以直观地显示 2 个术语之间的计算路径?
我还可以使用哪些其他 nltk 语义指标?
【问题讨论】:
【参考方案1】:1.我仍然不明白 n.01 的含义以及为什么它是必要的。
来自here 和source of nltk 表明结果是"WORD.PART-OF-SPEECH.SENSE-NUMBER"
引用来源:
Create a Lemma from a "<word>.<pos>.<number>.<lemma>" string where:
<word> is the morphological stem identifying the synset
<pos> is one of the module attributes ADJ, ADJ_SAT, ADV, NOUN or VERB
<number> is the sense number, counting from 0.
<lemma> is the morphological form of interest
n 表示名词,我也建议阅读wordnet dataset。
2。有没有一种方法可以直观地显示 2 个术语之间的计算路径?
请查看相似性部分的nltk wordnet docs。你有几种路径算法选择(你可以尝试混合几种)。
nltk 文档中的几个例子:
from nltk.corpus import wordnet as wn
dog = wn.synset('dog.n.01')
cat = wn.synset('cat.n.01')
print(dog.path_similarity(cat))
print(dog.lch_similarity(cat))
print(dog.wup_similarity(cat))
对于可视化,您可以构建一个距离矩阵M[i,j]
其中:
M[i,j] = word_similarity(i, j)
并使用以下*** answer 绘制可视化。
3.我还可以使用哪些其他 nltk 语义指标?
如上所述,有几种方法可以计算单词相似度。我还建议调查gensim。我使用它的 word2vec 实现来实现单词相似性,它对我来说效果很好。
如果您在选择算法时需要任何帮助,请提供有关您所面临问题的更多信息。
更新:
更多关于词sense number
含义的信息可以找到here:
WordNet 中的词义通常从最常用到最不常用的顺序排列,最常用的词序号为 1...
问题是“狗”是模棱两可的,你必须为它选择正确的含义。
您可能会选择第一种含义作为幼稚的方法,或者根据您的应用或研究找到自己的算法来选择正确的含义。
要从 wordnet 中获取单词的所有可用定义(在 wordnet 文档中称为 synsets),您只需调用 wn.synsets(word)
。
我鼓励您深入了解每个定义的这些同义词集中包含的元数据。
下面的代码显示了一个简单的示例来获取此元数据并很好地打印它。
from nltk.corpus import wordnet as wn
dog_synsets = wn.synsets('dog')
for i, syn in enumerate(dog_synsets):
print('%d. %s' % (i, syn.name()))
print('alternative names (lemmas): "%s"' % '", "'.join(syn.lemma_names()))
print('definition: "%s"' % syn.definition())
if syn.examples():
print('example usage: "%s"' % '", "'.join(syn.examples()))
print('\n')
代码输出:
0. dog.n.01
alternative names (lemmas): "dog", "domestic_dog", "Canis_familiaris"
definition: "a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds"
example usage: "the dog barked all night"
1. frump.n.01
alternative names (lemmas): "frump", "dog"
definition: "a dull unattractive unpleasant girl or woman"
example usage: "she got a reputation as a frump", "she's a real dog"
2. dog.n.03
alternative names (lemmas): "dog"
definition: "informal term for a man"
example usage: "you lucky dog"
3. cad.n.01
alternative names (lemmas): "cad", "bounder", "blackguard", "dog", "hound", "heel"
definition: "someone who is morally reprehensible"
example usage: "you dirty dog"
4. frank.n.02
alternative names (lemmas): "frank", "frankfurter", "hotdog", "hot_dog", "dog", "wiener", "wienerwurst", "weenie"
definition: "a smooth-textured sausage of minced beef or pork usually smoked; often served on a bread roll"
5. pawl.n.01
alternative names (lemmas): "pawl", "detent", "click", "dog"
definition: "a hinged catch that fits into a notch of a ratchet to move a wheel forward or prevent it from moving backward"
6. andiron.n.01
alternative names (lemmas): "andiron", "firedog", "dog", "dog-iron"
definition: "metal supports for logs in a fireplace"
example usage: "the andirons were too hot to touch"
7. chase.v.01
alternative names (lemmas): "chase", "chase_after", "trail", "tail", "tag", "give_chase", "dog", "go_after", "track"
definition: "go after with the intent to catch"
example usage: "The policeman chased the mugger down the alley", "the dog chased the rabbit"
【讨论】:
什么是感觉数?阅读此wordnet.princeton.edu/man/wndb.5WN.html#sect4 我想知道是否可以避免定义它.. 我用更多关于感觉数字的信息更新了我的答案,希望对您有所帮助。如果您需要更多信息,请更新或使用更具体的用例打开另一个问题来讨论解决问题的方法。以上是关于WordNet Python 单词相似度的主要内容,如果未能解决你的问题,请参考以下文章