Thursday, October 24, 2013

Calculate the Semantic Orientation for the Adjectives

This is the sample program for calculating the semantic orientation for adjectives.If any negative words appear before any adjective, then Opposite word will be consider.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;

import rita.wordnet.RiWordnet;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;
public class SemanticOrientation {

public static void main(String[] args) throws IOException, TikaException {
MaxentTagger tagger = new MaxentTagger("C:/Users/rsharma/Downloads/jars/wsj-0-18-bidirectional-nodistsim.tagger");
ArrayList<String> nelist =new ArrayList<String>();
ArrayList<String> polist =new ArrayList<String>();
ArrayList<Object> nlist = new ArrayList<Object>();
ArrayList<Object> plist = new ArrayList<Object>();
String con = new Tika().parseToString(new File("C:/Users/rsharma/Downloads/ad.txt"));
String string = con.replaceAll("[^\\p{L}\\p{Nd}]", " ");
String string2 = string.replaceAll("\\s+", " ").toLowerCase();
String[] sarr = string2.split(" ");

for (int sp =0 ; sp<sarr.length;sp++) {

String nregex = "\\b"+"not"+"\\b";
Pattern pn = Pattern.compile(nregex);
Matcher mp = pn.matcher(sarr[sp]);
if(mp.find()) {
nlist.add(sp); } }

//System.out.println("not lcoation" + " " + nlist);
for (int s =0 ; s<sarr.length;s++){

String word = sarr[s];
String tagged = tagger.tagString(word);
Pattern p = Pattern.compile(".*JJ");
Matcher m = p.matcher(tagged);
if(m.find()){
String string3=m.group(0);
String adj=string3.replace("_JJ", "");
//System.out.println(noun);
plist.add(s);
polist.add(adj);
}
}
//System.out.println("adjective location" + " " + plist);

System.out.println(polist);
for( int b = 0; b < nlist.size();b++) {

for(int c =0; c< plist.size();c++) {
if((Integer.parseInt(nlist.get(b).toString()) + 2 ) ==  Integer.parseInt(plist.get(c).toString()) || (Integer.parseInt(nlist.get(b).toString()) + 1 ) ==  Integer.parseInt(plist.get(c).toString())) {
//System.out.println(plist.get(c) + "finding adjective");
nelist.add(plist.get(c).toString());
polist.remove(sarr[Integer.parseInt(plist.get(c).toString())]);
//System.out.println(polist + " " + "removed");
}
}
} //System.out.println(nelist);
// Would pass in a PApplet normally, but we don't need to here
RiWordnet wordnet = new RiWordnet();
ArrayList<String> antonymnList = new ArrayList<String>();
// Get a random noun
//String word = wordnet.getRandomWord("n");
// Get max 15 synonyms
//a >> adjective, verb >>> v , noun >>>> n, adverb >>> r
// String[] synonyms = wordnet.getAllSynonyms("beautiful", "a", 15);
// System.out.println(wordnet.getPos("good").toString());
if(nelist.size()!=0) {

for(int j = 0 ; j < nelist.size(); j++) {

//System.out.println(sarr[Integer.parseInt(nelist.get(j).toString())] + " ");

String[] antonyms = wordnet.getAllAntonyms(sarr[Integer.parseInt(nelist.get(j).toString())],"a");
// System.out.println("Random noun: " + word);
if (antonyms != null) {
//System.out.println(antonyms[0] + " " + "anty");
antonymnList.add(antonyms[0]);

} else {
System.out.println("No antonyms!");
}
}
polist.addAll(antonymnList);

}
System.out.println(polist);
SentimentalWordNet12 sn = new SentimentalWordNet12();

double totalScore = 0;
    for(String word : polist) {
        word = word.replaceAll("([^a-zA-Z\\s])", "");
        if (sn.extract(word) == null)
            continue;
     //   System.out.println(word);
        totalScore += sn.extract(word);
    }
    if(totalScore == 0)
    {
    System.out.println("Neutral Statement :" + totalScore);
    } else if(totalScore > 0) {
    System.out.println("Postive Statement :" + totalScore);
    } else {
    System.out.println("Negative Statement :" + totalScore);
    }

}
}

Tuesday, October 22, 2013

Extract the Noun from the Text using NLP

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;

import edu.stanford.nlp.tagger.maxent.MaxentTagger;

public class GetNoun {
public static void main(String[] args) throws IOException, TikaException {

MaxentTagger tagger = new MaxentTagger("C:/Users/rsharma/Downloads/jars/wsj-0-18-bidirectional-nodistsim.tagger");
String con = new Tika().parseToString(new File("C:/Users/rsharma/Downloads/text.txt"));
String string = con.replaceAll("[^\\p{L}\\p{Nd}]", " ");
String string2 = string.replaceAll("\\s+", " ");
String lo = string2.toLowerCase();
ArrayList<String> set = new ArrayList<String>();
StringTokenizer tokenizer = new StringTokenizer(lo);
while (tokenizer.hasMoreElements()) {
String word = (String) tokenizer.nextElement();
String tagged = tagger.tagString(word);
Pattern p = Pattern.compile(".*NN");
Matcher m = p.matcher(tagged);
if(m.find()){
String string3=m.group(0);
String noun=string3.replace("_NN", "");
set.add(noun);
}
}
File file = new File("C:/Users/rsharma/Downloads/filename.txt");
if (!file.exists()) {
file.createNewFile(); }
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(set.toString().replaceAll("\\[", "").replaceAll("\\]", ""));
bw.close();

System.out.println(set.toString().replaceAll("\\[", "").replaceAll("\\]", ""));
}
}