Example usage for edu.stanford.nlp.trees LabeledScoredTreeFactory LabeledScoredTreeFactory

List of usage examples for edu.stanford.nlp.trees LabeledScoredTreeFactory LabeledScoredTreeFactory

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees LabeledScoredTreeFactory LabeledScoredTreeFactory.

Prototype

public LabeledScoredTreeFactory(LabelFactory lf) 

Source Link

Document

Make a TreeFactory that uses LabeledScoredTree trees, where the labels are as specified by the user.

Usage

From source file:Ceist.CeistView.java

License:Open Source License

@Action
public void showTreeView() {
    if (treeViewBox == null) {
        JFrame mainFrame = CeistApp.getApplication().getMainFrame();
        treeViewBox = new dlgTreeView(mainFrame, false);
        treeViewBox.setLocationRelativeTo(mainFrame);

        String ptbTreeString = "(ROOT (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (NN test))) (. .)))";
        try {//from  w  ww. j ava  2  s. c  om
            Tree tree = (new PennTreeReader(new StringReader(ptbTreeString),
                    new LabeledScoredTreeFactory(new StringLabelFactory()))).readTree();
            treeViewBox.mainPanel.setTree(tree);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.corenlp.internal.DKPro2CoreNlp.java

License:Open Source License

public static Tree createStanfordTree(ROOT root) {
    return createStanfordTree(root, new LabeledScoredTreeFactory(CoreLabel.factory()));
}

From source file:de.tudarmstadt.ukp.dkpro.core.corenlp.internal.DKPro2CoreNlp.java

License:Open Source License

/**
 * Recursively creates an edu.stanford.nlp.trees.Tree from a ROOT annotation It also saves the
 * whitespaces before and after a token as <code>CoreAnnotation.BeforeAnnotation</code> and
 * <code>CoreAnnotation.AfterAnnotation</code> in the respective label of the current node.
 * //from   w  ww  . j  a  va 2  s. co  m
 * @param root
 *            the ROOT annotation
 * @return an {@link Tree} object representing the syntax structure of the sentence
 */
public static Tree createStanfordTree(ROOT root, Map<Token, IndexedWord> aIdxTokens) {
    return createStanfordTree(root, new LabeledScoredTreeFactory(CoreLabel.factory()), aIdxTokens);
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.StanfordCoreferenceResolver.java

License:Open Source License

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    modelProvider.configure(aJCas.getCas());

    List<Tree> trees = new ArrayList<Tree>();
    List<CoreMap> sentences = new ArrayList<CoreMap>();
    List<List<CoreLabel>> sentenceTokens = new ArrayList<List<CoreLabel>>();
    for (ROOT root : select(aJCas, ROOT.class)) {
        // Copy all relevant information from the tokens
        List<CoreLabel> tokens = new ArrayList<CoreLabel>();
        for (Token token : selectCovered(Token.class, root)) {
            tokens.add(tokenToWord(token));
        }//from  w  w w . j  a v a 2 s. co m
        sentenceTokens.add(tokens);

        // SemanticHeadFinder (nonTerminalInfo) does not know about PRN0, so we have to replace
        // it with PRN to avoid NPEs.
        TreeFactory tFact = new LabeledScoredTreeFactory(CoreLabel.factory()) {
            @Override
            public Tree newTreeNode(String aParent, List<Tree> aChildren) {
                String parent = aParent;
                if ("PRN0".equals(parent)) {
                    parent = "PRN";
                }
                Tree node = super.newTreeNode(parent, aChildren);
                return node;
            }
        };

        // deep copy of the tree. These are modified inside coref!
        Tree treeCopy = TreeUtils.createStanfordTree(root, tFact).treeSkeletonCopy();
        treeCopy.indexSpans();
        trees.add(treeCopy);

        // Build the sentence
        CoreMap sentence = new CoreLabel();
        sentence.set(TreeAnnotation.class, treeCopy);
        sentence.set(TokensAnnotation.class, tokens);
        sentence.set(RootKey.class, root);
        sentences.add(sentence);

        // https://code.google.com/p/dkpro-core-asl/issues/detail?id=590
        // We currently do not copy over dependencies from the CAS. This is supposed to fill
        // in the dependencies so we do not get NPEs.
        TreebankLanguagePack tlp = new PennTreebankLanguagePack();
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory(tlp.punctuationWordRejectFilter(),
                tlp.typedDependencyHeadFinder());
        ParserAnnotatorUtils.fillInParseAnnotations(false, true, gsf, sentence, treeCopy,
                GrammaticalStructure.Extras.NONE);

        // https://code.google.com/p/dkpro-core-asl/issues/detail?id=582
        SemanticGraph deps = sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
        for (IndexedWord vertex : deps.vertexSet()) {
            vertex.setWord(vertex.value());
        }

        // merge the new CoreLabels with the tree leaves
        MentionExtractor.mergeLabels(treeCopy, tokens);
        MentionExtractor.initializeUtterance(tokens);
    }

    Annotation document = new Annotation(aJCas.getDocumentText());
    document.set(SentencesAnnotation.class, sentences);

    Coreferencer coref = modelProvider.getResource();

    // extract all possible mentions
    // Reparsing only works when the full CoreNLP pipeline system is set up! Passing false here
    // disables reparsing.
    RuleBasedCorefMentionFinder finder = new RuleBasedCorefMentionFinder(false);
    List<List<Mention>> allUnprocessedMentions = finder.extractPredictedMentions(document, 0,
            coref.corefSystem.dictionaries());

    // add the relevant info to mentions and order them for coref
    Map<Integer, CorefChain> result;
    try {
        Document doc = coref.mentionExtractor.arrange(document, sentenceTokens, trees, allUnprocessedMentions);
        result = coref.corefSystem.coref(doc);
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    }

    for (CorefChain chain : result.values()) {
        CoreferenceLink last = null;
        for (CorefMention mention : chain.getMentionsInTextualOrder()) {
            CoreLabel beginLabel = sentences.get(mention.sentNum - 1).get(TokensAnnotation.class)
                    .get(mention.startIndex - 1);
            CoreLabel endLabel = sentences.get(mention.sentNum - 1).get(TokensAnnotation.class)
                    .get(mention.endIndex - 2);
            CoreferenceLink link = new CoreferenceLink(aJCas, beginLabel.get(TokenKey.class).getBegin(),
                    endLabel.get(TokenKey.class).getEnd());

            if (mention.mentionType != null) {
                link.setReferenceType(mention.mentionType.toString());
            }

            if (last == null) {
                // This is the first mention. Here we'll initialize the chain
                CoreferenceChain corefChain = new CoreferenceChain(aJCas);
                corefChain.setFirst(link);
                corefChain.addToIndexes();
            } else {
                // For the other mentions, we'll add them to the chain.
                last.setNext(link);
            }
            last = link;

            link.addToIndexes();
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.StanfordDependencyConverter.java

License:Open Source License

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    String lang = language != null ? language : aJCas.getDocumentLanguage();

    if (!languagePacks.containsKey(lang)) {
        throw new AnalysisEngineProcessException(
                new IllegalStateException("Unsupported language [" + aJCas.getDocumentLanguage() + "]"));
    }/*from  w w  w . j  a va 2 s.c  om*/

    TreebankLanguagePack lp;
    try {
        lp = languagePacks.get(aJCas.getDocumentLanguage()).newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new AnalysisEngineProcessException(e);
    }

    List<CoreMap> sentences = new ArrayList<CoreMap>();
    for (ROOT root : select(aJCas, ROOT.class)) {
        // Copy all relevant information from the tokens
        List<Token> tokens = selectCovered(Token.class, root);
        List<CoreLabel> coreTokens = new ArrayList<CoreLabel>();
        for (Token token : tokens) {
            coreTokens.add(tokenToWord(token));
        }

        // SemanticHeadFinder (nonTerminalInfo) does not know about PRN0, so we have to replace
        // it with PRN to avoid NPEs.
        TreeFactory tFact = new LabeledScoredTreeFactory(CoreLabel.factory()) {
            @Override
            public Tree newTreeNode(String aParent, List<Tree> aChildren) {
                String parent = aParent;
                if ("PRN0".equals(parent)) {
                    parent = "PRN";
                }
                Tree node = super.newTreeNode(parent, aChildren);
                return node;
            }
        };

        Tree tree = TreeUtils.createStanfordTree(root, tFact);
        Trees.convertToCoreLabels(tree);
        tree.indexSpans();

        // Build the sentence
        CoreMap sentence = new CoreLabel();
        sentence.set(TreeAnnotation.class, tree);
        sentence.set(TokensAnnotation.class, coreTokens);
        sentence.set(RootKey.class, root);
        sentences.add(sentence);

        doCreateDependencyTags(aJCas, lp, tree, tokens);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.util.TreeUtils.java

License:Open Source License

/**
 * Recursively creates an edu.stanford.nlp.trees.Tree from a ROOT annotation It also saves the
 * whitespaces before and after a token as <code>CoreAnnotation.BeforeAnnotation</code> and
 * <code>CoreAnnotation.AfterAnnotation</code> in the respective label of the current node.
 * //  w w  w . j a v a  2s .  c  om
 * @param root
 *            the ROOT annotation
 * @return an {@link Tree} object representing the syntax structure of the sentence
 */
public static Tree createStanfordTree(Annotation root) {
    return createStanfordTree(root, new LabeledScoredTreeFactory(CoreLabel.factory()));
}

From source file:GUI.ParseTreeCorrector.java

/**
 *
 * @param parsedSentence//from   w  ww. j a v a2 s  .c o m
 * @param modifications
 */
public void TreeParseDisplay(String parsedSentence, Boolean modifications) {
    //======================================================================  
    parseTree.removeAll();
    expression.setText(sentences.get(sentenceOrder - 1));
    sentenceNumber.setText(String.valueOf(sentenceOrder / 2));

    if (modifications == Boolean.FALSE) {
        parse.setText(ParseDisplay(sentences.get(sentenceOrder)));
    }

    String parseString = parsedSentence;

    edu.stanford.nlp.parser.ui.TreeJPanel tjp = new edu.stanford.nlp.parser.ui.TreeJPanel();

    String ptbTreeString = (parseString);

    Tree tree = null;
    try {
        tree = (new PennTreeReader(new StringReader(ptbTreeString),
                new LabeledScoredTreeFactory(new StringLabelFactory()))).readTree();
    } catch (IOException ex) {
        Logger.getLogger(ParseTreeCorrector.class.getName()).log(Level.SEVERE, null, ex);
    }

    tjp.setTree(tree);
    tjp.setBackground(Color.white);
    tjp.setFont(new Font("Traditional arabic", 10, 10));
    JInternalFrame frame = new JInternalFrame();
    frame.getContentPane().add(tjp, BorderLayout.CENTER);
    frame.setPreferredSize(new Dimension(parseTree.getWidth(), parseTree.getHeight()));
    frame.setMaximizable(true);
    frame.setClosable(true);
    frame.setIconifiable(true);
    frame.setResizable(true);

    frame.pack();
    frame.setVisible(true);
    frame.setVisible(true);
    parseTree.add(frame);
    //====================================================================== 
}

From source file:nlpedit.ui.NLPTree.java

License:Open Source License

public Tree getTree() {
    Tree ntree = new LabeledScoredTreeNode();
    try {/*  www  .ja v a 2 s  .  co m*/
        ntree = (new PennTreeReader(new StringReader(getTreeString()),
                new LabeledScoredTreeFactory(new StringLabelFactory()))).readTree();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ntree;
}

From source file:reck.trees.RECKCTTreeNodeImpl.java

License:Open Source License

/**
 * Return a <code>TreeFactory</code> that produces trees of the
 * same type as the current <code>Tree</code>.  That is, this
 * implementation, will produce trees of type
 * <code>LabeledScoredTree(Node|Leaf)</code>.
 * The <code>Label</code> of <code>this</code>
 * is examined, and providing it is not <code>null</code>, a
 * <code>LabelFactory</code> which will produce that kind of
 * <code>Label</code> is supplied to the <code>TreeFactory</code>.
 * If the <code>Label</code> is <code>null</code>, a
 * <code>StringLabelFactory</code> will be used.
 * The factories returned on different calls a different: a new one is
 * allocated each time.//from  ww  w .ja va2  s  . c o  m
 *
 * @return a factory to produce labeled, scored trees
 */
public TreeFactory treeFactory() {
    LabelFactory lf;
    if (label() != null) {
        lf = label().labelFactory();
    } else {
        lf = StringLabel.factory();
    }
    return new LabeledScoredTreeFactory(lf);
}

From source file:reck.trees.RECKCTTreeNodeImpl.java

License:Open Source License

/**
 * Return a <code>TreeFactory</code> that produces trees of the
 * <code>LabeledScoredTree{Node|Leaf}</code> type, with
 * the <code>Label</code> made with the supplied
 * <code>LabelFactory</code>.
 * The factory returned is a different one each time
 *
 * @param lf The LabelFactory to use/*from   w  w w . j  av a 2  s .c o  m*/
 * @return a factory to produce labeled, scored trees
 */
public static TreeFactory factory(LabelFactory lf) {
    return new LabeledScoredTreeFactory(lf);
}