Search in sources :

Example 6 with TsurgeonPattern

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

the class MWETreeVisitor method loadOps.

private static List<Pair<TregexPattern, TsurgeonPattern>> loadOps() {
    List<Pair<TregexPattern, TsurgeonPattern>> ops = new ArrayList<>();
    try {
        BufferedReader br = new BufferedReader(new StringReader(editStr));
        List<TsurgeonPattern> tsp = new ArrayList<>();
        for (String line; (line = br.readLine()) != null; ) {
            if (DEBUG)
                log.info("Pattern is " + line);
            TregexPattern matchPattern = TregexPattern.compile(line);
            if (DEBUG)
                log.info(" [" + matchPattern + "]");
            tsp.clear();
            while (continuing(line = br.readLine())) {
                TsurgeonPattern p = Tsurgeon.parseOperation(line);
                if (DEBUG)
                    log.info("Operation is " + line + " [" + p + "]");
                tsp.add(p);
            }
            if (!tsp.isEmpty()) {
                TsurgeonPattern tp = Tsurgeon.collectOperations(tsp);
                ops.add(new Pair<>(matchPattern, tp));
            }
        }
    // while not at end of file
    } catch (IOException ioe) {
        log.warn(ioe);
    }
    return ops;
}
Also used : TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) TsurgeonPattern(edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPattern) Pair(edu.stanford.nlp.util.Pair)

Example 7 with TsurgeonPattern

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

the class GenerateTrees method readGrammar.

public void readGrammar(BufferedReader bin) {
    try {
        String line;
        Section section = Section.TERMINALS;
        while ((line = bin.readLine()) != null) {
            line = line.trim();
            if (line.equals("")) {
                continue;
            }
            if (line.length() > 0 && line.charAt(0) == '#') {
                // skip comments
                continue;
            }
            try {
                Section newSection = Section.valueOf(line.toUpperCase(Locale.ROOT));
                section = newSection;
                if (section == Section.TSURGEON) {
                    // this will tregex pattern until it has eaten a blank
                    // line, then read tsurgeon until it has eaten another
                    // blank line.
                    Pair<TregexPattern, TsurgeonPattern> operation = Tsurgeon.getOperationFromReader(bin, compiler);
                    tsurgeons.add(operation);
                }
                continue;
            } catch (IllegalArgumentException e) {
            // never mind, not an enum
            }
            String[] pieces = line.split(" +");
            switch(section) {
                case TSURGEON:
                    {
                        throw new RuntimeException("Found a non-empty line in a tsurgeon section after reading the operation");
                    }
                case TERMINALS:
                    {
                        Counter<String> productions = terminals.get(pieces[0]);
                        if (productions == null) {
                            productions = new ClassicCounter<>();
                            terminals.put(pieces[0], productions);
                        }
                        for (int i = 1; i < pieces.length; ++i) {
                            productions.incrementCount(pieces[i]);
                        }
                        break;
                    }
                case NONTERMINALS:
                    {
                        Counter<List<String>> productions = nonTerminals.get(pieces[0]);
                        if (productions == null) {
                            productions = new ClassicCounter<>();
                            nonTerminals.put(pieces[0], productions);
                        }
                        String[] sublist = Arrays.copyOfRange(pieces, 1, pieces.length);
                        productions.incrementCount(Arrays.asList(sublist));
                    }
            }
        }
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) TregexPattern(edu.stanford.nlp.trees.tregex.TregexPattern) Counter(edu.stanford.nlp.stats.Counter) ClassicCounter(edu.stanford.nlp.stats.ClassicCounter) ClassicCounter(edu.stanford.nlp.stats.ClassicCounter) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) TsurgeonPattern(edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPattern)

Aggregations

TsurgeonPattern (edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPattern)7 TregexPattern (edu.stanford.nlp.trees.tregex.TregexPattern)4 ArrayList (java.util.ArrayList)4 Pair (edu.stanford.nlp.util.Pair)3 TregexMatcher (edu.stanford.nlp.trees.tregex.TregexMatcher)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 SpanishVerbStripper (edu.stanford.nlp.international.spanish.SpanishVerbStripper)1 AnCoraPronounDisambiguator (edu.stanford.nlp.international.spanish.process.AnCoraPronounDisambiguator)1 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)1 CoreLabel (edu.stanford.nlp.ling.CoreLabel)1 Label (edu.stanford.nlp.ling.Label)1 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)1 Counter (edu.stanford.nlp.stats.Counter)1 Tree (edu.stanford.nlp.trees.Tree)1 List (java.util.List)1