- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个脚本,可以确定一个句子是积极的、中性的还是消极的。
我上网搜索发现通过medium article可以使用 NLTK 库来完成。
所以,我已经尝试过这段代码。
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
def extract_features(word_list):
return dict([(word, True) for word in word_list])
if __name__ == '__main__':
# Load positive and negative reviews
positive_fileids = movie_reviews.fileids('pos')
negative_fileids = movie_reviews.fileids('neg')
features_positive = [(extract_features(movie_reviews.words(fileids=[f])),
'Positive') for f in positive_fileids]
features_negative = [(extract_features(movie_reviews.words(fileids=[f])),
'Negative') for f in negative_fileids]
# Split the data into train and test (80/20)
threshold_factor = 0.8
threshold_positive = int(threshold_factor * len(features_positive))
threshold_negative = int(threshold_factor * len(features_negative))
features_train = features_positive[:threshold_positive] + features_negative[:threshold_negative]
features_test = features_positive[threshold_positive:] + features_negative[threshold_negative:]
print("\nNumber of training datapoints:", len(features_train))
print("Number of test datapoints:", len(features_test))
# Train a Naive Bayes classifier
classifier = NaiveBayesClassifier.train(features_train)
print("\nAccuracy of the classifier:", nltk.classify.util.accuracy(classifier, features_test))
print("\nTop 10 most informative words:")
for item in classifier.most_informative_features()[:10]:
print(item[0])
# Sample input reviews
input_reviews = [
"Started off as the greatest series of all time, but had the worst ending of all time.",
"Exquisite. 'Big Little Lies' takes us to an incredible journey with its emotional and intriguing storyline.",
"I love Brooklyn 99 so much. It has the best crew ever!!",
"The Big Bang Theory and to me it's one of the best written sitcoms currently on network TV.",
"'Friends' is simply the best series ever aired. The acting is amazing.",
"SUITS is smart, sassy, clever, sophisticated, timely and immensely entertaining!",
"Cumberbatch is a fantastic choice for Sherlock Holmes-he is physically right (he fits the traditional reading of the character) and he is a damn good actor",
"What sounds like a typical agent hunting serial killer, surprises with great characters, surprising turning points and amazing cast."
"This is one of the most magical things I have ever had the fortune of viewing.",
"I don't recommend watching this at all!"
]
print("\nPredictions:")
for review in input_reviews:
print("\nReview:", review)
probdist = classifier.prob_classify(extract_features(review.split()))
pred_sentiment = probdist.max()
print("Predicted sentiment:", pred_sentiment)
print("Probability:", round(probdist.prob(pred_sentiment), 2))
这是我得到的输出
Number of training datapoints: 1600
Number of test datapoints: 400
Accuracy of the classifier: 0.735
Top 10 most informative words:
outstanding
insulting
vulnerable
ludicrous
uninvolving
avoids
astounding
fascination
affecting
seagal
Predictions:
Review: Started off as the greatest series of all time, but had the worst ending of all time.
Predicted sentiment: Negative
Probability: 0.64
Review: Exquisite. 'Big Little Lies' takes us to an incredible journey with its emotional and intriguing storyline.
Predicted sentiment: Positive
Probability: 0.89
Review: I love Brooklyn 99 so much. It has the best crew ever!!
Predicted sentiment: Negative
Probability: 0.51
Review: The Big Bang Theory and to me it's one of the best written sitcoms currently on network TV.
Predicted sentiment: Positive
Probability: 0.62
Review: 'Friends' is simply the best series ever aired. The acting is amazing.
Predicted sentiment: Positive
Probability: 0.55
Review: SUITS is smart, sassy, clever, sophisticated, timely and immensely entertaining!
Predicted sentiment: Positive
Probability: 0.82
Review: Cumberbatch is a fantastic choice for Sherlock Holmes-he is physically right (he fits the traditional reading of the character) and he is a damn good actor
Predicted sentiment: Positive
Probability: 1.0
Review: What sounds like a typical agent hunting serial killer, surprises with great characters, surprising turning points and amazing cast.This is one of the most magical things I have ever had the fortune of viewing.
Predicted sentiment: Positive
Probability: 0.95
Review: I don't recommend watching this at all!
Predicted sentiment: Negative
Probability: 0.53
Process finished with exit code 0
我面临的问题是数据集非常有限,因此输出精度非常低。有没有更好的库或资源或其他任何东西来检查一个陈述是积极的、中立的还是消极的?
更具体地说,我想将其应用于一般的日常谈话
最佳答案
亚马逊客户评论数据集是一个庞大的数据集,包含 1.3 亿多条客户评论。您可以通过匹配评论和评级来使用它进行情感分析。这么多的数据也非常适合 super 花哨的数据密集型深度学习方法。
( https://s3.amazonaws.com/amazon-reviews-pds/readme.html )
如果您特别寻找电影评论,大型电影评论数据集也是一个选择,其中包含 50K+ IMDB 评论。(http://ai.stanford.edu/~amaas/data/sentiment/)
我建议使用词嵌入来增强您的模型,而不是使用一次性编码的词袋。
关于python - 判断一个句子是肯定的、中性的还是否定的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59521135/
假设我有一个数据集,每行包含一个句子,该句子来自一个非常大的调查(德语和法语)中的一个开放式问题。大多数句子(答案)是合乎逻辑的;即有意义的单词组合。但是,也有一些粗心的受访者只是简单地填写了各种不合
我的 MySQL 数据库中有一个表,其结构如下: CREATE TABLE `papers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varch
在新的 Edge 浏览器(基于 chromium)中,有一个文本到语音的选项,在阅读页面时它会突出显示正在阅读的句子和单词,就像这样 - 过去我有一个简单的 Windows TTS 应用程序,我通过将
我有一本书的图像文件。我正在编写一个 Web 应用程序,用于加载书籍并一次显示一页。我想知道如何在页面中选择一个句子并显示一条消息。据我所知,它必须具有图像坐标。 请参阅http://epaper.d
我使用的 GPS 输出多个 NMEA 语句,可用于定位数据。 (GPGGA 和 GPRMC)。有什么理由我应该使用一个而不是另一个吗?我应该检查它们并比较数据吗?我可以随便挑一个使用吗? 在这一点上,
我想使用TinyGPS++在 Arduino 上解析 NMEA 数据并在 OLED 显示屏上显示信息。但是,NMEA 数据将通过 USB 接收,而不是使用软件串行和 TX/RX 引脚。 我按照 Tin
我需要删除其中的所有空格。 例如:这是我的代码O/P:Thisismycode 这是我到目前为止的代码。 import java.util.Scanner; public class nospace{
我对 python 很陌生,我不知道如何解决以下问题: 我有两个数据框,我想使用某种 VLOOKUP 函数来将句子与特定关键字相匹配。在下面的示例中,(df1) 3e 句子应与“banana”(df2
这个问题已经有答案了: How slicing in Python works (38 个回答) Python list slice syntax used for no obvious reason
我想在我的表格作者的句子列中找到以 # 开头的单词。我不知道我在寻找什么词,因为我只知道它以 # 开头。 表:作者(姓名,句子) 作者 |句子 艾伯特 |我#want to be #discussin
句子
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve
我目前正在经历免费代码营的第一个 JS 挑战。 我在标题为“句子首字母大写”的挑战中遇到了问题。在这个挑战中,我需要编写一个函数,将给定字符串中单词的每个第一个字母大写,并将所有其他字母小写。 Her
假设我有一个文本,看起来像这样: Some sentence in which a fox jumps over some fence. Another sentence in which a
我是 C++ 的初学者,我想了解有关字符的更多信息,但我遇到了问题。我试图制作一个程序,它复制一个句子并在空格 (' ') 之间添加一个新行 ('\n'),就像一个单词一个单词地分开一个句子. int
我需要将一个句子(例如“Hello world”)复制到一个字符串列表中,意思是复制到一个字符数组中,其中每 2 个单词由 '\0' 分隔。请注意,单词被定义为一行中没有空格的任意数量的字符。 因此,
我有这样一个字符串, my_str ='·in this match, dated may 1, 2013 (the "the match") is between brooklyn centenni
我在列表中有一堆句子,我想使用 nltk 库来阻止它。我可以一次提取一个句子,但是我在从列表中提取句子并将它们重新组合在一起时遇到了问题。我缺少一个步骤吗? nltk 库很新。谢谢! import n
我有一个词和文本。我必须找到所有包含该词的提案。你有什么想法吗? piblic List GetSnetences(string word) { // search all proposals
我正在通过 doc2vec 模型使用 gensim 库在 Python 中构建 NLP 聊天应用程序。我有硬编码的文档并给出了一组训练示例,我通过抛出用户问题来测试模型,然后第一步找到最相似的文档。在
我有以下代码,每 10 个单词拆分一行。 #!/bin/bash while read line do counter=1; for word in $line do
我是一名优秀的程序员,十分优秀!