pattern
Cameron Simpson
cs at cskk.id.au
Sat Jun 16 18:33:33 EDT 2018
On 16Jun2018 11:59, Sharan Basappa <sharan.basappa at gmail.com> wrote:
>This is so kind of you. Thanks for spending time to explain the code.
>It did help a lot. I did go back and brush up lists & dictionaries.
>
>At this point, I think, I need to go back and brush up Python from the start.
>So, I will do that first.
Sure, sounds good.
But write code! It is not enough to read code and read about code. You need to
write code and modify code. Otherwise the skills don't internalise well.
If you're running the code you asked about, one way to learn a lot about
something that looks obscrure is simply to put in print() calls at various
places, eg:
print("iterate over traing_data =", repr(training_data))
for pattern in training_data:
# tokenize each word in the sentence
print("pattern =", repr(pattern))
w = nltk.word_tokenize(pattern['sentence'])
print("w =", repr(w))
# add to our words list
words.extend(w)
print("words =", repr(words))
# add to documents in our corpus
documents.append((w, pattern['class']))
print("documents =", repr(documents))
Note the use of repr(): it will print out the structure of lists and so forth,
very useful.
Just reviewing that loop, the logic does look a little weird to me. I think the
"documents.append" should be inside the loop because otherwise it only accrues
the _last_ "w" and "pattern".
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list