Select 只有 'NN' 和 'VB' 来自 NTLK 的单词 pos_tag
Select only 'NN' and 'VB' words from NTLK pos_tag
我只需要打印输入句子中的 'NN' 和 'VB' 个词。
import nltk
import re
import time
var = raw_input("Please enter something: ")
exampleArray = [var]
def processLanguage():
try:
for item in exampleArray:
tokenized = nltk.word_tokenize(item)
tagged = nltk.pos_tag(tokenized)
print tagged
time.sleep(555)
except Exception, e:
print str(e)
processLanguage()
改一下怎么样
print tagged
至
print [(word, tag) for word, tag in tagged if tag in ('NN', 'VB')]
您可能需要使用 POS 标签的前 2 个字符,请参阅
nn_vb_tagged = [(word,tag) for word, tag in tagged
if tag.startswith('NN') or tag.startswith('VB')]
你可以试试这个:
example = "This is a sample sentence, showing off the stop words filtration.!"
word_tokens = word_tokenize(example)
pos = nltk.pos_tag(word_tokens)
selective_pos = ['NN','VB']
selective_pos_words = []
for word,tag in pos:
if tag in selective_pos:
selective_pos_words.append((word,tag))
print(selective_pos_words)
通过在列表 "selective_pos" 中添加你的 select 五个词性,你可以 select 任何你喜欢的词。
我只需要打印输入句子中的 'NN' 和 'VB' 个词。
import nltk
import re
import time
var = raw_input("Please enter something: ")
exampleArray = [var]
def processLanguage():
try:
for item in exampleArray:
tokenized = nltk.word_tokenize(item)
tagged = nltk.pos_tag(tokenized)
print tagged
time.sleep(555)
except Exception, e:
print str(e)
processLanguage()
改一下怎么样
print tagged
至
print [(word, tag) for word, tag in tagged if tag in ('NN', 'VB')]
您可能需要使用 POS 标签的前 2 个字符,请参阅
nn_vb_tagged = [(word,tag) for word, tag in tagged
if tag.startswith('NN') or tag.startswith('VB')]
你可以试试这个:
example = "This is a sample sentence, showing off the stop words filtration.!"
word_tokens = word_tokenize(example)
pos = nltk.pos_tag(word_tokens)
selective_pos = ['NN','VB']
selective_pos_words = []
for word,tag in pos:
if tag in selective_pos:
selective_pos_words.append((word,tag))
print(selective_pos_words)
通过在列表 "selective_pos" 中添加你的 select 五个词性,你可以 select 任何你喜欢的词。