Chunk.draw()无法在python IDLE中工作

时间:2017-12-24 17:49:54

标签: python nltk

我无法在Python IDLE中绘制分块数据,我在Python cmd中成功打印它,我正在使用python 2.7并且我在youtube上关注教程,我发现this tutorial,但我无法获得通过使用draw()函数获得所需的结果。 这是我的代码,如果我做错了什么,请提示我,

import nltk
from nltk.corpus import state_union
from nltk import PunktSentenceTokenizer
'''
POS tag list:

CC  Coordinating conjunction
CD  Cardinal number
DT  Determiner
EX  Existential there
FW  Foreign word
IN  Preposition or subordinating conjunction
JJ  Adjective
JJR Adjective, comparative
JJS Adjective, superlative
LS  List item marker
MD  Modal
NN  Noun, singular or mass
NNS Noun, plural
NNP Proper noun, singular
NNPS    Proper noun, plural
PDT Predeterminer
POS Possessive ending
PRP Personal pronoun
PRP$    Possessive pronoun
RB  Adverb
RBR Adverb, comparative
RBS Adverb, superlative
RP  Particle
SYM Symbol
TO  to
UH  Interjection
VB  Verb, base form
VBD Verb, past tense
VBG Verb, gerund or present participle
VBN Verb, past participle
VBP Verb, non-3rd person singular present
VBZ Verb, 3rd person singular present
WDT Wh-determiner
WP  Wh-pronoun
WP$ Possessive wh-pronoun
WRB Wh-adverb
'''
train_text = state_union.raw("2005-GWBush.txt")
sample_text = state_union.raw("2006-GWBush.txt")

custom_sent_tokenizer = PunktSentenceTokenizer(train_text) #A

tokenized = custom_sent_tokenizer.tokenize(sample_text)   #B

def process_content():
try:
    for i in tokenized:
        words = nltk.word_tokenize(i)
        tagged = nltk.pos_tag(words)
        chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>} """
        chunkParser = nltk.RegexpParser(chunkGram)
        chunked = chunkParser.parse(tagged)

        chunked.draw()

except Exception as e:
        print(str(e))

process_content()

1 个答案:

答案 0 :(得分:0)

Python 2.7版没有提供这样的功能,我安装了Python 3.6,现在chunk.draw()工作正常。