Java 产品评论的情感分析
我正在使用SentiWordNet对产品评论进行情绪分析。但它显示了一些积极的词,如“不错”等,作为积极的,也产生了一个错误的分数。大多数时候,返回的分数超过了-1和1。 我使用的代码发布在这里。请看这个:Java 产品评论的情感分析,java,classification,sentiment-analysis,review,Java,Classification,Sentiment Analysis,Review,我正在使用SentiWordNet对产品评论进行情绪分析。但它显示了一些积极的词,如“不错”等,作为积极的,也产生了一个错误的分数。大多数时候,返回的分数超过了-1和1。 我使用的代码发布在这里。请看这个: package scoreget; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.sql.Connection;
package scoreget;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
public class SWN3 {
private String pathToSWN = "E:\\proj\\SentiWordNet_3.0.0.1.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 connect(int p_id, String desc, Double score,String T_nam) throws ClassNotFoundException, SQLException
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root", "");
String sql="insert into "+T_nam+"(p_id,Description,Score) values(?,?,?)";
PreparedStatement pst=con.prepareStatement(sql);
pst.setInt(1,p_id);
pst.setString(2, desc);
pst.setDouble(3, score);
pst.executeUpdate();
}
catch (Exception e){
e.printStackTrace();}
}
public static String calcscore(String p_id,String desc) throws ClassNotFoundException, SQLException
{
SWN3 test = new SWN3();
int pid=Integer.parseInt(p_id);
String sentence=desc;
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)+totalScore;
}
connect(pid,sentence,totalScore,"review");
return (Double.toString(totalScore));
}
public Double extract(String word, String pos)
{
return _dict.get(word+"#"+pos);
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
SWN3 obj=new SWN3();
}
}
package-scoreget;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Set;
导入java.util.Vector;
公共级SWN3{
私有字符串pathToSWN=“E:\\proj\\SentiWordNet_3.0.0.1.txt”;
私有HashMap_dict;
公共SWN3(){
_dict=newhashmap();
HashMap_temp=新HashMap();
试一试{
BufferedReader csv=新的BufferedReader(新文件阅读器(pathToSWN));
字符串行=”;
而((line=csv.readLine())!=null)
{
String[]data=line.split(“\t”);
Double score=Double.parseDouble(数据[2])-Double.parseDouble(数据[3]);
字符串[]字=数据[4]。拆分(“”);
for(字符串w:单词)
{
字符串[]w_n=w.split(“#”);
w_n[0]+=“#”+数据[0];
int index=Integer.parseInt(w_n[1])-1;
如果(_temp.containsKey(w_n[0]))
{
向量v=_temp.get(w_n[0]);
如果(索引>v.size())
对于(int i=v.size();i=0.25)
sent=“弱阳性”;
其他的
如果(分数<0&&score>=-0.25)
sent=“弱_负”;
其他的
如果(分数<-0.25&&分数>=-0.5)
sent=“否定”;
其他的
如果(score)你能发布一些示例输出吗?出于好奇,我进一步研究了这个问题,发现你的代码实际上并没有做你描述的事情:它只是在文件中读取。如果你需要一些帮助,你将不得不做更多的事,而不仅仅是将一些随机代码粘贴到一个问题中。