pythonpython-2.7nlpnltknltk-book

'str' object is not callable in NLTK


import re
import nltk
import pandas as pd
from nltk.chunk import RegexpParser
from textblob import TextBlob
data = open('data.txt', 'r')
data = data.read()

# region Fetch Account Type
chunkData = r"""DataChunk: {(<NNP><NNP>+<CD>+)}
  }<JJ|IN|DT|TO>+{"""



lines = [line for line in open('data.txt')]
lstLines=data.split('|')
dataLines=[]
for lines in lstLines:
    dataLines=lines.split("\n")

for line in dataLines:
    if 'Data' in line:
        DataTags = TextBlob(line).tags
        Datachunker = RegexpParser(chunkData)
        Datachunked = Datachunker.parse(DataTags)

        for chunk in Datachunked:

                if type(chunk) == nltk.tree.Tree and chunk.label() == "DataChunk":
                    DatachunkedLst = chunk.leaves()
                    Datachunked = [leaf[0] for leaf in DatachunkedLst if leaf[1] == 'CD']
                    Data = '/'.join(Datachunked)

Error:if type(chunk) == nltk.tree.Tree and chunk.label() == "DataChunk": TypeError: 'str' object is not callable

However i am able to print chunk.label()


Solution

  • I suspect that the following is happening:

    Somewhere in your code – it's not included in your snippet, though – you assign a string value to a variable called type, eg.:

    type = "context-free"
    

    This gives exactly your error message:

    >>> type = "context-free"
    >>> type(object())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object is not callable
    

    I think it's quite likely that this is happening here; at least it frequently occurs to me that I want to store some kind of "type" in a variable, but I usually refrain from that because of the kind of trouble you see here.

    One way to avoid bugs like that is using a linter: A tool that is run in the background by your editor, checking your code and spotting dangerous stuff just like this. I highly recommend you try one!