senti wordnet - How to predict sentiment using sentiwordnet -
i need sentiment analysis on csv files containing movie reviews. i'm using sentiwordnet sentiment analysis.the main problem facing in main() function .here can find polarity of single sentence.(string sentence="i love hate current political climate.")but want use complete csv file , find polarity of each review.the path sentiwordnet "c:\users\intel\desktop\sentiment final\finalsentiment.txt"
package swn3; import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.filereader; import java.io.inputstreamreader; import java.util.arrays; import java.util.hashmap; import java.util.iterator; import java.util.scanner; import java.util.set; import java.util.vector; public class swn3 { private string pathtoswn = "c:\\users\\intel\\desktop\\sentiment final\\finalsentiment.txt"; private hashmap<string, double> _dict; public swn3(){ _dict = new hashmap<string, double>(); 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]);// calculate synset score score = poss - negs string[] words = data[4].split(" ");// synset terms for(string w:words)//go through terms of current synset. { // synterm , synterm rank 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 = 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 = 0;i<index; i++) v.add(0.0); v.add(index, score); _temp.put(w_n[0], v); } } } set<string> temp = _temp.keyset(); (iterator<string> iterator = temp.iterator(); iterator.hasnext();) { string word = (string) iterator.next(); vector<double> v = _temp.get(word); // calculate weighted average. weigh synsets according rank. // score= 1/2*first + 1/3*second + 1/4*third ..... etc. // sum = 1/1 + 1/2 + 1/3 ... double score = 0.0; double sum = 0.0; for(int = 0; < v.size(); i++) score += ((double)1/(double)(i+1))*v.get(i); for(int = 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, score); } } 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 static void main(string[] args) throws exception { swn3 test = new swn3(); string sentence="i love hate current political climate."; string[] words = sentence.split("\\s+"); //splits sentence , put array. double totalscore = 0; int review=0; for(string word : words) { word = word.replaceall("([^a-za-z\\s])", "");//^ means not , a-za-z means upper , lower case characters.overall means not letters if (test.extract(word) == null) continue; totalscore += test.extract(word); } if(totalscore>=0.75) review= 5; else if(totalscore> 0.25 && totalscore<=0.5) review = 4; else if(totalscore > 0 && totalscore<=0.25) review = 3; else if(totalscore< 0 && totalscore>=-0.25) review= 2; else if(totalscore< -0.25 && totalscore>=-0.5) review= 1; else if(totalscore<=-0.75) review= 0; system.out.println(review); system.out.println(totalscore); } }
Comments
Post a Comment