Search in sources :

Example 1 with PennTreebankLanguagePack

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));
     */
}
Also used : ArrayList(java.util.ArrayList) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 2 with PennTreebankLanguagePack

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);
    }
}
Also used : TypedDependency(edu.stanford.nlp.trees.TypedDependency) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) GrammaticalStructureFactory(edu.stanford.nlp.trees.GrammaticalStructureFactory) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) Tree(edu.stanford.nlp.trees.Tree) TreebankLanguagePack(edu.stanford.nlp.trees.TreebankLanguagePack) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 3 with PennTreebankLanguagePack

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());
    }
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) SceneGraphImage(edu.stanford.nlp.scenegraph.image.SceneGraphImage) LexicalizedParser(edu.stanford.nlp.parser.lexparser.LexicalizedParser) GrammaticalStructureFactory(edu.stanford.nlp.trees.GrammaticalStructureFactory) BufferedReader(java.io.BufferedReader) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) Tree(edu.stanford.nlp.trees.Tree) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack) TreebankLanguagePack(edu.stanford.nlp.trees.TreebankLanguagePack) SceneGraphImageRegion(edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack)

Example 4 with PennTreebankLanguagePack

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);
}
Also used : LexicalizedParser(edu.stanford.nlp.parser.lexparser.LexicalizedParser) Properties(info.ephyra.util.Properties) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack)

Aggregations

PennTreebankLanguagePack (edu.stanford.nlp.trees.PennTreebankLanguagePack)4 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)2 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)2 GrammaticalStructureFactory (edu.stanford.nlp.trees.GrammaticalStructureFactory)2 Tree (edu.stanford.nlp.trees.Tree)2 TreebankLanguagePack (edu.stanford.nlp.trees.TreebankLanguagePack)2 IndexedWord (edu.stanford.nlp.ling.IndexedWord)1 SceneGraphImage (edu.stanford.nlp.scenegraph.image.SceneGraphImage)1 SceneGraphImageRegion (edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion)1 GrammaticalStructure (edu.stanford.nlp.trees.GrammaticalStructure)1 TypedDependency (edu.stanford.nlp.trees.TypedDependency)1 Properties (info.ephyra.util.Properties)1 BufferedReader (java.io.BufferedReader)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1