2013-03-27 35 views
5

つぶやきを含むいくつかのCSVファイルについて感情分析をする必要があります。私は感情分析を行うためにSentiWordNetを使用しています。SentiWordNetの使い方

自分のサイトで提供した次のサンプルJavaコードを入手しました。私はそれを使う方法がわからない。分析したいCSVファイルのパスはC:\Users\MyName\Desktop\tweets.csvです。 SentiWordNet_3.0.0.txtのパスはC:\Users\MyName\Desktop\SentiWordNet_3.0.0\home\swn\www\admin\dump\SentiWordNet_3.0.0_20130122.txtです。私はjavaに新しい、plsの助け、ありがとう!下のサンプルJavaコードへのリンクはthisです。

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); 
    } 
} 

Newcode:ファイルの最初にすべての「ごみ」を削除することによって、すべての開始の

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<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 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>=0.75) 
      return "very positive"; 
     else if(averageScore > 0.25 && averageScore<0.5) 
      return "positive"; 
     else if(averageScore>=0.5) 
      return "positive"; 
     else if(averageScore < 0 && averageScore>=-0.25) 
      return "negative"; 
     else if(averageScore < -0.25 && averageScore>=-0.5) 
      return "negative"; 
     else if(averageScore<=-0.75) 
      return "very negative"; 
     return "neutral"; 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
    } 

答えて

7

まず(説明、命令等を含む。)

一つの可能​​な使用法SWN3を変更すると、その中のextractメソッドをDoubleに返します。

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; 
} 

次に、タグを付ける文字列を与えると、単語(記号と未知の文字は含まない)のみを持つように分割し、各単語にextractメソッドから返された結果を使用すると、文字列の平均重量:

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); 
} 
verageScore = totalScore; 

if(averageScore>=0.75) 
    return "very positive"; 
else if(averageScore > 0.25 && averageScore<0.5) 
    return "positive"; 
else if(averageScore>=0.5) 
    return "positive"; 
else if(averageScore < 0 && averageScore>=-0.25) 
    return "negative"; 
else if(averageScore < -0.25 && averageScore>=-0.5) 
    return "negative"; 
else if(averageScore<=-0.75) 
    return "very negative"; 
return "neutral"; 

この方法がわかりやすく、うまく動作します。


UPDATE:

それはStringキーとDouble値を持つことになりますので、私は_dict = new HashMap<String, Double>();_dictを変更しました。

だから私は、あなたが、主な機能を書くことでcsvファイルのパスを提供し、そこから単語を抽出する必要があり、そのために_dict.put(word, sent);願い_dict.put(word, score);

+0

返信いただきありがとうございます、私はまだいくつかの部分ではっきりしていません。これは何を意味するのでしょうか? if(_dict.get(word + "#r")!= null)#n、#a、#r、#v?ありがとう! – Belgarion

+1

ファイルの最初の列を見ると、これらの文字(* noun *、* verb * ..の略)がわかりますので、すべてのケースをカバーする必要があります。 – Maroun

+1

ああ、私は参照してください。私はまだ私のtweet.csvファイルへのリンクを置くためにもう少し助けが必要ですか? C:\ Users \ MyName \ Desktop \ tweets.csv私は上に私の更新されたコードを貼り付け、plsは、それを編集して自由に感じる! – Belgarion

0

を置き換えます。単語とその位置を送信して抽出機能を呼び出します。