【问题标题】:using sentiwordnet for calculating polarity使用 sentiwordnet 计算极性
【发布时间】:2013-09-02 18:01:16
【问题描述】:

有人使用 sentiwordnet。当我从该站点下载的 sentiwordnet 文件被拆分时,我不断收到错误 java.lang.ArrayIndexOutOfBoundsException: 2 http://sentiwordnet.isti.cnr.it/ 。我想我必须在使用它之前格式化 sentiwordnet 文件。但是怎么做呢?

这是程序

    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 = "data"+File.separator+"SentiWordNet_3.0.0.txt";
    private HashMap<String, String> _dict;

    public SWN3(){

        _dict = new HashMap<String, String>();
        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, sent);
            }
        }
        catch(Exception e){e.printStackTrace();}        
    }

    public String extract(String word, String pos)
    {
        return _dict.get(word+"#"+pos);
    }
}

【问题讨论】:

  • 您包含的网址未指向任何资源。
  • 查看这里返回的数据是什么——String[] data = line.split("\t");
  • 好的,我看到你的网址已经包含了下一句的开头。

标签: java semantics


【解决方案1】:

查看源文件,在读取实际数据记录之前,有很多头应该被读取和丢弃。

尝试丢弃任何以“#”开头的行。

【讨论】:

  • readline() 方法返回一条线,例如 00037457 0.375 0.5 active#1 趋向于变得更严重或更广泛的范围; “活动性结核病”,但有时返回的行是一个空字符串,即使有一些文本。我该如何解决这个问题
  • BufferedReader.readLine() 总是返回到下一个 LF、CR 或 CRLF。所以如果它返回一个空字符串,那是因为有一个空行。丢弃它并继续。
  • 在每次拆分时,您可能应该检查是否返回了一些有用的东西。这样如果你读到的东西不符合你的解析要求,你就可以优雅地处理它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2012-04-30
  • 2017-10-07
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多