Search code examples
pythonnltk

I'm getting this error: filter object at 0x041A7F28 error when I run this program listed below


I'm trying to get this program to print the verbs, I get the same error in the interpreter and as a program ran from the command line. Any help would be great! Thank you in advance!

  from __future__ import print_function
import nltk
from nltk.tokenize import word_tokenize

with open ('words.txt') as fin
    for line in fin:

        tokens = word_tokenize(line)

pos_tagged = nltk.pos_tag(tokens)

print(pos_tagged)

verbs = filter(lambda x:x[1]=='VB',pos_tagged)

print(verbs)

output:

[('German', 'JJ'),... ('has', 'VBZ'), ('shown.3', 'VBN')]

error: <filter object at 0x041A7F28>


Solution

  • <filter object at 0x041A7F28>
    

    That's not an error. That's how filter objects print.

    To get the results of the filter, you have to iterate over the filter object. The easiest way to do that is to pass it to list():

    verbs = list(filter(lambda x:x[1]=='VB',pos_tagged))