1

I am in desperate need for some help with the following.

For my master thesis I have to conduct a sentiment analysis on some Amazon, Twitter and Facebook data. I have saved these data in a csv document. Now I want to use SentiWordNet to obtain the polarity scores. However I'm unable to run the script provided on their website using python.

First of all I have to say that I am completely new to Java. So please don't blame me for not knowing it all. I have spent a lot of time searching on the internet for some information or tutorials with no luck. There was one topic on this site from a person with a similar problem (How to use SentiWordNet), although I came across a different problem. Whenever I run the script below, I get the following message: ImportError: No module named java.io.BufferedReader. I tried to search on the internet for a solution, but I couldn't find any. Could someone please help me out with how to run this script. For starters, I have already removed the garbage in the sentiwordnet.txt file. The pathway to the SentiWordNet.txt file is \Users\Mo\Documents\etc. This is also the pathway for the csv file. Btw I'm running this script on OSX with python 2.7.5.

Thank you so much in advance for your help!!!

    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);
    }
}
Community
  • 1
  • 1

1 Answers1

0

Firstly, how are you running the class. Through command line or within an IDE such as Eclipse. If you are using a command line you must ensure you classpath has been set properly. If you are unfamiliar with such matters I would encourage creating a java project in an IDE and running it from there as the classpath will be configured for you. Creating your first Java project

Wadester
  • 453
  • 4
  • 12