Search in sources :

Example 11 with TregexPattern

use of edu.stanford.nlp.trees.tregex.TregexPattern in project CoreNLP by stanfordnlp.

the class Tsurgeon method getOperationFromReader.

/**
   * Parses a tsurgeon script text input and compiles a tregex pattern and a list
   * of tsurgeon operations into a pair.
   *
   * @param reader Reader to read patterns from
   * @return A pair of a tregex and tsurgeon pattern read from a file, or {@code null}
   *    when the operations present in the Reader have been exhausted
   * @throws IOException If any IO problem
   */
public static Pair<TregexPattern, TsurgeonPattern> getOperationFromReader(BufferedReader reader, TregexPatternCompiler compiler) throws IOException {
    String patternString = getTregexPatternFromReader(reader);
    // log.info("Read tregex pattern: " + patternString);
    if (patternString.isEmpty()) {
        return null;
    }
    TregexPattern matchPattern = compiler.compile(patternString);
    TsurgeonPattern collectedPattern = getTsurgeonOperationsFromReader(reader);
    return new Pair<>(matchPattern, collectedPattern);
}
Also used : TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern) Pair(edu.stanford.nlp.util.Pair)

Example 12 with TregexPattern

use of edu.stanford.nlp.trees.tregex.TregexPattern in project CoreNLP by stanfordnlp.

the class SemanticHeadFinder method determineNonTrivialHead.

/**
   * Determine which daughter of the current parse tree is the
   * head.  It assumes that the daughters already have had their
   * heads determined.  Uses special rule for VP heads
   *
   * @param t The parse tree to examine the daughters of.
   *          This is assumed to never be a leaf
   * @return The parse tree that is the head
   */
@Override
protected Tree determineNonTrivialHead(Tree t, Tree parent) {
    String motherCat = tlp.basicCategory(t.label().value());
    if (DEBUG) {
        log.info("At " + motherCat + ", my parent is " + parent);
    }
    // downstream code was written assuming "not" would be the head...
    if (motherCat.equals("CONJP")) {
        for (TregexPattern pattern : headOfConjpTregex) {
            TregexMatcher matcher = pattern.matcher(t);
            if (matcher.matchesAt(t)) {
                return matcher.getNode("head");
            }
        }
    // if none of the above patterns match, use the standard method
    }
    if (motherCat.equals("SBARQ") || motherCat.equals("SINV")) {
        if (!makeCopulaHead) {
            for (TregexPattern pattern : headOfCopulaTregex) {
                TregexMatcher matcher = pattern.matcher(t);
                if (matcher.matchesAt(t)) {
                    return matcher.getNode("head");
                }
            }
        }
    // if none of the above patterns match, use the standard method
    }
    // do VPs with auxiliary as special case
    if ((motherCat.equals("VP") || motherCat.equals("SQ") || motherCat.equals("SINV"))) {
        Tree[] kids = t.children();
        if (DEBUG) {
            log.info("Semantic head finder: at VP");
            log.info("Class is " + t.getClass().getName());
            t.pennPrint(System.err);
        //log.info("hasVerbalAuxiliary = " + hasVerbalAuxiliary(kids, verbalAuxiliaries));
        }
        // looks for auxiliaries
        Tree[] tmpFilteredChildren = null;
        if (hasVerbalAuxiliary(kids, verbalAuxiliaries, true) || hasPassiveProgressiveAuxiliary(kids)) {
            // String[] how = new String[] {"left", "VP", "ADJP", "NP"};
            // Including NP etc seems okay for copular sentences but is
            // problematic for other auxiliaries, like 'he has an answer'
            String[] how;
            if (hasVerbalAuxiliary(kids, copulars, true)) {
                // Only allow ADJP in copular constructions
                // In constructions like "It gets cold", "get" should be the head
                how = new String[] { "left", "VP", "ADJP" };
            } else {
                how = new String[] { "left", "VP" };
            }
            if (tmpFilteredChildren == null) {
                tmpFilteredChildren = ArrayUtils.filter(kids, REMOVE_TMP_AND_ADV);
            }
            Tree pti = traverseLocate(tmpFilteredChildren, how, false);
            if (DEBUG) {
                log.info("Determined head (case 1) for " + t.value() + " is: " + pti);
            }
            if (pti != null) {
                return pti;
            // } else {
            // log.info("------");
            // log.info("SemanticHeadFinder failed to reassign head for");
            // t.pennPrint(System.err);
            // log.info("------");
            }
        }
        // looks for copular verbs
        if (hasVerbalAuxiliary(kids, copulars, false) && !isExistential(t, parent) && !isWHQ(t, parent)) {
            String[] how;
            if (motherCat.equals("SQ")) {
                how = new String[] { "right", "VP", "ADJP", "NP", "WHADJP", "WHNP" };
            } else {
                how = new String[] { "left", "VP", "ADJP", "NP", "WHADJP", "WHNP" };
            }
            // Avoid undesirable heads by filtering them from the list of potential children
            if (tmpFilteredChildren == null) {
                tmpFilteredChildren = ArrayUtils.filter(kids, REMOVE_TMP_AND_ADV);
            }
            Tree pti = traverseLocate(tmpFilteredChildren, how, false);
            // In SQ, only allow an NP to become head if there is another one to the left (then it's probably predicative)
            if (motherCat.equals("SQ") && pti != null && pti.label() != null && pti.label().value().startsWith("NP")) {
                boolean foundAnotherNp = false;
                for (Tree kid : kids) {
                    if (kid == pti) {
                        break;
                    } else if (kid.label() != null && kid.label().value().startsWith("NP")) {
                        foundAnotherNp = true;
                        break;
                    }
                }
                if (!foundAnotherNp) {
                    pti = null;
                }
            }
            if (DEBUG) {
                log.info("Determined head (case 2) for " + t.value() + " is: " + pti);
            }
            if (pti != null) {
                return pti;
            } else {
                if (DEBUG) {
                    log.info("------");
                    log.info("SemanticHeadFinder failed to reassign head for");
                    t.pennPrint(System.err);
                    log.info("------");
                }
            }
        }
    }
    Tree hd = super.determineNonTrivialHead(t, parent);
    if (DEBUG) {
        log.info("Determined head (case 3) for " + t.value() + " is: " + hd);
    }
    return hd;
}
Also used : TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern) TregexMatcher(edu.stanford.nlp.trees.tregex.TregexMatcher)

Example 13 with TregexPattern

use of edu.stanford.nlp.trees.tregex.TregexPattern in project CoreNLP by stanfordnlp.

the class GrammaticalRelation method getRelatedNodes.

/** Given a {@code Tree} node {@code t}, attempts to
   *  return a list of nodes to which node {@code t} has this
   *  grammatical relation, with {@code t} as the governor.
   *
   *  @param t Target for finding dependents of t related by this GR
   *  @param root The root of the Tree
   *  @return A Collection of dependent nodes to which t bears this GR
   */
public Collection<TreeGraphNode> getRelatedNodes(TreeGraphNode t, TreeGraphNode root, HeadFinder headFinder) {
    Set<TreeGraphNode> nodeList = new ArraySet<>();
    for (TregexPattern p : targetPatterns) {
        // cdm: I deleted: && nodeList.isEmpty()
        // Initialize the TregexMatcher with the HeadFinder so that we
        // can use the same HeadFinder through the entire process of
        // building the dependencies
        TregexMatcher m = p.matcher(root, headFinder);
        while (m.findAt(t)) {
            TreeGraphNode target = (TreeGraphNode) m.getNode("target");
            if (target == null) {
                throw new AssertionError("Expression has no target: " + p);
            }
            nodeList.add(target);
            if (DEBUG) {
                log.info("found " + this + "(" + t + "-" + t.headWordNode() + ", " + m.getNode("target") + "-" + ((TreeGraphNode) m.getNode("target")).headWordNode() + ") using pattern " + p);
                for (String nodeName : m.getNodeNames()) {
                    if (nodeName.equals("target"))
                        continue;
                    log.info("  node " + nodeName + ": " + m.getNode(nodeName));
                }
            }
        }
    }
    return nodeList;
}
Also used : TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern) ArraySet(edu.stanford.nlp.util.ArraySet) TregexMatcher(edu.stanford.nlp.trees.tregex.TregexMatcher)

Example 14 with TregexPattern

use of edu.stanford.nlp.trees.tregex.TregexPattern in project CoreNLP by stanfordnlp.

the class UniversalSemanticHeadFinder method determineNonTrivialHead.

/**
   * Determine which daughter of the current parse tree is the
   * head.  It assumes that the daughters already have had their
   * heads determined.  Uses special rule for VP heads
   *
   * @param t The parse tree to examine the daughters of.
   *          This is assumed to never be a leaf
   * @return The parse tree that is the head
   */
@Override
protected Tree determineNonTrivialHead(Tree t, Tree parent) {
    String motherCat = tlp.basicCategory(t.label().value());
    if (DEBUG) {
        log.info("At " + motherCat + ", my parent is " + parent);
    }
    // downstream code was written assuming "not" would be the head...
    if (motherCat.equals("CONJP")) {
        for (TregexPattern pattern : headOfConjpTregex) {
            TregexMatcher matcher = pattern.matcher(t);
            if (matcher.matchesAt(t)) {
                return matcher.getNode("head");
            }
        }
    // if none of the above patterns match, use the standard method
    }
    if (motherCat.equals("SBARQ") || motherCat.equals("SINV")) {
        if (!makeCopulaHead) {
            for (TregexPattern pattern : headOfCopulaTregex) {
                TregexMatcher matcher = pattern.matcher(t);
                if (matcher.matchesAt(t)) {
                    return matcher.getNode("head");
                }
            }
        }
    // if none of the above patterns match, use the standard method
    }
    // do VPs with auxiliary as special case
    if ((motherCat.equals("VP") || motherCat.equals("SQ") || motherCat.equals("SINV"))) {
        Tree[] kids = t.children();
        if (DEBUG) {
            log.info("Semantic head finder: at VP");
            log.info("Class is " + t.getClass().getName());
            t.pennPrint(System.err);
        //log.info("hasVerbalAuxiliary = " + hasVerbalAuxiliary(kids, verbalAuxiliaries));
        }
        // looks for auxiliaries
        Tree[] tmpFilteredChildren = null;
        if (hasVerbalAuxiliary(kids, verbalAuxiliaries, true) || hasPassiveProgressiveAuxiliary(kids)) {
            // String[] how = new String[] {"left", "VP", "ADJP", "NP"};
            // Including NP etc seems okay for copular sentences but is
            // problematic for other auxiliaries, like 'he has an answer'
            String[] how;
            if (hasVerbalAuxiliary(kids, copulars, true)) {
                // Only allow ADJP in copular constructions
                // In constructions like "It gets cold", "get" should be the head
                how = new String[] { "left", "VP", "ADJP" };
            } else {
                how = new String[] { "left", "VP" };
            }
            if (tmpFilteredChildren == null) {
                tmpFilteredChildren = ArrayUtils.filter(kids, REMOVE_TMP_AND_ADV);
            }
            Tree pti = traverseLocate(tmpFilteredChildren, how, false);
            if (DEBUG) {
                log.info("Determined head (case 1) for " + t.value() + " is: " + pti);
            }
            if (pti != null) {
                return pti;
            // } else {
            // log.info("------");
            // log.info("SemanticHeadFinder failed to reassign head for");
            // t.pennPrint(System.err);
            // log.info("------");
            }
        }
        // looks for copular verbs
        if (hasVerbalAuxiliary(kids, copulars, false) && !isExistential(t, parent) && !isWHQ(t, parent)) {
            String[][] how;
            //TODO: also allow ADVP to be heads
            if (motherCat.equals("SQ")) {
                how = new String[][] { { "right", "VP", "ADJP", "NP", "UCP", "PP", "WHADJP", "WHNP" } };
            } else {
                how = new String[][] { { "left", "VP", "ADJP", "NP", "UCP", "PP", "WHADJP", "WHNP" } };
            }
            // Avoid undesirable heads by filtering them from the list of potential children
            if (tmpFilteredChildren == null) {
                tmpFilteredChildren = ArrayUtils.filter(kids, REMOVE_TMP_AND_ADV);
            }
            Tree pti = null;
            for (int i = 0; i < how.length && pti == null; i++) {
                pti = traverseLocate(tmpFilteredChildren, how[i], false);
            }
            // In SQ, only allow an NP to become head if there is another one to the left (then it's probably predicative)
            if (motherCat.equals("SQ") && pti != null && pti.label() != null && pti.label().value().startsWith("NP")) {
                boolean foundAnotherNp = false;
                for (Tree kid : kids) {
                    if (kid == pti) {
                        break;
                    } else if (kid.label() != null && kid.label().value().startsWith("NP")) {
                        foundAnotherNp = true;
                        break;
                    }
                }
                if (!foundAnotherNp) {
                    pti = null;
                }
            }
            if (DEBUG) {
                log.info("Determined head (case 2) for " + t.value() + " is: " + pti);
            }
            if (pti != null) {
                return pti;
            } else {
                if (DEBUG) {
                    log.info("------");
                    log.info("SemanticHeadFinder failed to reassign head for");
                    t.pennPrint(System.err);
                    log.info("------");
                }
            }
        }
    }
    Tree hd = super.determineNonTrivialHead(t, parent);
    if (DEBUG) {
        log.info("Determined head (case 3) for " + t.value() + " is: " + hd);
    }
    return hd;
}
Also used : TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern) TregexMatcher(edu.stanford.nlp.trees.tregex.TregexMatcher)

Example 15 with TregexPattern

use of edu.stanford.nlp.trees.tregex.TregexPattern in project CoreNLP by stanfordnlp.

the class InputPanel method getMatchTreeVisitor.

/**
   * Check all active treebanks to find the trees that match the given pattern when interpreted
   * as a tregex pattern.
   *
   * @param patternString string version of the tregex pattern you wish to match
   * @param t The thread we are running on
   * @return tree visitor that contains the trees that were matched as well as the parts of those trees that matched
   */
private TRegexGUITreeVisitor getMatchTreeVisitor(String patternString, Thread t) {
    TRegexGUITreeVisitor vis = null;
    try {
        TregexPattern pattern = compiler.compile(patternString);
        //handles);
        vis = new TRegexGUITreeVisitor(pattern);
        List<FileTreeNode> treebanks = FilePanel.getInstance().getActiveTreebanks();
        double multiplier = 100.0 / treebanks.size();
        int treebankNum = 1;
        for (FileTreeNode treebank : treebanks) {
            if (t.isInterrupted()) {
                //get out as quickly as possible if interrupted
                t.interrupt();
                // cdm 2008: I added here resetting the buttons or else it didn't seem to happen; not quite sure this is the right place to do it but.
                SwingUtilities.invokeLater(() -> {
                    setTregexState(false);
                    InputPanel.this.searchThread = null;
                });
                return vis;
            }
            vis.setFilename(treebank.getFilename().intern());
            treebank.getTreebank().apply(vis);
            updateProgressBar(multiplier * treebankNum++);
        }
    } catch (OutOfMemoryError oome) {
        vis = null;
        doError("Sorry, search aborted as out of memory.\nTry either running Tregex with more memory or sticking to searches that don't produce thousands of matches.", oome);
    } catch (Exception e) {
        doError("Sorry, there was an error compiling or running the Tregex pattern.  Please press Help if you need assistance.", e);
    }
    return vis;
}
Also used : TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern)

Aggregations

TregexPattern (edu.stanford.nlp.trees.tregex.TregexPattern)29 TregexMatcher (edu.stanford.nlp.trees.tregex.TregexMatcher)16 Tree (edu.stanford.nlp.trees.Tree)8 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)6 CoreLabel (edu.stanford.nlp.ling.CoreLabel)6 ParserConstraint (edu.stanford.nlp.parser.common.ParserConstraint)6 Pair (edu.stanford.nlp.util.Pair)6 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)5 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)5 ArrayList (java.util.ArrayList)5 TregexParseException (edu.stanford.nlp.trees.tregex.TregexParseException)4 TsurgeonPattern (edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPattern)4 Mention (edu.stanford.nlp.coref.data.Mention)3 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)3 IntPair (edu.stanford.nlp.util.IntPair)3 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)3 SerializableFunction (edu.stanford.nlp.process.SerializableFunction)2 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)2 TreeReader (edu.stanford.nlp.trees.TreeReader)2