use of edu.stanford.nlp.trees.PennTreebankLanguagePack in project CoreNLP by stanfordnlp.
the class FastExactAutomatonMinimizer method main.
public static void main(String[] args) {
/*
TransducerGraph fa = new TransducerGraph();
fa.addArc(fa.getStartNode(),"1","a","");
fa.addArc(fa.getStartNode(),"2","b","");
fa.addArc(fa.getStartNode(),"3","c","");
fa.addArc("1","4","a","");
fa.addArc("2","4","a","");
fa.addArc("3","5","c","");
fa.addArc("4",fa.getEndNode(),"c","");
fa.addArc("5",fa.getEndNode(),"c","");
System.out.println(fa);
ExactAutomatonMinimizer minimizer = new ExactAutomatonMinimizer();
System.out.println(minimizer.minimizeFA(fa));
*/
System.out.println("Starting minimizer test...");
List<List<String>> pathList = new ArrayList<>();
TransducerGraph randomFA = TransducerGraph.createRandomGraph(5000, 5, 1.0, 5, pathList);
List<Double> outputs = randomFA.getPathOutputs(pathList);
TransducerGraph.GraphProcessor quasiDeterminizer = new QuasiDeterminizer();
AutomatonMinimizer minimizer = new FastExactAutomatonMinimizer();
TransducerGraph.NodeProcessor ntsp = new TransducerGraph.SetToStringNodeProcessor(new PennTreebankLanguagePack());
TransducerGraph.ArcProcessor isp = new TransducerGraph.InputSplittingProcessor();
TransducerGraph.ArcProcessor ocp = new TransducerGraph.OutputCombiningProcessor();
TransducerGraph detGraph = quasiDeterminizer.processGraph(randomFA);
// combine outputs into inputs
TransducerGraph combGraph = new TransducerGraph(detGraph, ocp);
// minimize the thing
TransducerGraph result = minimizer.minimizeFA(combGraph);
System.out.println("Minimized from " + randomFA.getNodes().size() + " to " + result.getNodes().size());
// pull out strings from sets returned by minimizer
result = new TransducerGraph(result, ntsp);
// split outputs from inputs
result = new TransducerGraph(result, isp);
List<Double> minOutputs = result.getPathOutputs(pathList);
System.out.println("Equal? " + outputs.equals(minOutputs));
/*
randomFA = new TransducerGraph(randomFA, new TransducerGraph.OutputCombiningProcessor());
System.out.print("Starting fast minimization...");
FastExactAutomatonMinimizer minimizer2 = new FastExactAutomatonMinimizer();
Timing.startTime();
TransducerGraph minimizedRandomFA = minimizer2.minimizeFA(randomFA);
Timing.tick("done. ( "+randomFA.getArcs().size()+" arcs to "+minimizedRandomFA.getArcs().size()+" arcs)");
minimizedRandomFA = new TransducerGraph(minimizedRandomFA, new TransducerGraph.InputSplittingProcessor());
List minOutputs = minimizedRandomFA.getPathOutputs(pathList);
System.out.println("Equal? "+outputs.equals(minOutputs));
System.out.print("Starting slow minimization...");
ExactAutomatonMinimizer minimizer = new ExactAutomatonMinimizer();
Timing.startTime();
minimizedRandomFA = minimizer.minimizeFA(randomFA);
Timing.tick("done. ( "+randomFA.getArcs().size()+" arcs to "+minimizedRandomFA.getArcs().size()+" arcs)");
minimizedRandomFA = new TransducerGraph(minimizedRandomFA, new TransducerGraph.InputSplittingProcessor());
minOutputs = minimizedRandomFA.getPathOutputs(pathList);
System.out.println("Equal? "+outputs.equals(minOutputs));
*/
}
use of edu.stanford.nlp.trees.PennTreebankLanguagePack in project CoreNLP by stanfordnlp.
the class DependencyIndexITest method checkTree.
private static void checkTree(Tree tree) {
List<Tree> leaves = tree.getLeaves();
for (Tree leaf : leaves) {
CoreLabel l = null;
if (leaf.label() instanceof CoreLabel)
l = (CoreLabel) leaf.label();
if (l != null) {
// System.err.println(l + " " + l.get(CoreAnnotations.IndexAnnotation.class));
int index = l.get(CoreAnnotations.IndexAnnotation.class);
String text = l.get(CoreAnnotations.TextAnnotation.class);
if (text.equals("Mary"))
assertEquals(1, index);
else if (text.equals("had"))
assertEquals(2, index);
else if (text.equals("a"))
assertEquals(3, index);
else if (text.equals("little"))
assertEquals(4, index);
else if (text.equals("lamb"))
assertEquals(5, index);
else if (text.equals("."))
assertEquals(6, index);
} else {
// System.err.println(leaf + " is not a CoreLabel.");
}
}
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
Collection<TypedDependency> deps = gs.typedDependenciesCCprocessed(GrammaticalStructure.Extras.MAXIMAL);
// System.out.println(deps);
// collect all nodes in deps
Set<IndexedWord> nodes = Generics.newHashSet();
for (TypedDependency dep : deps) {
nodes.add(dep.gov());
nodes.add(dep.dep());
}
// check the indices for all nodes
for (IndexedWord n : nodes) {
String text = n.value();
int index = n.get(CoreAnnotations.IndexAnnotation.class);
if (text.equals("Mary"))
assertEquals(1, index);
else if (text.equals("had"))
assertEquals(2, index);
else if (text.equals("a"))
assertEquals(3, index);
else if (text.equals("little"))
assertEquals(4, index);
else if (text.equals("lamb"))
assertEquals(5, index);
else if (text.equals("."))
assertEquals(6, index);
}
}
use of edu.stanford.nlp.trees.PennTreebankLanguagePack in project CoreNLP by stanfordnlp.
the class SceneGraphImagePCFGParser method main.
public static void main(String[] args) throws IOException {
LexicalizedParser parser = LexicalizedParser.getParserFromSerializedFile(PCFG_MODEL);
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
String filename = args[0];
BufferedReader reader = IOUtils.readerFromString(filename);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
SceneGraphImage img = SceneGraphImage.readFromJSON(line);
if (img == null) {
continue;
}
for (SceneGraphImageRegion region : img.regions) {
if (region.tokens != null) {
for (CoreLabel token : region.tokens) {
token.remove(CoreAnnotations.PartOfSpeechAnnotation.class);
}
Tree t = parser.apply(region.tokens);
region.gs = gsf.newGrammaticalStructure(t);
}
}
System.out.println(img.toJSON());
}
}
use of edu.stanford.nlp.trees.PennTreebankLanguagePack in project lucida by claritylab.
the class StanfordParser method initialize.
/**
* Initializes static resources.
*
* @throws Exception
*/
public static void initialize() throws Exception {
if (parser != null)
return;
Properties properties = Properties.loadFromClassName(StanfordParser.class.getName());
tlp = new PennTreebankLanguagePack();
String modelFile = properties.getProperty("modelFile");
if (modelFile == null)
throw new Exception("Required property '" + "modelFile' is undefined");
parser = new LexicalizedParser(modelFile);
}
Aggregations