亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在NLTK / Python中使用電影評論語料庫進行分類

在NLTK / Python中使用電影評論語料庫進行分類

慕無忌1623718 2019-11-19 11:00:54
我希望根據NLTK第6章進行分類。這本書似乎跳過了創建類別的步驟,而且我不確定自己做錯了什么。我在這里有以下響應的腳本。我的問題主要來自第一部分-基于目錄名稱的類別創建。此處的其他一些問題使用了文件名(即pos_1.txt和neg_1.txt),但我希望創建可以將文件轉儲到的目錄。from nltk.corpus import movie_reviewsreviews = CategorizedPlaintextCorpusReader('./nltk_data/corpora/movie_reviews', r'(\w+)/*.txt', cat_pattern=r'/(\w+)/.txt')reviews.categories()['pos', 'neg']documents = [(list(movie_reviews.words(fileid)), category)            for category in movie_reviews.categories()            for fileid in movie_reviews.fileids(category)]all_words=nltk.FreqDist(    w.lower()     for w in movie_reviews.words()     if w.lower() not in nltk.corpus.stopwords.words('english') and w.lower() not in  string.punctuation)word_features = all_words.keys()[:100]def document_features(document):     document_words = set(document)     features = {}    for word in word_features:        features['contains(%s)' % word] = (word in document_words)    return featuresprint document_features(movie_reviews.words('pos/11.txt'))featuresets = [(document_features(d), c) for (d,c) in documents]train_set, test_set = featuresets[100:], featuresets[:100]classifier = nltk.NaiveBayesClassifier.train(train_set)print nltk.classify.accuracy(classifier, test_set)classifier.show_most_informative_features(5)返回:File "test.py", line 38, in <module>    for w in movie_reviews.words()File "/usr/local/lib/python2.6/dist-packages/nltk/corpus/reader/plaintext.py", line 184, in words    self, self._resolve(fileids, categories))File "/usr/local/lib/python2.6/dist-packages/nltk/corpus/reader/plaintext.py", line 91, in words    in self.abspaths(fileids, True, True)])File "/usr/local/lib/python2.6/dist-packages/nltk/corpus/reader/util.py", line 421, in concat    raise ValueError('concat() expects at least one object!')ValueError: concat() expects at least one object!
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 1358 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號