【发布时间】:2020-04-23 09:44:08
【问题描述】:
我需要 NLTK 中的一种方法来计算文本主观性的分数(实数)。 NLTK 中有类似的东西吗?
some_magic_method(my_text):
...
# 0.34
【问题讨论】:
标签: machine-learning nltk sentiment-analysis
我需要 NLTK 中的一种方法来计算文本主观性的分数(实数)。 NLTK 中有类似的东西吗?
some_magic_method(my_text):
...
# 0.34
【问题讨论】:
标签: machine-learning nltk sentiment-analysis
简短的回答是“不”。目前,NLTK 中没有为subjectivity 生成数值的方法。唯一报告主观性数值的包是TextBlob。
也就是说,nltk.sentiment.util.demo_sent_subjectivity() 模块使用由Pang and Lee (2004) 开发的数据集报告主观性,其中包含 5000 条主观和 5000 条客观处理的电影评论。正如我所说,与 textblob 不同的是,此模块仅将语句(或词袋)标识为 subjective 或 objective,并且不会为它们分配数值。
虽然没有明确提到默认分类器,但我“认为”这个模块使用了一个朴素的贝叶斯分类器,它可以改变。你可以找到这个模块的文档here。此外,here 是NLTK 提供的一个示例。
【讨论】:
一个简单的谷歌搜索产生https://www.nltk.org/api/nltk.sentiment.html,它有一个主观预测器。在情绪的背景下,如果您从与情绪分离的东西中寻找,您可以查看 Pang 和 Lee 2004 数据集。使用一个简单的计数向量化 SVM,我得到了 90% 的准确率。这是定义类的代码的 sn-p(来自我的 GitHub),如果您想要整个代码,我可以提供更多。
class ObjectivityDetector():
'''SVM predicts the objectivity/subjectivity of a sentence. Trained on pang/lee 2004 with NER removal. Pre-grid searched and 5 fold validated and has a 90% accuracy and 0.89 F1 macro'''
def __init__(self,train,model_file=None):
self.pipeline = Pipeline(
[
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', CalibratedClassifierCV( #calibrated CV wrapping SGD to get probability outputs
SGDClassifier(
loss='hinge',
penalty='l2',
alpha=1e-4,
max_iter=1000,
learning_rate='optimal',
tol=None,),
cv=5)),
]
)
self.train(train)
def train(self,train):
learner = self.pipeline.fit(train['text'],train['truth'])
self.learner = learner
def predict(self,test):
predicted = self.learner.predict(test)
probs = self.learner.predict_proba(test)
certainty = certainty_(probs)
return predicted,certainty
def score(self,predicted,test):
acc = accuracy_score(test['truth'].to_numpy(),predicted[0])*100
f1 = f1_score(test['truth'].to_numpy(),predicted[0], average='macro')
print("Accuracy: {}\nMacro F1-score: {}".format(acc, f1))
return acc,f1
【讨论】: