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)
错误:如果type(chunk)== nltk.tree.Tree和chunk.label()==&#34; DataChunk&#34;: TypeError:&#39; str&#39;对象不可调用
但是我可以打印chunk.label()
答案 0 :(得分: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
我认为这很有可能发生在这里;至少它经常发生在我身上,我想存储某种类型的&#34;类型&#34;在变量中,但我通常会避免这种情况,因为你在这里遇到了麻烦。
避免类似错误的一种方法是使用linter:由编辑器在后台运行的工具,检查代码并发现危险的东西,就像这样。我强烈建议你尝试一下!