【问题标题】:NLTK - Automatically translating similar wordsNLTK - 自动翻译相似词
【发布时间】:2014-01-24 03:25:13
【问题描述】:

总体目标:我正在使用 NLTK 和 Gensim 在 Python 中制作产品评论的 LDA 模型。我想在不同的 n-gram 上运行它。

问题: unigrams 一切都很好,但是当我使用 bigrams 运行时,我开始获得具有重复信息的主题。例如,主题 1 可能包含:['good product', 'good value'],主题 4 可能包含:['great product', 'great value']。对于人类来说,这些显然传达相同的信息,但显然'good product' 和'great product' 是不同的二元组。我如何通过算法确定 'good product' 和 'great product' 足够相似,以便我可以将其中一个的所有出现转换为另一个(可能是语料库中出现频率更高的那个)?

我的尝试:我玩弄了 WordNet 的 Synset 树,但运气不佳。原来good 是一个“形容词”,但great 是一个“形容词卫星”,因此返回None 以获得路径相似性。我的想法是做以下事情:

  1. 词性标记句子
  2. 使用这些 POS 找到正确的同义词集
  3. 计算两个 Synset 的相似度
  4. 如果它们高于某个阈值,则计算两个词的出现次数
  5. 用出现次数最多的词替换出现次数最少的词

不过,理想情况下,我想要一种算法,它可以确定 good 和 great 在我的语料库中是相似的(也许在同时出现的意义上),这样它就可以扩展到不属于通用英语的单词,但出现在我的语料库中,这样它就可以扩展到 n-grams(可能 Oracle 和 terrible 在我的语料库中是同义词,或者 @987654334 @ 和 feature creation 相似)。

关于算法的任何建议,或让 WordNet synset 表现的建议?

【问题讨论】:

  • 这些没有向我传达相同的信息。伟大胜于好。此外,“物有所值”表明产品的质量水平具有吸引力的价格。 “好产品”意味着产品是高质量的。最新的 Mac Pro 看起来是一款很棒的产品,我不会称之为物超所值。一种方法是询问是否用优质或价值代替产品是否真的会改变一些感兴趣的结果。
  • @ChrisP - 我明白你的意思。但这是我得到的两个主题的示例:主题 1 - ['great service', 'good product', 'great price'],主题 2 - ['good service', 'quality product', 'good price']。没有人会将它们标记为两个不同的主题,并且当有其他主题可以考虑时,它就没有用了。 good product 和 great product 都以积极的方式描述了产品,您可以从 easy reference 中看到它们的相似之处。
  • @user2979931 有没有回答您的问题?

标签: python algorithm nltk wordnet gensim


【解决方案1】:

你说(强调):

不过,理想情况下,我想要一种算法,可以确定我的语料库中的好和伟大是相似的(也许在同时发生的意义上)

您可以通过衡量这些词与其他词在同一个句子中出现的频率(即共现)来衡量词的相似性。为了捕捉更多的语义相关性,您可能还可以捕捉搭配,即该词出现在该词附近的同一单词窗口中的频率。

This paper 处理词义消歧 (WSD),它使用搭配和周围的词(共现)作为其特征空间的一部分。结果非常好,所以我想您可以使用相同的功能来解决您的问题。

在 Python 中,您可以使用sklearn,尤其是您可能需要查看SVM(带有示例代码)来帮助您入门。

总体思路是这样的:

  1. 获取一对要检查相似性的二元组
  2. 使用您的语料库,为每个二元组生成搭配和共现特征
  3. 训练 SVM 以学习第一个二元组的特征
  4. 在出现其他二元组时运行 SVM(您可以在此处获得一些分数)
  5. 可能使用分数来确定两个二元组是否相似

【讨论】:

    【解决方案2】:

    如果你打算使用 WordNet,你有

    问题1:词义消歧(WSD),即如何自动确定使用哪个同义词集?

    >>> for i in wn.synsets('good','a'):
    ...     print i.name, i.definition
    ... 
    good.a.01 having desirable or positive qualities especially those suitable for a thing specified
    full.s.06 having the normally expected amount
    good.a.03 morally admirable
    estimable.s.02 deserving of esteem and respect
    beneficial.s.01 promoting or enhancing well-being
    good.s.06 agreeable or pleasing
    good.s.07 of moral excellence
    adept.s.01 having or showing knowledge and skill and aptitude
    good.s.09 thorough
    dear.s.02 with or in a close or intimate relationship
    dependable.s.04 financially sound
    good.s.12 most suitable or right for a particular purpose
    good.s.13 resulting favorably
    effective.s.04 exerting force or influence
    good.s.15 capable of pleasing
    good.s.16 appealing to the mind
    good.s.17 in excellent physical condition
    good.s.18 tending to promote physical well-being; beneficial to health
    good.s.19 not forged
    good.s.20 not left to spoil
    good.s.21 generally admired
    
    >>> for i in wn.synsets('great','a'):
    ...     print i.name, i.definition
    ... 
    great.s.01 relatively large in size or number or extent; larger than others of its kind
    great.s.02 of major significance or importance
    great.s.03 remarkable or out of the ordinary in degree or magnitude or effect
    bang-up.s.01 very good
    capital.s.03 uppercase
    big.s.13 in an advanced stage of pregnancy
    

    假设您以某种方式获得了正确的感觉,也许您尝试了这样的事情 (https://github.com/alvations/pywsd),假设您正确理解了 POS 和 synset:

    good.a.01 具有理想或积极的品质,尤其是那些 适用于指定的事物 great.s.01 规模或数量或范围相对较大;比其他同类大

    问题 2:您将如何比较这两个同义词集?

    让我们试试相似度函数,但你发现它们没有给你分数:

    >>> good = wn.synsets('good','a')[0]
    >>> great = wn.synsets('great','a')[0]
    >>> print max(wn.path_similarity(good,great), wn.path_similarity(great, good))
    None
    >>> print max(wn.wup_similarity(good,great), wn.wup_similarity(great, good))
    
    >>> print max(wn.res_similarity(good,great,semcor_ic), wn.res_similarity(great, good,semcor_ic))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1312, in res_similarity
        return synset1.res_similarity(synset2, ic, verbose)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 738, in res_similarity
        ic1, ic2, lcs_ic = _lcs_ic(self, other, ic)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1643, in _lcs_ic
        (synset1, synset2))
    nltk.corpus.reader.wordnet.WordNetError: Computing the least common subsumer requires Synset('good.a.01') and Synset('great.s.01') to have the same part of speech.
    >>> print max(wn.jcn_similarity(good,great,semcor_ic), wn.jcn_similarity(great, good,semcor_ic))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1316, in jcn_similarity
        return synset1.jcn_similarity(synset2, ic, verbose)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 759, in jcn_similarity
        ic1, ic2, lcs_ic = _lcs_ic(self, other, ic)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1643, in _lcs_ic
        (synset1, synset2))
    nltk.corpus.reader.wordnet.WordNetError: Computing the least common subsumer requires Synset('good.a.01') and Synset('great.s.01') to have the same part of speech.
    >>> print max(wn.lin_similarity(good,great,semcor_ic), wn.lin_similarity(great, good,semcor_ic))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1320, in lin_similarity
        return synset1.lin_similarity(synset2, ic, verbose)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 789, in lin_similarity
        ic1, ic2, lcs_ic = _lcs_ic(self, other, ic)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1643, in _lcs_ic
        (synset1, synset2))
    nltk.corpus.reader.wordnet.WordNetError: Computing the least common subsumer requires Synset('good.a.01') and Synset('great.s.01') to have the same part of speech.
    >>> print max(wn.lch_similarity(good,great), wn.lch_similarity(great, good))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1304, in lch_similarity
        return synset1.lch_similarity(synset2, verbose, simulate_root)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 638, in lch_similarity
        (self, other))
    nltk.corpus.reader.wordnet.WordNetError: Computing the lch similarity requires Synset('good.a.01') and Synset('great.s.01') to have the same part of speech.
    

    让我们尝试一对不同的同义词,因为good 有satellite-adjective 和adjective 而great 只有satellite,让我们用最小的公分母:

    good.s.13 resulting favorably
    great.s.01 relatively large in size or number or extent; larger than others of its kind
    

    你意识到satellite-adjective之间仍然没有相似性信息进行比较:

    >>> print max(wn.lin_similarity(good,great,semcor_ic), wn.lin_similarity(great, good,semcor_ic))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1320, in lin_similarity
        return synset1.lin_similarity(synset2, ic, verbose)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 789, in lin_similarity
        ic1, ic2, lcs_ic = _lcs_ic(self, other, ic)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1645, in _lcs_ic
        ic1 = information_content(synset1, ic)
      File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/reader/wordnet.py", line 1666, in information_content
        raise WordNetError(msg % synset.pos)
    nltk.corpus.reader.wordnet.WordNetError: Information content file has no entries for part-of-speech: s
    >>> print max(wn.path_similarity(good,great), wn.path_similarity(great, good))None
    None
    

    现在看来,WordNet 造成的问题比它在这里解决的任何问题都多,让我们尝试另一种方法,让我们尝试词聚类,参见 http://en.wikipedia.org/wiki/Word-sense_induction

    此时我也放弃回答 OP 发布的广泛而开放的问题,因为集群中有很多工作对像我这样的凡人来说是自动的 =)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多