You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by co...@apache.org on 2011/01/07 04:21:52 UTC
svn commit: r1056179 - in /incubator/opennlp/trunk/opennlp-tools/src:
main/java/opennlp/tools/cmdline/ main/java/opennlp/tools/cmdline/chunker/
main/java/opennlp/tools/formats/ test/java/opennlp/tools/formats/
Author: colen
Date: Fri Jan 7 03:21:48 2011
New Revision: 1056179
URL: http://svn.apache.org/viewvc?rev=1056179&view=rev
Log:
OPENNLP-60 Initial version of the Chunker Converter for AD format
Added:
incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerConverterTool.java (with props)
incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStream.java (with props)
incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStreamFactory.java (with props)
incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/formats/ADChunkSampleStreamTest.java (with props)
Modified:
incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java
incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADParagraphStream.java
Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java?rev=1056179&r1=1056178&r2=1056179&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java Fri Jan 7 03:21:48 2011
@@ -25,6 +25,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import opennlp.tools.cmdline.chunker.ChunkerConverterTool;
import opennlp.tools.cmdline.chunker.ChunkerEvaluatorTool;
import opennlp.tools.cmdline.chunker.ChunkerMETool;
import opennlp.tools.cmdline.chunker.ChunkerTrainerTool;
@@ -102,6 +103,7 @@ public final class CLI {
tools.add(new ChunkerMETool());
tools.add(new ChunkerTrainerTool());
tools.add(new ChunkerEvaluatorTool());
+ tools.add(new ChunkerConverterTool());
// Parser
tools.add(new ParserTool());
Added: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerConverterTool.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerConverterTool.java?rev=1056179&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerConverterTool.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerConverterTool.java Fri Jan 7 03:21:48 2011
@@ -0,0 +1,58 @@
+/*
+ * 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.cmdline.chunker;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import opennlp.tools.chunker.ChunkSample;
+import opennlp.tools.cmdline.AbstractConverterTool;
+import opennlp.tools.cmdline.ObjectStreamFactory;
+import opennlp.tools.formats.ADChunkSampleStreamFactory;
+
+/**
+ * Tool to convert multiple data formats into native opennlp chunler training
+ * format.
+ */
+public class ChunkerConverterTool extends AbstractConverterTool<ChunkSample> {
+
+ private static final Map<String, ObjectStreamFactory<ChunkSample>> streamFactories;
+
+ static {
+ Map<String, ObjectStreamFactory<ChunkSample>> mutableStreamFactories =
+ new HashMap<String, ObjectStreamFactory<ChunkSample>>();
+
+ mutableStreamFactories.put("ad", new ADChunkSampleStreamFactory());
+
+ streamFactories = Collections.unmodifiableMap(mutableStreamFactories);
+ }
+
+ public String getName() {
+ return "ChunkerConverter";
+ }
+
+ public String getShortDescription() {
+ return "converts foreign data formats to native format";
+ }
+
+ @Override
+ protected ObjectStreamFactory<ChunkSample> createStreamFactory(String format) {
+ return streamFactories.get(format);
+ }
+}
Propchange: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerConverterTool.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStream.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStream.java?rev=1056179&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStream.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStream.java Fri Jan 7 03:21:48 2011
@@ -0,0 +1,231 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+
+import opennlp.tools.chunker.ChunkSample;
+import opennlp.tools.formats.ADParagraphStream.Paragraph;
+import opennlp.tools.formats.ADParagraphStream.ParagraphParser.Leaf;
+import opennlp.tools.formats.ADParagraphStream.ParagraphParser.Node;
+import opennlp.tools.formats.ADParagraphStream.ParagraphParser.TreeElement;
+import opennlp.tools.namefind.NameSample;
+import opennlp.tools.util.ObjectStream;
+import opennlp.tools.util.PlainTextByLineStream;
+
+/**
+ * Parser for Floresta Sita(c)tica Arvores Deitadas corpus, output to for the
+ * Portuguese Chunker training.
+ * <p>
+ * The heuristic to extract chunks where based o paper 'A Machine Learning
+ * Approach to Portuguese Clause Identification', (Eraldo Fernandes, Cicero
+ * Santos and Ruy Milidiú).<br>
+ * <p>
+ * Data can be found on this web site:<br>
+ * http://www.linguateca.pt/floresta/corpus.html
+ * <p>
+ * Information about the format:<br>
+ * Susana Afonso.
+ * "Ãrvores deitadas: Descrição do formato e das opções de análise na Floresta Sintáctica"
+ * .<br>
+ * 12 de Fevereiro de 2006.
+ * http://www.linguateca.pt/documentos/Afonso2006ArvoresDeitadas.pdf
+ * <p>
+ * Detailed info about the NER tagset:
+ * http://beta.visl.sdu.dk/visl/pt/info/portsymbol.html#semtags_names
+ * <p>
+ * <b>Note:</b> Do not use this class, internal use only!
+ */
+public class ADChunkSampleStream implements ObjectStream<ChunkSample> {
+
+ private final ObjectStream<ADParagraphStream.Paragraph> adSentenceStream;
+
+ private int start = -1;
+ private int end = -1;
+
+ private int index = 0;
+
+ /**
+ * Creates a new {@link NameSample} stream from a line stream, i.e.
+ * {@link ObjectStream}< {@link String}>, that could be a
+ * {@link PlainTextByLineStream} object.
+ *
+ * @param lineStream
+ * a stream of lines as {@link String}
+ */
+ public ADChunkSampleStream(ObjectStream<String> lineStream) {
+ this.adSentenceStream = new ADParagraphStream(lineStream);
+ }
+
+ /**
+ * Creates a new {@link NameSample} stream from a {@link InputStream}
+ *
+ * @param in
+ * the Corpus {@link InputStream}
+ * @param charsetName
+ * the charset of the Arvores Deitadas Corpus
+ */
+ public ADChunkSampleStream(InputStream in, String charsetName) {
+
+ try {
+ this.adSentenceStream = new ADParagraphStream(new PlainTextByLineStream(
+ in, charsetName));
+ } catch (UnsupportedEncodingException e) {
+ // UTF-8 is available on all JVMs, will never happen
+ throw new IllegalStateException(e);
+ }
+ }
+
+ public ChunkSample read() throws IOException {
+
+ Paragraph paragraph;
+ while ((paragraph = this.adSentenceStream.read()) != null) {
+
+ if (end > -1 && index >= end) {
+ // leave
+ return null;
+ }
+
+ if (start > -1 && index < start) {
+ index++;
+ // skip this one
+ } else {
+ Node root = paragraph.getRoot();
+ List<String> sentence = new ArrayList<String>();
+ List<String> tags = new ArrayList<String>();
+ List<String> target = new ArrayList<String>();
+
+ processRoot(root, sentence, tags, target);
+
+ if (sentence.size() > 0) {
+ index++;
+ return new ChunkSample(sentence, tags, target);
+ }
+
+ }
+
+ }
+ return null;
+ }
+
+ private void processRoot(Node root, List<String> sentence, List<String> tags,
+ List<String> target) {
+ if (root != null) {
+ TreeElement[] elements = root.getElements();
+ for (int i = 0; i < elements.length; i++) {
+ if (elements[i].isLeaf()) {
+ processLeaf((Leaf) elements[i], false, "O", sentence, tags, target);
+ } else {
+ processNode((Node) elements[i], sentence, tags, target);
+ }
+ }
+ }
+ }
+
+ private void processNode(Node node, List<String> sentence, List<String> tags,
+ List<String> target) {
+ String phraseTag = getChunkTag(node.getSyntacticTag());
+
+ TreeElement[] elements = node.getElements();
+ for (int i = 0; i < elements.length; i++) {
+ if (elements[i].isLeaf()) {
+ boolean isIntermediate = false;
+ if ( i > 0 && elements[i - 1].isLeaf() && phraseTag != null && !phraseTag.equals("O")) {
+ isIntermediate = true;
+ }
+ processLeaf((Leaf) elements[i], isIntermediate, phraseTag, sentence,
+ tags, target);
+ } else {
+ processNode((Node) elements[i], sentence, tags, target);
+ }
+ }
+ }
+
+ private void processLeaf(Leaf leaf, boolean isIntermediate, String phraseTag,
+ List<String> sentence, List<String> tags, List<String> target) {
+ String chunkTag;
+
+
+
+ if (leaf.getSyntacticTag() != null
+ && phraseTag.equals("O")) {
+ if(leaf.getSyntacticTag().endsWith("v-fin")) {
+ phraseTag = "VP";
+ } else if(leaf.getSyntacticTag().endsWith(":n")) {
+ phraseTag = "NP";
+ }
+ }
+
+ if (!phraseTag.equals("O")) {
+ if (isIntermediate) {
+ chunkTag = "I-" + phraseTag;
+ } else {
+ chunkTag = "B-" + phraseTag;
+ }
+ } else {
+ chunkTag = phraseTag;
+ }
+
+ sentence.add(leaf.getLexeme());
+ if (leaf.getSyntacticTag() == null) {
+ tags.add(leaf.getLexeme());
+ } else {
+ tags.add(getMorphologicalTag(leaf.getSyntacticTag()));
+ }
+ target.add(chunkTag);
+ }
+
+ private String getMorphologicalTag(String tag) {
+ return tag.substring(tag.lastIndexOf(":") + 1);
+ }
+
+ private String getChunkTag(String tag) {
+
+ String phraseTag = tag.substring(tag.lastIndexOf(":") + 1);
+
+ if (phraseTag.equals("np") || phraseTag.equals("ap")
+ || phraseTag.equals("advp") || phraseTag.equals("vp")
+ || phraseTag.equals("pp")) {
+ phraseTag = phraseTag.toUpperCase();
+ } else {
+ phraseTag = "O";
+ }
+ return phraseTag;
+ }
+
+ public void setStart(int aStart) {
+ this.start = aStart;
+ }
+
+ public void setEnd(int aEnd) {
+ this.end = aEnd;
+ }
+
+ public void reset() throws IOException, UnsupportedOperationException {
+ adSentenceStream.reset();
+ }
+
+ public void close() throws IOException {
+ adSentenceStream.close();
+ }
+
+}
Propchange: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStreamFactory.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStreamFactory.java?rev=1056179&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStreamFactory.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStreamFactory.java Fri Jan 7 03:21:48 2011
@@ -0,0 +1,88 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.nio.charset.Charset;
+
+import opennlp.tools.chunker.ChunkSample;
+import opennlp.tools.cmdline.ArgumentParser;
+import opennlp.tools.cmdline.ArgumentParser.OptionalParameter;
+import opennlp.tools.cmdline.ArgumentParser.ParameterDescription;
+import opennlp.tools.cmdline.CmdLineUtil;
+import opennlp.tools.cmdline.ObjectStreamFactory;
+import opennlp.tools.cmdline.TerminateToolException;
+import opennlp.tools.util.ObjectStream;
+
+/**
+ * A Factory to create a Arvores Deitadas ChunkStream from the command line
+ * utility.
+ * <p>
+ * <b>Note:</b> Do not use this class, internal use only!
+ */
+public class ADChunkSampleStreamFactory implements
+ ObjectStreamFactory<ChunkSample> {
+
+ interface Parameters {
+ @ParameterDescription(valueName = "encoding")
+ String getEncoding();
+
+ @ParameterDescription(valueName = "sampleData")
+ String getData();
+
+ @ParameterDescription(valueName = "start", description = "index of first sentence")
+ @OptionalParameter
+ Integer getStart();
+
+ @ParameterDescription(valueName = "end", description = "index of last sentence")
+ @OptionalParameter
+ Integer getEnd();
+ }
+
+ public String getUsage() {
+ return ArgumentParser.createUsage(Parameters.class);
+ }
+
+ public boolean validateArguments(String[] args) {
+ return ArgumentParser.validateArguments(args, Parameters.class);
+ }
+
+ public ObjectStream<ChunkSample> create(String[] args) {
+
+ Parameters params = ArgumentParser.parse(args, Parameters.class);
+
+ Charset encoding = CmdLineUtil.getEncodingParameter(args);
+
+ if (encoding == null) {
+ throw new TerminateToolException(1);
+ }
+
+ ADChunkSampleStream sampleStream = new ADChunkSampleStream(CmdLineUtil.openInFile(new File(params
+ .getData())), encoding.name());
+
+ if(params.getStart() != null && params.getStart() > -1) {
+ sampleStream.setStart(params.getStart());
+ }
+
+ if(params.getEnd() != null && params.getEnd() > -1) {
+ sampleStream.setEnd(params.getEnd());
+ }
+
+ return sampleStream;
+ }
+}
Propchange: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADChunkSampleStreamFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADParagraphStream.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADParagraphStream.java?rev=1056179&r1=1056178&r2=1056179&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADParagraphStream.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/formats/ADParagraphStream.java Fri Jan 7 03:21:48 2011
@@ -138,8 +138,28 @@ public class ADParagraphStream extends
if( element.isLeaf() ) {
if (nodeStack.isEmpty()) {
root.addElement(element);
- } else {
- nodeStack.peek().addElement(element);
+ } else {
+ // look for the node with the correct level
+ Node peek = nodeStack.peek();
+ if (element.level == 0) { // add to the root
+ nodeStack.firstElement().addElement(element);
+ } else {
+ Node parent = null;
+ int index = nodeStack.size() - 1;
+ while(parent == null) {
+ if(peek.getLevel() < element.getLevel()) {
+ parent = peek;
+ } else {
+ index--;
+ if(index > -1) {
+ peek = nodeStack.get(index);
+ } else {
+ parent = nodeStack.firstElement();
+ }
+ }
+ }
+ parent.addElement(element);
+ }
}
} else {
if (!nodeStack.isEmpty()) {
Added: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/formats/ADChunkSampleStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/formats/ADChunkSampleStreamTest.java?rev=1056179&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/formats/ADChunkSampleStreamTest.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/formats/ADChunkSampleStreamTest.java Fri Jan 7 03:21:48 2011
@@ -0,0 +1,83 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import opennlp.tools.chunker.ChunkSample;
+import opennlp.tools.util.PlainTextByLineStream;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class ADChunkSampleStreamTest {
+
+ List<ChunkSample> samples = new ArrayList<ChunkSample>();
+
+ @Test
+ public void testSimpleCount() throws IOException {
+ assertEquals(4, samples.size());
+ }
+
+ @Test
+ public void testChunks() throws IOException {
+
+ assertEquals("Inicia", samples.get(0).getSentence()[0]);
+ assertEquals("v-fin", samples.get(0).getTags()[0]);
+ assertEquals("B-NP", samples.get(0).getPreds()[2]);
+
+ assertEquals("em", samples.get(0).getSentence()[1]);
+ assertEquals("prp", samples.get(0).getTags()[1]);
+ assertEquals("B-PP", samples.get(0).getPreds()[1]);
+
+ assertEquals("o", samples.get(0).getSentence()[2]);
+ assertEquals("art", samples.get(0).getTags()[2]);
+ assertEquals("B-NP", samples.get(0).getPreds()[2]);
+
+ assertEquals("próximo", samples.get(0).getSentence()[3]);
+ assertEquals("adj", samples.get(0).getTags()[3]);
+ assertEquals("I-NP", samples.get(0).getPreds()[3]);
+
+ assertEquals("Casas", samples.get(3).getSentence()[0]);
+ assertEquals("n", samples.get(3).getTags()[0]);
+ assertEquals("B-NP", samples.get(3).getPreds()[0]);
+
+ }
+
+ @Before
+ public void setup() throws IOException {
+ InputStream in = ADParagraphStreamTest.class
+ .getResourceAsStream("/opennlp/tools/formats/ad.sample");
+
+ ADChunkSampleStream stream = new ADChunkSampleStream(
+ new PlainTextByLineStream(in, "UTF-8"));
+
+ ChunkSample sample = stream.read();
+
+ while (sample != null) {
+ samples.add(sample);
+ sample = stream.read();
+ }
+ }
+
+}
Propchange: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/formats/ADChunkSampleStreamTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain