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() == "DataChunk": TypeError: 'str' object is not callable
但是我可以打印chunk.label()