函数 returns python 中的一个句子(名词 POS 标记)中有多少个名词

function that returns how many nouns in a sentence (noun POS tag) in python

var = ("this is my problem , i need help for a function like this one ")
exampleArray = [var]
def process_language():
    for item in exampleArray:
        tokenized = nltk.word_tokenize(item)
        tagged = nltk.pos_tag(tokenized)
        print (tagged)
process_language()

这个函数returns:

[('this', 'DT'), ('is', 'VBZ'), ('my', 'PRP$'), ('problem', 'NN'), (',', ','), 
('i', 'VBP'), ('need', 'VBP'), ('help', 'NN'), ('for', 'IN'), ('a', 'DT'), 
('function', 'NN'), ('like', 'IN'), ('this', 'DT'), ('one', 'NN')]

我正在寻找与 returns 4 类似的函数,表示四个名词谢谢

sentence = ("this is my problem , i need help for a function like this one ")
sentence = nltk.word_tokenize(sentence)
sent = pos_tag(sentence)
listnn = ([s for s in sent if s[1] == 'NN'])
print (listnn)
print (len(listnn))

结果是 [('problem', 'NN'), ('help', 'NN'), ('function', 'NN'), ('one', 'NN')] 4

算一下

>>> from nltk import word_tokenize, pos_tag
>>> s = "this is my problem , i need help for a function like this one "
>>> sum(1 for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN'))
4

带名词

>>> from nltk import word_tokenize, pos_tag
>>> s = "this is my problem , i need help for a function like this one "
>>> sum(1 for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN'))
4
>>> nouns = [word for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN')]
>>> len(nouns)
4
>>> nouns
['problem', 'help', 'function', 'one']

有名词和词性

>>> from nltk import word_tokenize, pos_tag
>>> s = "this is my problem , i need help for a function like this one "
>>> nouns = [(word,pos) for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN')]
>>> nouns
[('problem', 'NN'), ('help', 'NN'), ('function', 'NN'), ('one', 'NN')]