AttributeError: 'tokenizer' object has no attribute 'sent_tokenize'
本问题已经有最佳答案,请猛点这里访问。
当我运行以下代码时,会出现以下错误:
为什么会给我那个错误?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from nltk.tokenize import sent_tokenize, \word_tokenize, WordPunctTokenizer from nltk.corpus import brown class tokenizer: input_data='' def __init__(self, input_data='', ): self.input_data = input_data def gen_SentTokenizers(self,input_data): sentence_token= self.sent_tokenize(input_data) # Error! return sentence_token def gen_WordTokenizers(self, input_data): word_token=self.word_tokenize(input_data) return word_token def gen_WordPunctToken(self, input_data): word_PunctToken=self.WordPunctTokenizer().tokenize(input_data) return word_PunctToken if __name__=='__main__': input_data = ' '.join(brown.words()) token1 = tokenizer() token2 = tokenizer() token3 = tokenizer() token1.gen_SentTokenizers(input_data) print(" Sentence tokenizer:") print(token1) token2.gen_WordPunctToken(input_data) print(" Word punct tokenizer:") print(token2) token3.gen_WordTokenizers(input_data) print(" Word tokenizer:") print(token3) |
终端上:
1 2 | pip install --upgrade nltk python -m nltk.downloader popular |
在代码中:
1 2 3 | from nltk.tokenize import sent_tokenize text = 'This str has many sentences. A sentence here. A sentence there.' sent_tokenize(text) |