我正在构建一个情感分析算法,可以对.txt语料库中的分段进行分段,但代码中存在一些问题,我不知道如何解决?
class Splitter(object):
def _init_(self):
self.nltk_splitter = nltk.data.load('tokenizers/punkt/english/pickle')
self.nltk_tokenizer = nltk.tokenize.TreebankWordTokenizer()
def split(self,text):
"""imput format: a .txt file
output format : a list of lists of words.
for eg [['this', 'is']['life' , 'worth' , 'living']]"""
sentences = self.nltk_splitter.tokenize(text)
tokenized_sentences = [self.nltk_tokenizer.tokenize(sent) for sent in sentences]
return tokenized_sentences
然后我做了以下事情
>>> f = open('amazonshoes.txt')
>>> raw = f.read()
>>> text = nltk.Text(raw)
>>> splitter = Splitter()
>>> splitted_sentences = splitter.split(text)
,错误是
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
splitted_sentences = splitter.split(text)
File "<pyshell#14>", line 9, in split
sentences = self.nltk_splitter.tokenize(text)
AttributeError: 'Splitter' object has no attribute 'nltk_splitter'
答案 0 :(得分:2)
类Splitter
的构造函数应该被称为__init__
,带有两个前导和尾部下划线。
目前未执行_init_
方法(单个下划线),因此您创建的Splitter
对象(通过调用Splitter()
)永远不会获取属性/字段nltk_splitter
< / p>