opennlp.tools.parser.AbstractParserEventStream Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreemnets. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package opennlp.tools.parser;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import opennlp.model.Event;
import opennlp.tools.chunker.ChunkerContextGenerator;
import opennlp.tools.dictionary.Dictionary;
import opennlp.tools.parser.chunking.Parser;
import opennlp.tools.postag.DefaultPOSContextGenerator;
import opennlp.tools.postag.POSContextGenerator;
import opennlp.tools.util.ObjectStream;
/**
* Abstract class extended by parser event streams which perform tagging and chunking.
*/
public abstract class AbstractParserEventStream extends opennlp.tools.util.AbstractEventStream {
private ChunkerContextGenerator chunkerContextGenerator;
private POSContextGenerator tagContextGenerator;
protected HeadRules rules;
protected Set punctSet;
/**
* The type of events being generated by this event stream.
*/
protected ParserEventTypeEnum etype;
protected boolean fixPossesives;
protected Dictionary dict;
public AbstractParserEventStream(ObjectStream d, HeadRules rules, ParserEventTypeEnum etype, Dictionary dict) {
super(d);
this.dict = dict;
if (etype == ParserEventTypeEnum.CHUNK) {
this.chunkerContextGenerator = new ChunkContextGenerator();
}
else if (etype == ParserEventTypeEnum.TAG) {
this.tagContextGenerator = new DefaultPOSContextGenerator(null);
}
this.rules = rules;
punctSet = rules.getPunctuationTags();
this.etype = etype;
init();
}
@Override
protected Iterator createEvents(Parse sample) {
List newEvents = new ArrayList();
Parse.pruneParse(sample);
if (fixPossesives) {
Parse.fixPossesives(sample);
}
sample.updateHeads(rules);
Parse[] chunks = getInitialChunks(sample);
if (etype == ParserEventTypeEnum.TAG) {
addTagEvents(newEvents, chunks);
}
else if (etype == ParserEventTypeEnum.CHUNK) {
addChunkEvents(newEvents, chunks);
}
else {
addParseEvents(newEvents, Parser.collapsePunctuation(chunks,punctSet));
}
return newEvents.iterator();
}
protected void init() {
fixPossesives = false;
}
public AbstractParserEventStream(ObjectStream d, HeadRules rules, ParserEventTypeEnum etype) {
this(d,rules,etype,null);
}
public static Parse[] getInitialChunks(Parse p) {
List chunks = new ArrayList();
getInitialChunks(p, chunks);
return chunks.toArray(new Parse[chunks.size()]);
}
private static void getInitialChunks(Parse p, List ichunks) {
if (p.isPosTag()) {
ichunks.add(p);
}
else {
Parse[] kids = p.getChildren();
boolean allKidsAreTags = true;
for (int ci = 0, cl = kids.length; ci < cl; ci++) {
if (!kids[ci].isPosTag()) {
allKidsAreTags = false;
break;
}
}
if (allKidsAreTags) {
ichunks.add(p);
}
else {
for (int ci = 0, cl = kids.length; ci < cl; ci++) {
getInitialChunks(kids[ci], ichunks);
}
}
}
}
/**
* Produces all events for the specified sentence chunks
* and adds them to the specified list.
* @param newEvents A list of events to be added to.
* @param chunks Pre-chunked constituents of a sentence.
*/
protected abstract void addParseEvents(List newEvents, Parse[] chunks);
private void addChunkEvents(List chunkEvents, Parse[] chunks) {
List toks = new ArrayList();
List tags = new ArrayList();
List preds = new ArrayList();
for (int ci = 0, cl = chunks.length; ci < cl; ci++) {
Parse c = chunks[ci];
if (c.isPosTag()) {
toks.add(c.toString());
tags.add(c.getType());
preds.add(Parser.OTHER);
}
else {
boolean start = true;
String ctype = c.getType();
Parse[] kids = c.getChildren();
for (int ti=0,tl=kids.length;ti tagEvents, Parse[] chunks) {
List toks = new ArrayList();
List preds = new ArrayList();
for (int ci = 0, cl = chunks.length; ci < cl; ci++) {
Parse c = chunks[ci];
if (c.isPosTag()) {
toks.add(c.toString());
preds.add(c.getType());
}
else {
Parse[] kids = c.getChildren();
for (int ti=0,tl=kids.length;ti