在整个句子上应用NLP WordNetLemmatizer对具有未知pos的单词显示错误

问题描述 投票:2回答:1

我想在整个句子上应用NLP WordNetLemmatizer。问题是我得到一个错误:

KeyError: 'NNP'

这就像我正在获得未知的'pos'值,但我不知道为什么。我想获取单词的基本形式,但是如果没有“ pos”,它将无法正常工作。你能告诉我我在做什么错吗?

import nltk

from nltk.tokenize import PunktSentenceTokenizer
from nltk.tokenize import word_tokenize
from nltk.tokenize import RegexpTokenizer
from nltk.stem import WordNetLemmatizer 

nltk.download('averaged_perceptron_tagger')

sentence = "I want to find the best way to lemmantize this sentence so that I can see better results of it"

taged_words = nltk.pos_tag(sentence)
print(taged_words)


lemmantised_sentence = []

lemmatizer = WordNetLemmatizer()
for word in taged_words:

     filtered_text_lemmantised =  lemmatizer.lemmatize(word[0], pos=word[1])
     print(filtered_text_lemmantised)

     lemmantised_sentence.append(filtered_text_lemmantised)

lemmantised_sentence = ' '.join(lemmantised_sentence)
print(lemmantised_sentence)
python nlp nltk
1个回答
3
投票

将句子发送到pos_tag函数之前,应先将其拆分。另外,pos参数接受的字符串类型有所不同。它仅接受“ N”,“ V”等。我已经从此https://stackoverflow.com/a/15590384/7349991更新了您的代码。

import nltk

from nltk.tokenize import PunktSentenceTokenizer
from nltk.tokenize import word_tokenize
from nltk.tokenize import RegexpTokenizer
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet

def main():
    nltk.download('averaged_perceptron_tagger')
    nltk.download('wordnet')

    sentence = "I want to find the best way to lemmantize this sentence so that I can see better results of it"

    taged_words = nltk.pos_tag(sentence.split())
    print(taged_words)

    lemmantised_sentence = []


    lemmatizer = WordNetLemmatizer()
    for word in taged_words:
        if word[1]=='':
            continue
        filtered_text_lemmantised = lemmatizer.lemmatize(word[0], pos=get_wordnet_pos(word[1]))
        print(filtered_text_lemmantised)

        lemmantised_sentence.append(filtered_text_lemmantised)

    lemmantised_sentence = ' '.join(lemmantised_sentence)
    print(lemmantised_sentence)

def get_wordnet_pos(treebank_tag):

    if treebank_tag.startswith('J'):
        return wordnet.ADJ
    elif treebank_tag.startswith('V'):
        return wordnet.VERB
    elif treebank_tag.startswith('N'):
        return wordnet.NOUN
    else:
        return wordnet.ADV


if __name__ == '__main__':
    main()
© www.soinside.com 2019 - 2024. All rights reserved.