Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在nltk中搜索特定的POS标签?_Python_Nltk - Fatal编程技术网

Python 如何在nltk中搜索特定的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 = [

在执行之后,我得到带有等价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 = ['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')]