如何在NLTK中获取文本的主观性分数?

问题描述 投票:2回答:1

我需要NLTK中的一种方法来计算文本主观性的分数(实数)。NLTK中有没有这样的方法?

some_magic_method(my_text):
    ...

# 0.34
machine-learning nltk sentiment-analysis
1个回答
0
投票

简单的谷歌搜索可以得到 https:/www.nltk.orgapinltk.sentiment.html 其中有一个主观性预测因子。这是在情怀的背景下,如果你是从脱离情怀的东西,你可以看看Pang和Lee 2004的数据集。用一个简单的计数向量化的SVM,我对它的准确率达到了90%。这里是一段定义类的代码(来自我的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

0
投票

简短的回答是 "没有"。目前,在 NLTK 的数值,产生一个 subjectivity. 唯一能报告主观性数值的软件包是 TextBlob.

也就是说,该模块 nltk.sentiment.util.demo_sent_subjectivity() 报告的主观性,使用的数据集由 Pang和Lee(2004年) 包含5000条主观和5000条客观处理后的影评。我说过,与textblob不同,这个模块只识别语句(或词袋),要么是 subjectiveobjective 并没有给它们分配一个数值。

虽然没有明确提到默认的分类器,但我 "认为 "这个模块使用了一个天真的贝叶斯分类器,这个分类器是可以改变的。你可以找到这个模块的文档 此处. 还有: 此处 是一个例子。NLTK.

© www.soinside.com 2019 - 2024. All rights reserved.