Kronosinhaven

Friday, July 8, 2011

typedependency

import edu.stanford.nlp.objectbank.TokenizerFactory;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.process.*;
import edu.stanford.nlp.trees.*;
import java.io.*;
import java.util.*;
/**
*
* @author kronos
*/
public class typedependancyFinder {

public static List prsd_list;
public static LexicalizedParser lp = new LexicalizedParser("./res/englishPCFG.ser.gz");
public static InputStream in=null;
public static DataInputStream dis=null;
public static BufferedReader br=null;
public static TokenizerFactory tf = PTBTokenizer.factory(false, new WordTokenFactory());
public static TreePrint tp=null;
public static Tree t=null;
public static TreebankLanguagePack tlp = new PennTreebankLanguagePack();
public static GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
public static GrammaticalStructure gs=null;


public List getTypeDepndncy(String line)
{
List td=null;
try {
in=new ByteArrayInputStream(line.getBytes("UTF-8"));
dis = new DataInputStream(in);
br = new BufferedReader(new InputStreamReader(dis));
tp = new TreePrint("typedDependenciesCollapsed");
String sentence;
while ((sentence = br.readLine()) != null)
{
List tokens = tf.getTokenizer(new StringReader(sentence)).tokenize();
lp.parse(tokens);
t = lp.getBestParse();
gs = gsf.newGrammaticalStructure(t);
td=(List) gs.typedDependenciesCollapsed();
}
} catch (Exception e) {

System.out.println("" + e.toString());
e.printStackTrace();
}
return td;
}

public static void main(String[] args) {
System.out.println( new typedependancyFinder().getTypeDepndncy("It's unclear whether Wednesday's twist will affect the timing of Google's initial public offering"));
}

}

pos tagger sandford

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

/**
*
* @author kronos
*/
public class postagers {

public static MaxentTagger tagger=null;

public postagers() {
try {
tagger=new MaxentTagger("./res/posmodels/left3words-wsj-0-18.tagger");
} catch (Exception e) {
e.printStackTrace();
}
}

public String generatePOS(String sen)
{
return tagger.tagString(sen);
}

}

parsed tree using standford parser

import edu.stanford.nlp.objectbank.TokenizerFactory;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.process.*;
import edu.stanford.nlp.trees.*;
import java.io.*;
import java.util.*;

public class parsedTree {

public static List prsd_list;
public static LexicalizedParser lp = new LexicalizedParser("./res/englishPCFG.ser.gz");
public static InputStream in=null;
public static DataInputStream dis=null;
public static BufferedReader br=null;
public static TokenizerFactory tf = PTBTokenizer.factory(false, new WordTokenFactory());
public static TreePrint tp=null;
public static Tree t=null;

public static String getParsedTree(String line)
{
String prsd="";

try {
in=new ByteArrayInputStream(line.getBytes("UTF-8"));
dis = new DataInputStream(in);
br = new BufferedReader(new InputStreamReader(dis));
tp = new TreePrint("penn,typedDependenciesCollapsed");
String sentence;

while ((sentence = br.readLine()) != null)
{
List tokens = tf.getTokenizer(new StringReader(sentence)).tokenize();
lp.parse(tokens);
t = lp.getBestParse();
prsd=t.toString();
String arr[]=prsd.split(" ");
List ls=new ArrayList();
prsd="";
for(int i=0;i {
if(!arr[i].contains("["))
{
prsd+=arr[i]+" ";
if(arr[i].contains("(")){
while(arr[i].contains("("))
{
ls.add("(");
arr[i]=arr[i].substring(1);
}
ls.add(arr[i]);}
if(arr[i].contains(")")){
ls.add(arr[i].substring(0, arr[i].indexOf(")")));
arr[i]=arr[i].replace(arr[i].substring(0, arr[i].indexOf(")")), "");
while(arr[i].contains(")"))
{
ls.add(")");
arr[i]=arr[i].substring(1);
}

}
}
}

prsd_list=ls;
}
dis.close();

}catch (Exception e){e.printStackTrace();}

return prsd;
}

public static void main(String[] args) {
System.out.println(getParsedTree("In Wednesday's filing, Google said it planned to complete the IPO 'as soon as practicable'."));
}

}