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;
}
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);
}
}
Aggregations