Search in sources :

Example 91 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class SemgrexPattern method main.

/**
   * Prints out all matches of a semgrex pattern on a file of dependencies.
   * <br>
   * Usage:<br>
   * java edu.stanford.nlp.semgraph.semgrex.SemgrexPattern [args]
   * <br>
   * See the help() function for a list of possible arguments to provide.
   */
public static void main(String[] args) throws IOException {
    Map<String, Integer> flagMap = Generics.newHashMap();
    flagMap.put(PATTERN, 1);
    flagMap.put(TREE_FILE, 1);
    flagMap.put(MODE, 1);
    flagMap.put(EXTRAS, 1);
    flagMap.put(CONLLU_FILE, 1);
    flagMap.put(OUTPUT_FORMAT_OPTION, 1);
    Map<String, String[]> argsMap = StringUtils.argsToMap(args, flagMap);
    args = argsMap.get(null);
    // TODO: allow patterns to be extracted from a file
    if (!(argsMap.containsKey(PATTERN)) || argsMap.get(PATTERN).length == 0) {
        help();
        System.exit(2);
    }
    SemgrexPattern semgrex = SemgrexPattern.compile(argsMap.get(PATTERN)[0]);
    String modeString = DEFAULT_MODE;
    if (argsMap.containsKey(MODE) && argsMap.get(MODE).length > 0) {
        modeString = argsMap.get(MODE)[0].toUpperCase();
    }
    SemanticGraphFactory.Mode mode = SemanticGraphFactory.Mode.valueOf(modeString);
    String outputFormatString = DEFAULT_OUTPUT_FORMAT;
    if (argsMap.containsKey(OUTPUT_FORMAT_OPTION) && argsMap.get(OUTPUT_FORMAT_OPTION).length > 0) {
        outputFormatString = argsMap.get(OUTPUT_FORMAT_OPTION)[0].toUpperCase();
    }
    OutputFormat outputFormat = OutputFormat.valueOf(outputFormatString);
    boolean useExtras = true;
    if (argsMap.containsKey(EXTRAS) && argsMap.get(EXTRAS).length > 0) {
        useExtras = Boolean.valueOf(argsMap.get(EXTRAS)[0]);
    }
    List<SemanticGraph> graphs = Generics.newArrayList();
    // TODO: allow other sources of graphs, such as dependency files
    if (argsMap.containsKey(TREE_FILE) && argsMap.get(TREE_FILE).length > 0) {
        for (String treeFile : argsMap.get(TREE_FILE)) {
            log.info("Loading file " + treeFile);
            MemoryTreebank treebank = new MemoryTreebank(new TreeNormalizer());
            treebank.loadPath(treeFile);
            for (Tree tree : treebank) {
                // TODO: allow other languages... this defaults to English
                SemanticGraph graph = SemanticGraphFactory.makeFromTree(tree, mode, useExtras ? GrammaticalStructure.Extras.MAXIMAL : GrammaticalStructure.Extras.NONE);
                graphs.add(graph);
            }
        }
    }
    if (argsMap.containsKey(CONLLU_FILE) && argsMap.get(CONLLU_FILE).length > 0) {
        CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
        for (String conlluFile : argsMap.get(CONLLU_FILE)) {
            log.info("Loading file " + conlluFile);
            Iterator<SemanticGraph> it = reader.getIterator(IOUtils.readerFromString(conlluFile));
            while (it.hasNext()) {
                SemanticGraph graph = it.next();
                graphs.add(graph);
            }
        }
    }
    for (SemanticGraph graph : graphs) {
        SemgrexMatcher matcher = semgrex.matcher(graph);
        if (!(matcher.find())) {
            continue;
        }
        if (outputFormat == OutputFormat.LIST) {
            log.info("Matched graph:");
            log.info(graph.toString(SemanticGraph.OutputFormat.LIST));
            boolean found = true;
            while (found) {
                log.info("Matches at: " + matcher.getMatch().value() + "-" + matcher.getMatch().index());
                List<String> nodeNames = Generics.newArrayList();
                nodeNames.addAll(matcher.getNodeNames());
                Collections.sort(nodeNames);
                for (String name : nodeNames) {
                    log.info("  " + name + ": " + matcher.getNode(name).value() + "-" + matcher.getNode(name).index());
                }
                log.info();
                found = matcher.find();
            }
        } else if (outputFormat == OutputFormat.OFFSET) {
            if (graph.vertexListSorted().isEmpty()) {
                continue;
            }
            System.out.printf("+%d %s%n", graph.vertexListSorted().get(0).get(CoreAnnotations.LineNumberAnnotation.class), argsMap.get(CONLLU_FILE)[0]);
        }
    }
}
Also used : SemanticGraphFactory(edu.stanford.nlp.semgraph.SemanticGraphFactory) CoNLLUDocumentReader(edu.stanford.nlp.trees.ud.CoNLLUDocumentReader) TreeNormalizer(edu.stanford.nlp.trees.TreeNormalizer) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) Tree(edu.stanford.nlp.trees.Tree) MemoryTreebank(edu.stanford.nlp.trees.MemoryTreebank)

Example 92 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class SemgrexDemo method main.

public static void main(String[] args) {
    String treeString = "(ROOT  (S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))";
    // Typically the tree is constructed by parsing or reading a
    // treebank.  This is just for example purposes
    Tree tree = Tree.valueOf(treeString);
    // This creates English uncollapsed dependencies as a
    // SemanticGraph.  If you are creating many SemanticGraphs, you
    // should use a GrammaticalStructureFactory and use it to generate
    // the intermediate GrammaticalStructure instead
    SemanticGraph graph = SemanticGraphFactory.generateUncollapsedDependencies(tree);
    // Alternatively, this could have been the Chinese params or any
    // other language supported.  As of 2014, only English and Chinese
    TreebankLangParserParams params = new EnglishTreebankParserParams();
    GrammaticalStructureFactory gsf = params.treebankLanguagePack().grammaticalStructureFactory(params.treebankLanguagePack().punctuationWordRejectFilter(), params.typedDependencyHeadFinder());
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    log.info(graph);
    SemgrexPattern semgrex = SemgrexPattern.compile("{}=A <<nsubj {}=B");
    SemgrexMatcher matcher = semgrex.matcher(graph);
    // ancestor of both "dog" and "my" via the nsubj relation
    while (matcher.find()) {
        log.info(matcher.getNode("A") + " <<nsubj " + matcher.getNode("B"));
    }
}
Also used : SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) GrammaticalStructureFactory(edu.stanford.nlp.trees.GrammaticalStructureFactory) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) Tree(edu.stanford.nlp.trees.Tree) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) TreebankLangParserParams(edu.stanford.nlp.parser.lexparser.TreebankLangParserParams) EnglishTreebankParserParams(edu.stanford.nlp.parser.lexparser.EnglishTreebankParserParams)

Example 93 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class Ssurgeon method testRead.

/**
   * Reads in the test file and prints readable to string (for debugging).
   * Input file consists of semantic graphs, in compact form.
   */
public void testRead(File tgtDirPath) throws Exception {
    List<SsurgeonPattern> patterns = readFromDirectory(tgtDirPath);
    System.out.println("Patterns, num = " + patterns.size());
    int num = 1;
    for (SsurgeonPattern pattern : patterns) {
        System.out.println("\n# " + (num++));
        System.out.println(pattern);
    }
    System.out.println("\n\nRESOURCES ");
    for (SsurgeonWordlist rsrc : inst().getResources()) {
        System.out.println(rsrc + "* * * * *");
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    boolean runFlag = true;
    Ssurgeon.inst().initLog(new File("./ssurgeon_run.log"));
    while (runFlag) {
        try {
            System.out.println("Enter a sentence:");
            String line = in.readLine();
            if (line.isEmpty()) {
                System.exit(0);
            }
            System.out.println("Parsing...");
            SemanticGraph sg = SemanticGraph.valueOf(line);
            System.out.println("Graph = " + sg);
            Collection<SemanticGraph> generated = Ssurgeon.inst().exhaustFromPatterns(patterns, sg);
            System.out.println("# generated = " + generated.size());
            int index = 1;
            for (SemanticGraph gsg : generated) {
                System.out.println("\n# " + index);
                System.out.println(gsg);
                index++;
            }
        } catch (Exception e) {
            log.error(e);
        }
    }
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 94 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class Ssurgeon method exhaustFromPatterns.

private List<SemanticGraph> exhaustFromPatterns(List<SsurgeonPattern> patternList, SemanticGraph sg, int depth) throws Exception {
    List<SemanticGraph> retList = new ArrayList<>();
    for (SsurgeonPattern pattern : patternList) {
        Collection<SemanticGraph> generated = pattern.execute(sg);
        for (SemanticGraph modGraph : generated) {
            //modGraph = SemanticGraphUtils.resetVerticeOrdering(modGraph);
            //modGraph.vertexList(true);
            //modGraph.edgeList(true);
            retList.add(modGraph);
        }
        if (log != null && generated.size() > 0) {
            log.info("* * * * * * * * * ** * * * * * * * * *");
            log.info("Exhaust from patterns, depth=" + depth);
            if (logPrefix != null)
                log.info(logPrefix);
            log.info("Pattern = '" + pattern.getUID() + "' generated " + generated.size() + " matches");
            log.info("= = = = = = = = = =\nSrc graph:\n" + sg.toString() + "\n= = = = = = = = = =\n");
            int index = 1;
            for (SemanticGraph genSg : generated) {
                log.info("REWRITE " + (index++));
                log.info(genSg.toString());
                log.info(". . . . .\n");
            }
        }
    }
    if (retList.size() > 0) {
        List<SemanticGraph> referenceList = new ArrayList<>(retList);
        for (SemanticGraph childGraph : referenceList) {
            if (depth < 3)
                retList.addAll(exhaustFromPatterns(patternList, childGraph, depth + 1));
        }
    }
    return retList;
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 95 with SemanticGraph

use of edu.stanford.nlp.semgraph.SemanticGraph in project CoreNLP by stanfordnlp.

the class Ssurgeon method expandFromPatterns.

/**
   * Given a list of SsurgeonPattern edit scripts, and a SemanticGraph
   * to operate over, returns a list of expansions of that graph, with
   * the result of each edit applied against a copy of the graph.
   */
public List<SemanticGraph> expandFromPatterns(List<SsurgeonPattern> patternList, SemanticGraph sg) throws Exception {
    List<SemanticGraph> retList = new ArrayList<>();
    for (SsurgeonPattern pattern : patternList) {
        Collection<SemanticGraph> generated = pattern.execute(sg);
        for (SemanticGraph orderedGraph : generated) {
            //orderedGraph.vertexList(true);
            //orderedGraph.edgeList(true);
            retList.add(orderedGraph);
            System.out.println("\ncompact = " + orderedGraph.toCompactString());
            System.out.println("regular=" + orderedGraph);
        }
        if (generated.size() > 0) {
            if (log != null) {
                log.info("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                log.info("Pre remove duplicates, num=" + generated.size());
            }
            SemanticGraphUtils.removeDuplicates(generated, sg);
            if (log != null) {
                log.info("Expand from patterns");
                if (logPrefix != null)
                    log.info(logPrefix);
                log.info("Pattern = '" + pattern.getUID() + "' generated " + generated.size() + " matches");
                log.info("= = = = = = = = = =\nSrc graph:\n" + sg + "\n= = = = = = = = = =\n");
                int index = 1;
                for (SemanticGraph genSg : generated) {
                    log.info("REWRITE " + (index++));
                    log.info(genSg.toString());
                    log.info(". . . . .\n");
                }
            }
        }
    }
    return retList;
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Aggregations

SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)126 IndexedWord (edu.stanford.nlp.ling.IndexedWord)57 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)53 CoreLabel (edu.stanford.nlp.ling.CoreLabel)51 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)47 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)24 Tree (edu.stanford.nlp.trees.Tree)20 CoreMap (edu.stanford.nlp.util.CoreMap)19 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)18 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)16 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)16 Annotation (edu.stanford.nlp.pipeline.Annotation)14 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)12 ArrayList (java.util.ArrayList)12 Mention (edu.stanford.nlp.coref.data.Mention)11 java.util (java.util)11 edu.stanford.nlp.util (edu.stanford.nlp.util)10 Properties (java.util.Properties)9 Collectors (java.util.stream.Collectors)9 CorefCoreAnnotations (edu.stanford.nlp.coref.CorefCoreAnnotations)8