3

我正在尝试使用 Python 的 NLTK 模块对 NLP 进行第一次尝试,以对选定的推文进行情绪分析。我一直在关注本教程并下载了Sentiment140 推文语料库作为我的训练数据集,因为这仅用于教育目的(包含约 160 万条手分类推文)。

我的代码可以在这里找到。请注意,这是在 Python 2 中的 iPython Notebook 中完成的。

问题第 1 部分 我正在使用训练集中的 10,000 行测试我的代码,以查看它是否在输入所有 1.6m 行之前工作。当我运行代码时,第 96 行返回 None:

print classifier.show_most_informative_features(32)
Most Informative Features
None

但是,本教程建议我应该看到如下内容:

Most Informative Features
contains(not) = False          positi : negati =      1.6 : 1.0
contains(tired) = False          positi : negati =      1.2 : 1.0
contains(excited) = False          negati : positi =      1.2 : 1.0
contains(great) = False          negati : positi =      1.2 : 1.0
contains(looking) = False          positi : negati =      1.2 : 1.0
contains(like) = False          positi : negati =      1.2 : 1.0
contains(love) = False          negati : positi =      1.2 : 1.0
contains(amazing) = False          negati : positi =      1.2 : 1.0
contains(enemy) = False          positi : negati =      1.2 : 1.0
contains(about) = False          negati : positi =      1.2 : 1.0
contains(best) = False          negati : positi =      1.2 : 1.0
contains(forward) = False          positi : negati =      1.2 : 1.0
contains(friend) = False          negati : positi =      1.2 : 1.0
contains(horrible) = False          positi : negati =      1.2 : 1.0

我使用第 96 行作为指标来告诉我分类器是否有效。就我已经尝试过的修复而言:我在教程中看到一条评论,建议第 87 行应该是:

training_set = nltk.classify.util.apply_features(extract_features, tweets)

而不是现在的样子:

training_set = nltk.classify.apply_features(extract_features, tweets)

我尝试了这两种变体。

在我运行完整的 1.6m 行数据集以训练分类器之前,我想解决这个问题。

以下是我对笔记本的所有导入语句(一些导入语句用于笔记本的其他区域):

import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
from pandas.tseries.resample import TimeGrouper
from pandas.tseries.offsets import DateOffset
import nltk
from nltk.corpus import stopwords
from nltk import FreqDist
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import subjectivity
from nltk.sentiment import SentimentAnalyzer
import nltk.sentiment.util
import csv
import re
import networkx as nx
import time
%matplotlib inline
%pylab inline

问题第 2 部分 我如何调整此代码以返回极性分数本身。就像是:

compound: -0.6759, neg: 0.41, neu: 0.59, pos: 0.0

基于这个 NLTK page,我似乎会调用 .polarity_socres() 方法,但我不确定我什至会在我的代码中的哪个位置这样做。这是他们返回上述内容的代码:

sid = SentimentIntensityAnalyzer()
for sentence in sentences:
    print(sentence)
    ss = sid.polarity_scores(sentence)
    for k in sorted(ss):
        print('{0}: {1}, '.format(k, ss[k]), end='')
4

0 回答 0