title: How to use SentiWordNet
body: <p>I need to do sentiment analysis on some csv files containing tweets. I'm using <a href="http://sentiwordnet.isti.cnr.it/" rel="nofollow">SentiWordNet</a> to do the sentiment analysis.</p>
  
  <p>I got the following piece of sample java code they provided on their site. I'm not sure how to use it. The path of the csv file that I want to analyze is <code>C:\Users\MyName\Desktop\tweets.csv</code> . The path of the <code>SentiWordNet_3.0.0.txt</code> is <code>C:\Users\MyName\Desktop\SentiWordNet_3.0.0\home\swn\www\admin\dump\SentiWordNet_3.0.0_20130122.txt</code> . I'm new to java, pls help, thanks! The link to the sample java code below is <a href="http://sentiwordnet.isti.cnr.it/code/SWN3.java" rel="nofollow">this</a>.</p>
  
  <pre><code>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&lt;String, String&gt; _dict;
  
      public SWN3(){
  
          _dict = new HashMap&lt;String, String&gt;();
          HashMap&lt;String, Vector&lt;Double&gt;&gt; _temp = new HashMap&lt;String, Vector&lt;Double&gt;&gt;();
          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&lt;Double&gt; v = _temp.get(w_n[0]);
                          if(index&gt;v.size())
                              for(int i = v.size();i&lt;index; i++)
                                  v.add(0.0);
                          v.add(index, score);
                          _temp.put(w_n[0], v);
                      }
                      else
                      {
                          Vector&lt;Double&gt; v = new Vector&lt;Double&gt;();
                          for(int i = 0;i&lt;index; i++)
                              v.add(0.0);
                          v.add(index, score);
                          _temp.put(w_n[0], v);
                      }
                  }
              }
              Set&lt;String&gt; temp = _temp.keySet();
              for (Iterator&lt;String&gt; iterator = temp.iterator(); iterator.hasNext();) {
                  String word = (String) iterator.next();
                  Vector&lt;Double&gt; v = _temp.get(word);
                  double score = 0.0;
                  double sum = 0.0;
                  for(int i = 0; i &lt; v.size(); i++)
                      score += ((double)1/(double)(i+1))*v.get(i);
                  for(int i = 1; i&lt;=v.size(); i++)
                      sum += (double)1/(double)i;
                  score /= sum;
                  String sent = "";               
                  if(score&gt;=0.75)
                      sent = "strong_positive";
                  else
                  if(score &gt; 0.25 &amp;&amp; score&lt;=0.5)
                      sent = "positive";
                  else
                  if(score &gt; 0 &amp;&amp; score&gt;=0.25)
                      sent = "weak_positive";
                  else
                  if(score &lt; 0 &amp;&amp; score&gt;=-0.25)
                      sent = "weak_negative";
                  else
                  if(score &lt; -0.25 &amp;&amp; score&gt;=-0.5)
                      sent = "negative";
                  else
                  if(score&lt;=-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);
      }
  }
  </code></pre>
  
  <p>Newcode:</p>
  
  <pre><code>public class SWN3 {
          private String pathToSWN = "C:\\Users\\MyName\\Desktop\\SentiWordNet_3.0.0\\home\\swn\\www\\admin\\dump\\SentiWordNet_3.0.0.txt";
      private HashMap&lt;String, String&gt; _dict;
  
      public SWN3(){
  
          _dict = new HashMap&lt;String, String&gt;();
          HashMap&lt;String, Vector&lt;Double&gt;&gt; _temp = new HashMap&lt;String, Vector&lt;Double&gt;&gt;();
          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&lt;Double&gt; v = _temp.get(w_n[0]);
                          if(index&gt;v.size())
                              for(int i = v.size();i&lt;index; i++)
                                  v.add(0.0);
                          v.add(index, score);
                          _temp.put(w_n[0], v);
                      }
                      else
                      {
                          Vector&lt;Double&gt; v = new Vector&lt;Double&gt;();
                          for(int i = 0;i&lt;index; i++)
                              v.add(0.0);
                          v.add(index, score);
                          _temp.put(w_n[0], v);
                      }
                  }
              }
              Set&lt;String&gt; temp = _temp.keySet();
              for (Iterator&lt;String&gt; iterator = temp.iterator(); iterator.hasNext();) {
                  String word = (String) iterator.next();
                  Vector&lt;Double&gt; v = _temp.get(word);
                  double score = 0.0;
                  double sum = 0.0;
                  for(int i = 0; i &lt; v.size(); i++)
                      score += ((double)1/(double)(i+1))*v.get(i);
                  for(int i = 1; i&lt;=v.size(); i++)
                      sum += (double)1/(double)i;
                  score /= sum;
                  String sent = "";               
                  if(score&gt;=0.75)
                      sent = "strong_positive";
                  else
                  if(score &gt; 0.25 &amp;&amp; score&lt;=0.5)
                      sent = "positive";
                  else
                  if(score &gt; 0 &amp;&amp; score&gt;=0.25)
                      sent = "weak_positive";
                  else
                  if(score &lt; 0 &amp;&amp; score&gt;=-0.25)
                      sent = "weak_negative";
                  else
                  if(score &lt; -0.25 &amp;&amp; score&gt;=-0.5)
                      sent = "negative";
                  else
                  if(score&lt;=-0.75)
                      sent = "strong_negative";
                  _dict.put(word, sent);
              }
          }
          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 String classifytweet(){
          String[] words = twit.split("\\s+"); 
          double totalScore = 0, averageScore;
          for(String word : words) {
              word = word.replaceAll("([^a-zA-Z\\s])", "");
              if (_sw.extract(word) == null)
                  continue;
              totalScore += _sw.extract(word);
          }
          Double AverageScore = totalScore;
  
          if(averageScore&gt;=0.75)
              return "very positive";
          else if(averageScore &gt; 0.25 &amp;&amp; averageScore&lt;0.5)
              return  "positive";
          else if(averageScore&gt;=0.5)
              return  "positive";
          else if(averageScore &lt; 0 &amp;&amp; averageScore&gt;=-0.25)
              return "negative";
          else if(averageScore &lt; -0.25 &amp;&amp; averageScore&gt;=-0.5)
              return "negative";
          else if(averageScore&lt;=-0.75)
              return "very negative";
          return "neutral";
      }
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
      }
  </code></pre>