【发布时间】:2018-12-08 04:50:24
【问题描述】:
那里!
我尝试输出文本中每个单词的所有可能的词性(pos)。但是,我需要将输出打印为“列表列表”或“元组列表”以供进一步使用。
任何人都可以提供帮助,非常感谢!
import nltk
from nltk.tokenize import word_tokenize
text = "I can answer those question ." # original text
tokenized_text = word_tokenize(text) # word tokenization
wsj = nltk.corpus.treebank.tagged_words()
cfd1 = nltk.ConditionalFreqDist(wsj) # find all possible pos of each word
i = 0
while i< len(tokenized_text):
pos_only = list(cfd1[tokenized_text[i]])
y = pos_only
print(y)
i+=1
我的输出是
['NNP', 'PRP']
['MD', 'NN']
['NN', 'VB']
['DT']
['NN', 'VBP', 'VB']
['.']
我的预期输出是
[['NNP', 'PRP'], ['MD', 'NN'], ['NN', 'VB'], ['DT'], ['NN', 'VBP', 'VB'], ['.']]
或
[('NNP', 'PRP'), ('MD', 'NN'), ('NN', 'VB'), ('DT'), ('NN', 'VBP', 'VB'), ('.')]
【问题讨论】:
标签: list loops while-loop tuples output