Thứ nhất, đọc những câu trả lời một cách cẩn thận, chúng chứa các bộ phận của câu trả lời bạn cần và cũng một thời gian ngắn giải thích những gì phân loại làm và cách thức hoạt động trong NLTK:
Kiểm tra phân loại trên dữ liệu chú thích
Bây giờ để trả lời câu hỏi của bạn. Chúng tôi giả định rằng câu hỏi của bạn là theo dõi câu hỏi này: Using my own corpus instead of movie_reviews corpus for Classification in NLTK
Nếu văn bản thử nghiệm của bạn được cấu trúc giống như số liệu movie_review
, bạn có thể đọc dữ liệu thử nghiệm như bạn muốn cho dữ liệu đào tạo:
Chỉ trong trường hợp lời giải thích của mã này là không rõ ràng, đây là một hướng:
traindir = '/home/alvas/my_movie_reviews'
mr = CategorizedPlaintextCorpusReader(traindir, r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*', encoding='ascii')
hai dòng trên là để đọc một thư mục my_movie_reviews
với một cấu trúc như vậy:
\my_movie_reviews
\pos
123.txt
234.txt
\neg
456.txt
789.txt
README
Sau đó, dòng tiếp theo trích xuất tài liệu với thẻ pos/neg
của nó là một phần của cấu trúc thư mục.
documents = [([w for w in mr.words(i) if w.lower() not in stop and w not in string.punctuation], i.split('/')[0]) for i in mr.fileids()]
Dưới đây là lời giải thích cho các dòng trên:
# This extracts the pos/neg tag
labels = [i for i.split('/')[0]) for i in mr.fileids()]
# Reads the words from the corpus through the CategorizedPlaintextCorpusReader object
words = [w for w in mr.words(i)]
# Removes the stopwords
words = [w for w in mr.words(i) if w.lower() not in stop]
# Removes the punctuation
words = [w for w in mr.words(i) w not in string.punctuation]
# Removes the stopwords and punctuations
words = [w for w in mr.words(i) if w.lower() not in stop and w not in string.punctuation]
# Removes the stopwords and punctuations and put them in a tuple with the pos/neg labels
documents = [([w for w in mr.words(i) if w.lower() not in stop and w not in string.punctuation], i.split('/')[0]) for i in mr.fileids()]
Quá trình CÙNG nên được áp dụng khi bạn đọc các dữ liệu thử nghiệm !!!
Bây giờ để xử lý tính năng:
Những dòng này thêm top 100 tính năng cho các phân loại:
# Extract the words features and put them into FreqDist
# object which records the no. of times each unique word occurs
word_features = FreqDist(chain(*[i for i,j in documents]))
# Cuts the FreqDist to the top 100 words in terms of their counts.
word_features = word_features.keys()[:100]
Next để xử lý các tài liệu vào classify-thể định dạng:
# Splits the training data into training size and testing size
numtrain = int(len(documents) * 90/100)
# Process the documents for training data
train_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag in documents[:numtrain]]
# Process the documents for testing data
test_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag in documents[numtrain:]]
Bây giờ, hãy giải thích rằng việc hiểu danh sách dài cho train_set
và `test_set:
# Take the first `numtrain` no. of documents
# as training documents
train_docs = documents[:numtrain]
# Takes the rest of the documents as test documents.
test_docs = documents[numtrain:]
# These extract the feature sets for the classifier
# please look at the full explanation on https://stackoverflow.com/questions/20827741/nltk-naivebayesclassifier-training-for-sentiment-analysis/
train_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag in train_docs]
Bạn cũng cần xử lý các tài liệu như trên để trích xuất tính năng trong tài liệu thử nghiệm !!!
Vì vậy, dưới đây là cách bạn có thể đọc dữ liệu thử nghiệm:
stop = stopwords.words('english')
# Reads the training data.
traindir = '/home/alvas/my_movie_reviews'
mr = CategorizedPlaintextCorpusReader(traindir, r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*', encoding='ascii')
# Converts training data into tuples of [(words,label), ...]
documents = [([w for w in mr.words(i) if w.lower() not in stop and w not in string.punctuation], i.split('/')[0]) for i in mr.fileids()]
# Now do the same for the testing data.
testdir = '/home/alvas/test_reviews'
mr_test = CategorizedPlaintextCorpusReader(testdir, r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*', encoding='ascii')
# Converts testing data into tuples of [(words,label), ...]
test_documents = [([w for w in mr_test.words(i) if w.lower() not in stop and w not in string.punctuation], i.split('/')[0]) for i in mr_test.fileids()]
Sau đó, tiếp tục với các bước xử lý mô tả ở trên, và chỉ cần làm điều này để có được những nhãn cho tài liệu kiểm tra như @yvespeirsman trả lời:
#### FOR TRAINING DATA ####
stop = stopwords.words('english')
# Reads the training data.
traindir = '/home/alvas/my_movie_reviews'
mr = CategorizedPlaintextCorpusReader(traindir, r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*', encoding='ascii')
# Converts training data into tuples of [(words,label), ...]
documents = [([w for w in mr.words(i) if w.lower() not in stop and w not in string.punctuation], i.split('/')[0]) for i in mr.fileids()]
# Extract training features.
word_features = FreqDist(chain(*[i for i,j in documents]))
word_features = word_features.keys()[:100]
# Assuming that you're using full data set
# since your test set is different.
train_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag in documents]
#### TRAINS THE TAGGER ####
# Train the tagger
classifier = NaiveBayesClassifier.train(train_set)
#### FOR TESTING DATA ####
# Now do the same reading and processing for the testing data.
testdir = '/home/alvas/test_reviews'
mr_test = CategorizedPlaintextCorpusReader(testdir, r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*', encoding='ascii')
# Converts testing data into tuples of [(words,label), ...]
test_documents = [([w for w in mr_test.words(i) if w.lower() not in stop and w not in string.punctuation], i.split('/')[0]) for i in mr_test.fileids()]
# Reads test data into features:
test_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag in test_documents]
#### Evaluate the classifier ####
for doc, gold_label in test_set:
tagged_label = classifier.classify(doc)
if tagged_label == gold_label:
print("Woohoo, correct")
else:
print("Boohoo, wrong")
Nếu mã và giải thích ở trên làm cho không có ý nghĩa với bạn, sau đó bạn pHẢI đọc hướng dẫn này trước khi tiếp tục: http://www.nltk.org/howto/classify.html
012.
Bây giờ chúng ta hãy nói rằng bạn không có chú thích trong dữ liệu thử nghiệm của bạn, tức là test.txt
của bạn không có trong cấu trúc thư mục như movie_review
và chỉ là một textfile đồng bằng:
\test_movie_reviews
\1.txt
\2.txt
Sau đó không có điểm trong việc đọc nó thành một corpus phân loại, bạn chỉ có thể làm đọc và tag các văn bản, ví dụ:
for infile in os.listdir(`test_movie_reviews):
for line in open(infile, 'r'):
tagged_label = classifier.classify(doc)
NHƯNG bạn kHÔNG tHỂ đánh giá kết quả mà không cần chú thích, vì vậy bạn không thể kiểm tra thẻ nếu if-else
, cũng bạn cần phải mã hóa văn bản nếu bạn không sử dụng CategorPlaintextCorpusReader.
Nếu bạn chỉ muốn gắn thẻ một tệp văn bản thuần test.txt
:
import string
from itertools import chain
from nltk.corpus import stopwords
from nltk.probability import FreqDist
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
from nltk import word_tokenize
stop = stopwords.words('english')
# Extracts the documents.
documents = [([w for w in movie_reviews.words(i) if w.lower() not in stop and w.lower() not in string.punctuation], i.split('/')[0]) for i in movie_reviews.fileids()]
# Extract the features.
word_features = FreqDist(chain(*[i for i,j in documents]))
word_features = word_features.keys()[:100]
# Converts documents to features.
train_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag in documents]
# Train the classifier.
classifier = NaiveBayesClassifier.train(train_set)
# Tag the test file.
with open('test.txt', 'r') as fin:
for test_sentence in fin:
# Tokenize the line.
doc = word_tokenize(test_sentence.lower())
featurized_doc = {i:(i in doc) for i in word_features}
tagged_label = classifier.classify(featurized_doc)
print(tagged_label)
Một lần nữa, xin đừng chỉ cần sao chép và dán các giải pháp và cố gắng tìm hiểu lý do tại sao và làm thế nào nó hoạt động.
Cảm ơn bạn đã giải thích đầy đủ và tôi cố gắng hiểu chúng. Nhưng tôi thường gặp phải kết quả sai. Tôi có nghĩa là nó nên được 'pos' nhưng chương trình hiển thị' neg'. Và tôi không biết lý do. – ZaM
Có nhiều lý do và nó không hoàn hảo, có thể (i) dữ liệu không đủ, (ii) các tính năng không đủ tốt, (iii) lựa chọn phân loại, v.v. Thực hiện khóa học này https://www.coursera.org/ khóa học/ml để biết thêm thông tin. Và nếu bạn có thể mạnh mẽ khuyến khích bạn tham dự http://lxmls.it.pt/2015/ – alvas
Tôi đang bối rối. Đầu tiên tôi lấy một tập tin từ 'nltk/movie_reviews/neg/cv081.txt'. Sau đó, tôi quyết định ** thử nghiệm ** tệp của chương trình cung cấp cho 'Woohoo, correct'or'wrong'. Tôi đặt tập tin trong '/home/neg/cv081.txt' để thử nghiệm sau đó tôi nhận được' Boohoo, sai'as đầu ra! Sau đó, tôi đặt các tập tin trong '/ home/pos/cv081.txt' sau đó tôi nhận được' Woohoo, correct' là đầu ra! Sau đó, tôi kiểm tra cùng một tệp trên chương trình 'print (tagged_label)' nó cho tôi nhiều 'neg'. Và về chương trình mà 'print (tagged_label)'. Tôi không biết chính xác nó hoạt động ra sao. Nó cho tôi nhiều 'neg' ngay cả đối với tập tin' pos' !!. Làm thế nào tôi có thể đánh giá các đầu ra 'neg' và 'pos'. – ZaM