Python 如何在nltk中搜索特定的POS标签?
在执行之后,我得到带有等价POS标记的标记化单词。现在我需要知道如何搜索特定的POS标签,这样我就可以在文本中执行一些过滤 您可以使用简单的列表组件:Python 如何在nltk中搜索特定的POS标签?,python,nltk,Python,Nltk,在执行之后,我得到带有等价POS标记的标记化单词。现在我需要知道如何搜索特定的POS标签,这样我就可以在文本中执行一些过滤 您可以使用简单的列表组件: from nltk.tokenize import word_tokenize from nltk.tokenize import PunktSentenceTokenizer #Other lines of code question = ['Add a device under the name karthik'] responses = [
from nltk.tokenize import word_tokenize
from nltk.tokenize import PunktSentenceTokenizer
#Other lines of code
question = ['Add a device under the name karthik']
responses = ['Added']
if user_text in question:
token = word_tokenize(user_text)
custom_sent_tokenizer = PunktSentenceTokenizer([question])
tokenized = custom_sent_tokenizer.tokenize(user_text)
for i in tokenized[:5]:
words = nltk.word_tokenize(i)
tagged = nltk.pos_tag(words)
print(tagged)
或者您可以使用:
您可以使用一个简单的列表comp:
from nltk.tokenize import word_tokenize
from nltk.tokenize import PunktSentenceTokenizer
#Other lines of code
question = ['Add a device under the name karthik']
responses = ['Added']
if user_text in question:
token = word_tokenize(user_text)
custom_sent_tokenizer = PunktSentenceTokenizer([question])
tokenized = custom_sent_tokenizer.tokenize(user_text)
for i in tokenized[:5]:
words = nltk.word_tokenize(i)
tagged = nltk.pos_tag(words)
print(tagged)
或者您可以使用:
非常感谢你!第一种方法对我不管用,但第二种方法管用。非常感谢!第一种方法对我不管用,但第二种方法管用。
>>> sentence = "I am seated in an office, surrounded by heads and bodies."
>>> tokenized = word_tokenize(sentence)
>>> tagged = nltk.pos_tag(tokenized)
>>> list(filter(lambda x: x[1] == 'NNS', tagged))
[('heads', 'NNS'), ('bodies', 'NNS')]