- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
嗨,我正在尝试使用 python 2.x 中的朴素贝叶斯分类器进行情感分析。它使用 txt 文件读取情绪,然后根据样本 txt 文件情绪给出积极或消极的输出。我希望输出与输入的形式相同,例如我有一个包含 1000 个原始情绪的文本文件,我希望输出显示每种情绪的积极或消极。请帮忙。下面是我正在使用的代码
import math
import string
def Naive_Bayes_Classifier(positive, negative, total_negative, total_positive, test_string):
y_values = [0,1]
prob_values = [None, None]
for y_value in y_values:
posterior_prob = 1.0
for word in test_string.split():
word = word.lower().translate(None,string.punctuation).strip()
if y_value == 0:
if word not in negative:
posterior_prob *= 0.0
else:
posterior_prob *= negative[word]
else:
if word not in positive:
posterior_prob *= 0.0
else:
posterior_prob *= positive[word]
if y_value == 0:
prob_values[y_value] = posterior_prob * float(total_negative) / (total_negative + total_positive)
else:
prob_values[y_value] = posterior_prob * float(total_positive) / (total_negative + total_positive)
total_prob_values = 0
for i in prob_values:
total_prob_values += i
for i in range(0,len(prob_values)):
prob_values[i] = float(prob_values[i]) / total_prob_values
print prob_values
if prob_values[0] > prob_values[1]:
return 0
else:
return 1
if __name__ == '__main__':
sentiment = open(r'C:/Users/documents/sample.txt')
#Preprocessing of training set
vocabulary = {}
positive = {}
negative = {}
training_set = []
TOTAL_WORDS = 0
total_negative = 0
total_positive = 0
for line in sentiment:
words = line.split()
y = words[-1].strip()
y = int(y)
if y == 0:
total_negative += 1
else:
total_positive += 1
for word in words:
word = word.lower().translate(None,string.punctuation).strip()
if word not in vocabulary and word.isdigit() is False:
vocabulary[word] = 1
TOTAL_WORDS += 1
elif word in vocabulary:
vocabulary[word] += 1
TOTAL_WORDS += 1
#Training
if y == 0:
if word not in negative:
negative[word] = 1
else:
negative[word] += 1
else:
if word not in positive:
positive[word] = 1
else:
positive[word] += 1
for word in vocabulary.keys():
vocabulary[word] = float(vocabulary[word])/TOTAL_WORDS
for word in positive.keys():
positive[word] = float(positive[word])/total_positive
for word in negative.keys():
negative[word] = float(negative[word])/total_negative
test_string = raw_input("Enter the review: \n")
classifier = Naive_Bayes_Classifier(positive, negative, total_negative, total_positive, test_string)
if classifier == 0:
print "Negative review"
else:
print "Positive review"
最佳答案
我已经检查了您在评论中发布的 github 存储库。我尝试运行该项目,但出现一些错误。
无论如何,我已经检查了项目结构和用于训练朴素贝叶斯算法的文件,我认为以下代码可用于将结果数据写入 Excel 文件(即 .xls)
with open("test11.txt") as f:
for line in f:
classifier = naive_bayes_classifier(positive, negative, total_negative, total_positive, line)
result = 'Positive' if classifier == 0 else 'Negative'
data_to_be_written += ([line, result],)
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('test.xls')
worksheet = workbook.add_worksheet()
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in data_to_be_written:
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
workbook.close()
接下来,对于包含要测试的句子的文件的每一行,我调用分类器并准备一个将写入 csv 文件中的结构。
然后循环结构体并写入xls文件。
为此,我使用了一个名为 xlsxwriter 的 Python 站点包。
正如我之前告诉过你的,我在运行该项目时遇到了一些问题,因此这段代码也没有经过测试。它应该运作良好,但是无论如何,如果您遇到麻烦,请告诉我。
问候
关于python - 文本分析-无法将Python程序的输出写入csv或xls文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779723/
我正在使用 Python 2.76 向 .aspx 网页提交查询并通过 BeautifulSoup 获取结果,并希望将它们存储到 Excel 电子表格中。 import mechanize impor
我正在学习使用 Hadoop 执行大数据相关操作。 我需要对拆分为 8 个 xls 文件的数据集集合执行一些查询。每个 xls 文件都有多个工作表,查询只涉及其中一个工作表。 数据集可以在这里下载:h
我有一个 excel 文件,但文件大小太大,您有什么建议可以让我们以相同的格式减小文件大小吗? 最佳答案 有一些常见技巧可以减小 Excel 文档的大小,同时保持 Excel 格式。这取决于文件的具体
我有多个目录,每个目录包含任意数量的 .xls 文件。我想获取任何给定目录中的文件并将它们合并到一个 .xls 文件中,使用文件名作为选项卡名称。例如,如果有文件 NAME.xls、AGE.xls、L
使用下面的 Python 测试代码,我试图将 Excel (*.xls) 文件中唯一的工作表复制到一个包含一个工作表的新 Excel 文件中。 输入电子表格如下所示: from copy import
我有一个 xls 文件,其中包含 15 张数据,我想将所有数据复制到另一个 xls 文件中。我有很多这样的文件,所以我想使用宏 vba 编码对其进行自定义。我想在第一张纸上设计按钮,它会运行宏代码并将
我正在尝试从 xls 中读取数据,该数据运行良好 java.io.File f1=new java.io.File("E:/SELENIUM DATA/First_P1/DATA_SHEET.x
我有一个文件 .XLS 扩展名,但保存为 XMl 电子表格 2003 ,想要读取该文件并使用 java 代码将其转换为 .XLS 扩展名,我的代码如下 - 公共(public)类 ExcelImpor
我已经创建了 Servlet,它下载 POI XLS 文件,在第一个 get/post 请求时,新文件是使用sheet0下载的。当我执行第二个请求时,正在下载两张新文件,sheet0 是上一个请求,s
我有大量格式为:YYYYDDMM_rest_of_filename.xls 的 excel 文件 我如何使用 ruby 在 YYYYMMDD_rest_of_filename.xls 的可能数量较
我将 xlsx 文件保存在 firefox 中,浏览器将其识别为 97-2003 工作簿 (.xls) 当我打开 xls 文件时,它会生成弹出窗口 我也在不同的机器上尝试过这个,它在 firefox
伙计们。我在 DB 有日期作为字符串“d/m/Y”。 当我将此日期输入 Excel 时,它会显示正常日期,例如 2000 年 3 月 10 日,但是当我单击单元格时,它会显示值 '03/10/2000
我正在研究 INFORMIX 4GL 程序。该程序生成输出文本文件。这是输出的示例: Lot No|Purchaser name|Billing|Payment|Deposit|Balance|
代码如下: <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <%
我的应用程序正在从 MYSQL 动态获取数据并显示。问题是我什至必须选择“导出到 Xls”文件。这些文件应该是 A4 大小,所以我应该自定义 Xls 文件中内容的字体大小以适应 A4 大小。如果可能的
我必须运行一些 SQL 查询并将结果放入电子表格中。由于我处于 Spring/Java 环境中,因此我打算使用 JDBC 运行查询、迭代 ResultSet,并使用 Jakarta POI 创建一个简
我有一个包含多张工作表的 Excel 电子表格。格式如下: Date A B C D E F
我正在使用 jquery 数据表插件来显示数据。我添加了文件另存为 excel、csv 和 pdf 的选项。 虽然它允许我将表格内容保存为文件。但在 excel 和 csv 情况下,它都会将文件保存为
我正在尝试弄清楚如何读取 XLS 文档的内容并且我能够很好地获取字节,但我不知道从这里到哪里去。尝试 [[NSString alloc] initWithBytes:data.bytes length
在我的应用程序中,我需要以预定义格式导出到 xls 文件。 所以我刚刚集成了 php_excel2007。我正在使用一个具有预定义格式的模板。 问题 这里的单元格数据可能会动态变化。如果数据远大于单元
我是一名优秀的程序员,十分优秀!