Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中非英语推文的情感分析_Python_Python 2.7_Twitter_Nlp_Sentiment Analysis - Fatal编程技术网

python中非英语推文的情感分析

python中非英语推文的情感分析,python,python-2.7,twitter,nlp,sentiment-analysis,Python,Python 2.7,Twitter,Nlp,Sentiment Analysis,目标:将每条推文分类为正面或负面,并将其写入一个输出文件,该文件将包含用户名、原始推文和推文情绪 代码: import re,math input_file="raw_data.csv" fileout=open("Output.txt","w") wordFile=open("words.txt","w") expression=r"(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)" fileAFINN = 'AFINN-111.txt' afi

目标:将每条推文分类为正面或负面,并将其写入一个输出文件,该文件将包含用户名、原始推文和推文情绪

代码:

import re,math
input_file="raw_data.csv"
fileout=open("Output.txt","w")
wordFile=open("words.txt","w")
expression=r"(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"

fileAFINN = 'AFINN-111.txt'
afinn = dict(map(lambda (w, s): (w, int(s)), [ws.strip().split('\t') for ws in open(fileAFINN)]))

pattern=re.compile(r'\w+')
pattern_split = re.compile(r"\W+")
words = pattern_split.split(input_file.lower())
print "File processing started"
with open(input_file,'r') as myfile:
for line in myfile:
    line = line.lower()

    line=re.sub(expression," ",line)
    words = pattern_split.split(line.lower())
    sentiments = map(lambda word: afinn.get(word, 0), words)
    #print sentiments
    # How should you weight the individual word sentiments?
    # You could do N, sqrt(N) or 1 for example. Here I use sqrt(N)
    """
    Returns a float for sentiment strength based on the input text.
    Positive values are positive valence, negative value are negative valence.
    """
    if sentiments:
        sentiment = float(sum(sentiments))/math.sqrt(len(sentiments))
        #wordFile.write(sentiments)
    else:
        sentiment = 0
    wordFile.write(line+','+str(sentiment)+'\n')
fileout.write(line+'\n')
print "File processing completed"

fileout.close()
myfile.close()
wordFile.close()
问题:显然output.txt文件是

abc some tweet text 0
bcd some more tweets 1
efg some more tweet 0
问题1:如何在用户ID tweet文本之间添加逗号?输出应该是这样的

 abc,some tweet text,0
 bcd,some other tweet,1
 efg,more tweets,0
问题2:推文是用巴哈萨语(BM)写的,我正在使用的AFINN字典是用英语单词写的。所以分类是错误的。你知道我能用的BM字典吗

问题3:如何将此代码打包到JAR文件中

谢谢。

问题1:

由于
fileout.write(line+'\n')
,因此,
output.txt
当前仅由您正在读取的行组成。因为它是空间分隔的,所以您可以非常轻松地分隔线

line_data = line.split(' ') # Split the line into a list, separated by spaces
user_id = line_data[0] # The first element of the list
tweets = line_data[1:-1] # The middle elements of the list
sentiment = line_data[-1] # The last element of the list
fileout.write(user_id + "," + " ".join(tweets) + "," + sentiment +'\n')
问题2: 快速的谷歌搜索给了我这个。但不确定它是否具备您所需的一切:

问题3:
试试Jython

你能提供更多信息吗?您正在向我们提供
感情用事.txt
的输出,但是您的代码都没有写入
感情用事.txt
,因此我不确定您希望它采用什么格式。此外,您应该在代码末尾有一个
wordFile.close
。@Kristy Hughes感谢您指出了异常情况。我已经更新了原来的帖子。文件personations.txt已被output.txt替换,并在最后关闭了wordFile。也许您可以使用smileys创建一组tweet训练集,然后实现一个朴素的贝叶斯分类器?@clemtoy这是一个非常好的建议。从这个想法开始,我就有了这个想法。但是你看,基本的问题是,我对编程范式以及数据挖掘算法仍然是新手。这就是为什么我选择将这个问题分成几个部分,并选择一次解决一个,这将有助于我的学习。谢谢你的回答。在您的代码文件中。write(user_id+),“+tweets+”,“+emotional+'\n')抛出一个类型错误:无法连接'str'和'list'对象,我将其更正为wordFile。write(str(user_id)+','+str(tweets)+','+str str emotional)+'\n'),执行代码的输出类似于myuara,1mdb,[jawab]、[tuduhan]、[tun]、[m]、[isu]、[dana'、[hilang'”、0.0这不是我想要的。对了,哎呀,我忘了
tweets
是一个列表。使用
.join()
而不是
str()
。join的语法是
分隔符。join(list)
。既然你想让它用空格分隔,你就需要
”。join(tweets)
。更新了我的答案以反映这一点。