2017-05-29 118 views
0
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“海峡”对象不在NLTK调用

但是我能够打印chunk.label()

+0

你好维奈,请考虑写你想达到的目标。否则,我们只能猜测。 –

+0

请提供一些数据,你是从的data.txt文件 –

+0

读你有没有通过任何机会,一个字符串值赋给一个名为'type'变量?如'type =“无概念”'? – lenz

回答

0

我怀疑下面发生的事情:

某处在你的代码 - 它不包括在您的代码段 - 尽管 - 您将字符串值分配给名为type的变量,例如:

type = "context-free" 

这给出了你的错误信息:

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

我认为这很有可能在这里发生;至少我经常想到我想在某个变量中存储某种“类型”,但由于您在这里看到的那种麻烦,我通常不会这样做。为了避免这样的错误

一种方法是使用棉绒:即在后台通过您的编辑器中运行的工具,检查你的代码和点状出血危险的东西,就是这样的。我强烈建议你尝试一个!

+0

但块是不串的人,之前检查是否conditon –

+0

我从来没有说过'chunk'是一个字符串(或“串的人”,不管这是)。我正在谈论内置函数'type',它可以被重写。 – lenz

相关问题