【发布时间】:2019-05-14 16:53:51
【问题描述】:
我正在尝试使用nltk.pos_tag() 从list of lists text sequence 中仅提取名词。我能够从nltk.pos_tag() 列表中提取所有名词,而不保留列表序列的列表吗?如何通过保留列表序列的列表来实现这一点。非常感谢任何帮助。
这里,列表文本序列集合的意思是:由列表分隔的标记化单词的集合。
[[('icosmos', 'JJ'), ('cosmology', 'NN'), ('calculator', 'NN'), ('with', 'IN'), ('graph', 'JJ')], [('generation', 'NN'), ('the', 'DT'), ('expanding', 'VBG'), ('universe', 'JJ')], [( '美国', 'JJ'), ('研究所', 'NN')]]
输出应如下所示:
[['宇宙学', '计算器'], ['世代'], [研究所]]
我尝试过的如下:
def function1():
tokens_sentences = sent_tokenize(tokenized_raw_data.lower())
unfiltered_tokens = [[word for word in word_tokenize(word)] for word in tokens_sentences]
word_list = []
for i in range(len(unfiltered_tokens)):
word_list.append([])
for i in range(len(unfiltered_tokens)):
for word in unfiltered_tokens[i]:
if word[:].isalpha():
word_list[i].append(word[:])
tagged_tokens=[]
for token in word_list:
tagged_tokens.append(nltk.pos_tag(token))
noun_tagged = [(word,tag) for word, tag in tagged_tokens
if tag.startswith('NN') or tag.startswith('NNPS')]
print(nouns_tagged)
如果我在附加 tagged_tokens 列表后在原始代码中使用了下面提到的 code-shippet,则输出显示在单个列表中,这不是必需的。
only_tagged_nouns = []
for sentence in tagged_tokens:
for word, pos in sentence:
if (pos == 'NN' or pos == 'NNPS'):
only_tagged_nouns.append(word)
【问题讨论】:
标签: python list nltk pos-tagger