here was my post back to you:
Thanks for the clarification.
If I were adding code like this:
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()
how would I write it instead using your method with:
def process_content(tokenized):
for i in tokenized:
words = nltk.word_tokenize(i)
tagged = pos_tag(words)
print(tagged)
if __name__ == '__main__':
train_text = state_union.raw("2005-GWBush.txt")
sample_text = state_union.raw("2006-GWBush.txt")
custom_sent_tokenizer = PunktSentenceTokenizer(train_text)
tokenized = custom_sent_tokenizer.tokenize(sample_text)
process_content(tokenized)
by the way, this is where "chunked.draw()" was failing and I got neither an output nor an error to the screen because I'm not sure if my matplotlib path was correct.
however, my diagram randomly appeared a few minutes ago out of nowhere
EDIT: when I try to move or resize the window in which the diagram appears from matplotlib, I get an error that "Python has quit unexpectedly". Shieettttt
EDIT2: I'm not so concerned with the window as I am adding my proposed addition above to the coding so nice provided by snippsat.
Here's my attempt:
import nltk
from nltk import pos_tag, PunktSentenceTokenizer
from nltk.corpus import state_union
def process_content(tokenized):
for i in tokenized:
words = nltk.word_tokenize(i)
tagged = pos_tag(words)
print(tagged)
if __name__ == '__main__':
train_text = state_union.raw("2005-GWBush.txt")
sample_text = state_union.raw("2006-GWBush.txt")
custom_sent_tokenizer = PunktSentenceTokenizer(train_text)
tokenized = custom_sent_tokenizer.tokenize(sample_text)
chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>?}"""
chunkParser = nltk.RegexpParser(chunkGram)
chunked = chunkParser.parse(tagged)
chunked.draw()
process_content(tokenized)
Error:
[b][i][color=#cccccc][size=small][font=-apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', HelveticaNeue-Light, Ubuntu, 'Droid Sans', sans-serif][size=x-small][font=Monaco, Menlo, Consolas, 'Droid Sans Mono', Inconsolata, 'Courier New', monospace, 'Droid Sans Fallback']Traceback (most recent call last):[/font][/size][/font][/size][/color][/i][/b]
[b][i][color=#cccccc][size=small][font=-apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', HelveticaNeue-Light, Ubuntu, 'Droid Sans', sans-serif][size=x-small][font=Monaco, Menlo, Consolas, 'Droid Sans Mono', Inconsolata, 'Courier New', monospace, 'Droid Sans Fallback'] File "/Users/jordanXXX/Documents/NLP/chunking2", line 20, in <module>[/font][/size][/font][/size][/color][/i][/b]
[b][i][color=#cccccc][size=small][font=-apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', HelveticaNeue-Light, Ubuntu, 'Droid Sans', sans-serif][size=x-small][font=Monaco, Menlo, Consolas, 'Droid Sans Mono', Inconsolata, 'Courier New', monospace, 'Droid Sans Fallback'] chunked = chunkParser.parse(tagged)[/font][/size][/font][/size][/color][/i][/b]
[color=#cccccc][size=small][font=-apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', HelveticaNeue-Light, Ubuntu, 'Droid Sans', sans-serif][size=x-small][font=Monaco, Menlo, Consolas, 'Droid Sans Mono', Inconsolata, 'Courier New', monospace, 'Droid Sans Fallback'][b][i]NameError: name 'tagged' is not defined[/i][/b][/font][/size][/font][/size][/color]