如何在NLTK中创建情感分析语料库



我希望在Visual Studio Code for MacOSX中使用我自己创建的语料库;我已经阅读了大约100个论坛,我不知道我做错了什么,因为我是一个非常新的编程。

这个问题似乎是我能找到的最接近我需要做的事情;然而,我不知道如何做到以下几点:

例如,在Mac上,它将位于~/nltk_data/corpus中。看起来你还必须将你的新语料库附加到…/site-packages/nltk/corpus/中的__init__.py中。"

回答时,请注意我正在使用Homebrew,如果我需要在相同的编码中使用股票NLTK语料库数据集,则不想永久禁用使用另一条路径。

如果需要,我可以使用"PlaintextCorpusReader"以及下面提供的回溯来发布我的编码尝试,尽管我宁愿不使用PlaintextCorpusReader来无缝使用,而宁愿只使用简单的复制+粘贴。txt文件到我希望根据追加编码使用的适当位置。

谢谢。

Traceback (most recent call last):
  File "/Users/jordanXXX/Documents/NLP/bettertrainingdata", line 42, in <module>
    short_pos = open("short_reviews/pos.txt", "r").read
IOError: [Errno 2] No such file or directory: 'short_reviews/pos.txt'


编辑:


感谢您的回复。

我接受了你的建议,把这个文件夹从NLTK的语料库中移了出来。

我一直在尝试我的文件夹位置,我得到了不同的回溯。

如果你说最好的方法是使用PlaintextCorpusReader,那就这样吧;然而,也许对于我的应用程序,我想使用CategorizedPlaintextCorpusReader?

系统。argv绝对不是我的意思,所以我可以稍后再阅读。

首先,这是我的代码,没有尝试使用PlaintextCorpusReader,当包含poss .txt和negg .txt文件的文件夹"short_reviews"在NLP文件夹之外时,会导致上面的回溯:

import nltk
import random
from nltk.corpus import movie_reviews
from nltk.classify.scikitlearn import SklearnClassifier
import pickle
from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from nltk.classify import ClassifierI
from statistics import mode
from nltk import word_tokenize
class VoteClassifier(ClassifierI):
    def __init__(self, *classifiers):
        self._classifiers = classifiers
    def classify(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)
            votes.append(v)
        return mode(votes)
    def confidence(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)
            votes.append(v)
        choice_votes = votes.count(mode(votes))
        conf = choice_votes / len(votes)
        return conf
# def main():
#     file = open("short_reviews/pos.txt", "r")
#     short_pos = file.readlines()
#     file.close
short_pos = open("short_reviews/pos.txt", "r").read
short_neg = open("short_reviews/neg.txt", "r").read
documents = []
for r in short_pos.split('n'):
    documents.append( (r, "pos") )
for r in short_neg.split('n'):
    documents.append((r, "neg"))
all_words = []
short_pos_words = word.tokenize(short_pos)
short_neg_words = word.tokenize(short_neg)
for w in short_pos_words:
    all_words.append(w. lower())
for w in short_neg_words:
    all_words.append(w. lower())
all_words = nltk.FreqDist(all_words)

但是,当我使用与上面相同的代码将包含文本文件的文件夹"short_reviews"移动到NLP文件夹中时,但不使用PlaintextCorpusReader,会发生以下情况:

Traceback (most recent call last):
  File "/Users/jordanXXX/Documents/NLP/bettertrainingdata", line 47, in <module>
    for r in short_pos.split('n'):
AttributeError: 'builtin_function_or_method' object has no attribute 'split'

当我使用下面的代码将包含文本文件的文件夹"short_reviews"移动到NLP文件夹时,使用了PlaintextCorpusReader,出现了以下回溯:

import nltk
import random
from nltk.corpus import movie_reviews
from nltk.classify.scikitlearn import SklearnClassifier
import pickle
from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from nltk.classify import ClassifierI
from statistics import mode
from nltk import word_tokenize
from nltk.corpus import PlaintextCorpusReader
corpus_root = 'short_reviews'
word_lists = PlaintextCorpusReader(corpus_root, '*')
wordlists.fileids()

class VoteClassifier(ClassifierI):
    def __init__(self, *classifiers):
        self._classifiers = classifiers
    def classify(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)
            votes.append(v)
        return mode(votes)
    def confidence(self, features):
        votes = []
        for c in self._classifiers:
            v = c.classify(features)
            votes.append(v)
        choice_votes = votes.count(mode(votes))
        conf = choice_votes / len(votes)
        return conf
# def main():
#     file = open("short_reviews/pos.txt", "r")
#     short_pos = file.readlines()
#     file.close
short_pos = open("short_reviews/pos.txt", "r").read
short_neg = open("short_reviews/neg.txt", "r").read
documents = []
for r in short_pos.split('n'):
    documents.append((r, "pos"))
for r in short_neg.split('n'):
    documents.append((r, "neg"))
all_words = []
short_pos_words = word.tokenize(short_pos)
short_neg_words = word.tokenize(short_neg)
for w in short_pos_words:
    all_words.append(w. lower())
for w in short_neg_words:
    all_words.append(w. lower())
all_words = nltk.FreqDist(all_words)

Traceback (most recent call last):
  File "/Users/jordanXXX/Documents/NLP/bettertrainingdata2", line 18, in <module>
    word_lists = PlaintextCorpusReader(corpus_root, '*')
  File "/Library/Python/2.7/site-packages/nltk/corpus/reader/plaintext.py", line 62, in __init__
    CorpusReader.__init__(self, root, fileids, encoding)
  File "/Library/Python/2.7/site-packages/nltk/corpus/reader/api.py", line 87, in __init__
    fileids = find_corpus_fileids(root, fileids)
  File "/Library/Python/2.7/site-packages/nltk/corpus/reader/util.py", line 763, in find_corpus_fileids
    if re.match(regexp, prefix+fileid)]
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 141, in match
    return _compile(pattern, flags).match(string)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 251, in _compile
    raise error, v # invalid expression
error: nothing to repeat

您提到的答案包含一些非常糟糕(或者更确切地说,不适用)的建议。没有理由将自己的语料库放在nltk_data中,或者破解nltk.corpus.__init__.py以像本地语料库一样加载它。实际上,做这些事情。

你应该使用PlaintextCorpusReader。我不理解您不愿意这样做,但是如果您的文件是纯文本的,那么使用它是正确的工具。假设您有一个文件夹NLP/bettertrainingdata,您可以构建一个读取器,它将加载该文件夹中的所有.txt文件,如下所示:

myreader = nltk.corpus.reader.PlaintextCorpusReader(r"NLP/bettertrainingdata", r".*.txt")

如果你添加新的文件到文件夹,读者会发现并使用它们。如果您想要的是能够将您的脚本与其他文件夹一起使用,那么就这样做吧——您不需要一个不同的阅读器,您需要了解sys.argv。如果你想要一个有pos.txtneg.txt的分类语料库,那么你需要一个CategorizedPlaintextCorpusReader(见)。如果你想要的是别的东西,那么请编辑你的问题来解释你想要做什么

最新更新