使用 nltk 进行词性处理的一般同义词和词性



我正在尝试为句子中重要的单词(即不是"a"或"the")创建一个通用的同义词标识符,并且我正在使用python中的自然语言工具包(nltk)。我遇到的问题是 nltk 中的同义词查找器需要词性参数才能链接到其同义词。我试图解决此问题的是使用 nltk 中存在的简化词性标记器,然后减少第一个字母以便将此参数传递到同义词查找器中,但这不起作用。

def synonyms(Sentence):
    Keywords = []
    Equivalence = WordNetLemmatizer()
    Stemmer = stem.SnowballStemmer('english')
    for word in Sentence:
        word = Equivalence.lemmatize(word)
    words = nltk.word_tokenize(Sentence.lower())
    text = nltk.Text(words)
    tags = nltk.pos_tag(text)
    simplified_tags = [(word, simplify_wsj_tag(tag)) for word, tag in tags]
    for tag in simplified_tags:
        print tag
        grammar_letter = tag[1][0].lower()
        if grammar_letter != 'd':
            Call = tag[0].strip() + "." + grammar_letter.strip() + ".01"
            print Call
            Word_Set = wordnet.synset(Call)
            paths = Word_Set.lemma_names
            for path in paths:
                Keywords.append(Stemmer.stem(path))
    return Keywords

这是我目前正在使用的代码,正如你所看到的,我首先对输入进行词形还原,以减少从长远来看我将拥有的匹配数量(我计划在数万个句子上运行它),理论上我会在此之后对单词进行词干,以进一步达到这种效果并减少我生成的冗余单词的数量, 但是,此方法几乎总是以以下形式返回错误:

Traceback (most recent call last):
  File "C:Python27test.py", line 45, in <module>
    synonyms('spray reddish attack force')
  File "C:Python27test.py", line 39, in synonyms
    Word_Set = wordnet.synset(Call)
  File "C:Python27libsite-packagesnltkcorpusreaderwordnet.py", line 1016, in synset
    raise WordNetError(message % (lemma, pos))
WordNetError: no lemma 'reddish' with part of speech 'n'

我对这将运行的数据没有太多控制权,因此简单地清理我的语料库并不是一个真正的选择。关于如何解决这个问题的任何想法?

做了更多的研究,我有一个有希望的线索,但我仍然不确定如何实施它。在未找到或错误分配的单词的情况下,我想使用相似性度量(Leacock Chodorow,Wu-Palmer等)将单词链接到最接近的正确分类的其他关键字。也许与编辑距离测量相结合,但我再次无法找到任何关于此的文档。

显然 nltk 允许检索与单词相关的所有语法集。当然,它们中通常有许多反映了不同的词义。为了在功能上找到同义词(或者如果两个单词是同义词),您必须尝试匹配尽可能接近的同义词集,这可以通过上述任何相似性指标来实现。我编写了一些基本代码来执行此操作,如下所示,如何查找两个单词是否为同义词:

from nltk.corpus import wordnet
from nltk.stem.wordnet import WordNetLemmatizer
import itertools

def Synonym_Checker(word1, word2):
    """Checks if word1 and word2 and synonyms. Returns True if they are, otherwise False"""
    equivalence = WordNetLemmatizer()
    word1 = equivalence.lemmatize(word1)
    word2 = equivalence.lemmatize(word2)
    word1_synonyms = wordnet.synsets(word1)
    word2_synonyms = wordnet.synsets(word2)
    scores = [i.wup_similarity(j) for i, j in list(itertools.product(word1_synonyms, word2_synonyms))]
    max_index = scores.index(max(scores))
    best_match = (max_index/len(word1_synonyms), max_index % len(word1_synonyms)-1)
    word1_set = word1_synonyms[best_match[0]].lemma_names
    word2_set = word2_synonyms[best_match[1]].lemma_names
    match = False
    match = [match or word in word2_set for word in word1_set][0]
    return match
print Synonym_Checker("tomato", "Lycopersicon_esculentum")

可能会尝试实现越来越强大的词干提取算法,但对于我所做的前几次测试,这段代码实际上适用于我能找到的每个单词。如果有人对如何改进这个算法有想法,或者有任何改进这个答案的想法,我很想听听。

你能

try:包裹你的Word_Set = wordnet.synset(Call)并忽略WordNetError异常吗?看起来您遇到的错误是某些单词未正确分类,但无法识别的单词也会发生此异常,因此捕获异常对我来说似乎是一个好主意。

最新更新