Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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
Java 使用WordNet计算极性_Java_Semantics - Fatal编程技术网

Java 使用WordNet计算极性

Java 使用WordNet计算极性,java,semantics,Java,Semantics,有人与sentiwordnet合作过。当我从这个站点下载的sentiwordnet文件被拆分时,我不断收到错误java.lang.ArrayIndexOutOfBoundsException:2。我想我必须先格式化sentiwordnet文件,然后才能使用它。但是怎么做呢 这是节目 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.HashMap;

有人与sentiwordnet合作过。当我从这个站点下载的sentiwordnet文件被拆分时,我不断收到错误
java.lang.ArrayIndexOutOfBoundsException:2
。我想我必须先格式化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);
    }
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Set;
导入java.util.Vector;
公共级SWN3{
私有字符串pathToSWN=“data”+File.separator+“SentiWordNet_3.0.0.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查看源文件,那么在读取实际数据记录之前,应该读取并丢弃很多头文件


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

您包含的URL不指向任何资源。检查此处返回的数据-String[]data=line.split(“\t”);好的,我看到您的URL包含了下一句的开头。readline()方法返回一行,例如00037457 0.375 0.5 active#1,其范围可能会变得更严重或更宽;“active tuberculosis”,但有时返回的行是空字符串,即使有一些文本。如何处理此BufferedReader.readLine()始终返回到下一个LF、CR或CRLF。因此,如果它返回的是空字符串,那是因为有一个空行。丢弃它并继续。在每次拆分时,您可能应该检查是否返回了有用的内容。这样,如果您读取的内容不符合您的解析要求,您可以优雅地处理它。