You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2012/04/03 11:41:31 UTC
svn commit: r1308787 -
/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/muc/FullParseCorefEnhancerStream.java
Author: joern
Date: Tue Apr 3 09:41:31 2012
New Revision: 1308787
URL: http://svn.apache.org/viewvc?rev=1308787&view=rev
Log:
OPENNLP-56 Added a stream which can enhance Raw Coref Samples with a full parse.
Added:
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/muc/FullParseCorefEnhancerStream.java (with props)
Added: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/muc/FullParseCorefEnhancerStream.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/muc/FullParseCorefEnhancerStream.java?rev=1308787&view=auto
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/muc/FullParseCorefEnhancerStream.java (added)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/muc/FullParseCorefEnhancerStream.java Tue Apr 3 09:41:31 2012
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. 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.formats.muc;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import opennlp.tools.coref.CorefSample;
+import opennlp.tools.coref.mention.DefaultParse;
+import opennlp.tools.formats.muc.MucCorefContentHandler.CorefMention;
+import opennlp.tools.parser.AbstractBottomUpParser;
+import opennlp.tools.parser.Parse;
+import opennlp.tools.parser.Parser;
+import opennlp.tools.util.FilterObjectStream;
+import opennlp.tools.util.ObjectStream;
+import opennlp.tools.util.Span;
+
+public class FullParseCorefEnhancerStream extends FilterObjectStream<RawCorefSample, CorefSample> {
+
+ private final Parser parser;
+
+ public FullParseCorefEnhancerStream(Parser parser, ObjectStream<RawCorefSample> samples) {
+ super(samples);
+ this.parser = parser;
+ }
+
+ private static Parse createIncompleteParse(String tokens[]) {
+
+ // produce text
+ Span tokenSpans[] = new Span[tokens.length];
+ StringBuilder textBuilder = new StringBuilder();
+
+ for (int i = 0; i < tokens.length; i++) {
+
+ if (textBuilder.length() > 0) {
+ textBuilder.append(' ');
+ }
+
+ int startOffset = textBuilder.length();
+ textBuilder.append(tokens[i]);
+ tokenSpans[i] = new Span(startOffset, textBuilder.length());
+ }
+
+ String text = textBuilder.toString();
+
+ Parse p = new Parse(text, new Span(0, text.length()), AbstractBottomUpParser.INC_NODE, 0, 0);
+
+ for (int i = 0; i < tokenSpans.length; i++) {
+ Span tokenSpan = tokenSpans[i];
+ p.insert(new Parse(text, new Span(tokenSpan.getStart(), tokenSpan.getEnd()), AbstractBottomUpParser.TOK_NODE, 0, i));
+ }
+
+ return p;
+ }
+
+ public CorefSample read() throws IOException {
+
+ RawCorefSample sample = samples.read();
+
+ if (sample != null) {
+
+ List<Parse> enhancedParses = new ArrayList<Parse>();
+
+ List<String[]> sentences = sample.getTexts();
+ List<CorefMention[]> sentenceMentions = sample.getMentions();
+
+ for (int i = 0; i < sentences.size(); i++) {
+
+ String sentence[] = sentences.get(i);
+
+ Parse incompleteParse = createIncompleteParse(sentence);
+ Parse p = parser.parse(incompleteParse);
+
+ for (CorefMention mention : sentenceMentions.get(i)) {
+ DefaultParse.addMention(mention.id, mention.span, p.getTagNodes());
+ }
+
+ enhancedParses.add(p);
+ }
+
+ return new CorefSample(enhancedParses);
+ }
+ else {
+ return null;
+ }
+ }
+}
Propchange: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/muc/FullParseCorefEnhancerStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain