Java 使用WordNet获取错误分数
我正在用SentiWordNet做一些情绪分析,我参考了这里的帖子。然而,尽管尝试了各种输入,我还是得到了0.0分。这里有什么我做错的吗?谢谢Java 使用WordNet获取错误分数,java,twitter,sentiment-analysis,Java,Twitter,Sentiment Analysis,我正在用SentiWordNet做一些情绪分析,我参考了这里的帖子。然而,尽管尝试了各种输入,我还是得到了0.0分。这里有什么我做错的吗?谢谢 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.HashMap; import java.util.Iterator; import java.util.Set;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
public class SWN3 {
private String pathToSWN = "C:\\Users\\Malcolm\\Desktop\\SentiWordNet_3.0.0\\home\\swn\\www\\admin\\dump\\SentiWordNet_3.0.0.txt";
private HashMap<String, Double> _dict;
public SWN3(){
_dict = new HashMap<String, Double>();
HashMap<String, Vector<Double>> _temp = new HashMap<String, Vector<Double>>();
try{
BufferedReader csv = new BufferedReader(new FileReader(pathToSWN));
String line = "";
while((line = csv.readLine()) != null)
{
String[] data = line.split("\t");
Double score = Double.parseDouble(data[2])-Double.parseDouble(data[3]);
String[] words = data[4].split(" ");
for(String w:words)
{
String[] w_n = w.split("#");
w_n[0] += "#"+data[0];
int index = Integer.parseInt(w_n[1])-1;
if(_temp.containsKey(w_n[0]))
{
Vector<Double> v = _temp.get(w_n[0]);
if(index>v.size())
for(int i = v.size();i<index; i++)
v.add(0.0);
v.add(index, score);
_temp.put(w_n[0], v);
}
else
{
Vector<Double> v = new Vector<Double>();
for(int i = 0;i<index; i++)
v.add(0.0);
v.add(index, score);
_temp.put(w_n[0], v);
}
}
}
Set<String> temp = _temp.keySet();
for (Iterator<String> iterator = temp.iterator(); iterator.hasNext();) {
String word = (String) iterator.next();
Vector<Double> v = _temp.get(word);
double score = 0.0;
double sum = 0.0;
for(int i = 0; i < v.size(); i++)
score += ((double)1/(double)(i+1))*v.get(i);
for(int i = 1; i<=v.size(); i++)
sum += (double)1/(double)i;
score /= sum;
String sent = "";
if(score>=0.75)
sent = "strong_positive";
else
if(score > 0.25 && score<=0.5)
sent = "positive";
else
if(score > 0 && score>=0.25)
sent = "weak_positive";
else
if(score < 0 && score>=-0.25)
sent = "weak_negative";
else
if(score < -0.25 && score>=-0.5)
sent = "negative";
else
if(score<=-0.75)
sent = "strong_negative";
_dict.put(word, score);
}
}
catch(Exception e){e.printStackTrace();}
}
public Double extract(String word)
{
Double total = new Double(0);
if(_dict.get(word+"#n") != null)
total = _dict.get(word+"#n") + total;
if(_dict.get(word+"#a") != null)
total = _dict.get(word+"#a") + total;
if(_dict.get(word+"#r") != null)
total = _dict.get(word+"#r") + total;
if(_dict.get(word+"#v") != null)
total = _dict.get(word+"#v") + total;
return total;
}
public static void main(String[] args) {
SWN3 test = new SWN3();
String sentence="Hello have a Super awesome great day";
String[] words = sentence.split("\\s+");
double totalScore = 0;
for(String word : words) {
word = word.replaceAll("([^a-zA-Z\\s])", "");
if (test.extract(word) == null)
continue;
totalScore += test.extract(word);
}
System.out.println(totalScore);
}
}
通常,SentiWord.txt文件的格式很奇怪 您需要删除第一部分(包括注释和说明)和最后两行:
#
EMPTY LINE
解析器不知道如何处理这些情况,如果删除这两行代码,就可以了。我运行了你的程序,得到了0.908755408934904。你能发布你的
SentiWord.txt
文件的前10行吗?刚刚发布了前10行。你能删除文件顶部的所有评论吗?是的,先生,删除了包含评论的顶部部分。以上是SentiWordNet.3.0.0.txt的前10行。您运行了与我发布的代码相同的代码,但未对其进行编辑?非常感谢!=)真的很感激it@MarounMarounsentiwordnet.txt文件中#1和#2的含义是什么?也请帮帮我。例如:一个面向顶点或在朝向顶点的一侧的000027300 acroscopic#1。。。。提前谢谢。嗨@Faizal我得到了java.lang.ArrayIndexOutofbound异常:2,出了什么问题?
#
EMPTY LINE