You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2015/01/15 15:50:11 UTC
svn commit: r1652118 - in /uima/ruta/trunk/ruta-core/src:
main/java/org/apache/uima/ruta/engine/ test/java/org/apache/uima/ruta/engine/
Author: pkluegl
Date: Thu Jan 15 14:50:10 2015
New Revision: 1652118
URL: http://svn.apache.org/r1652118
Log:
UIMA-3866
- converted first bunch of analysis engines
Modified:
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/AnnotationWriter.java
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlAnnotator.java
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverter.java
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverterVisitor.java
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaCutter.java
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HtmlConverterTest.java
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/ViewWriterTest.java
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/AnnotationWriter.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/AnnotationWriter.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/AnnotationWriter.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/AnnotationWriter.java Thu Jan 15 14:50:10 2015
@@ -23,32 +23,62 @@ import java.io.File;
import java.io.IOException;
import org.apache.uima.UimaContext;
-import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.FileUtils;
+/**
+ * This Analysis Engine can be utilized to write the covered text of annotations in a text file,
+ * whereas each covered text is put into a new line. If the analysis engine, for example, is
+ * configured for the type <code>uima.example.Person</code>, then all covered texts of all Person
+ * annotations are stored in a text file, one person in each line. A descriptor file for this
+ * Analysis Engine is located in the folder <code>descriptor/utils</code> of a UIMA Ruta project.
+ *
+ */
public class AnnotationWriter extends JCasAnnotator_ImplBase {
- private static final String OUTPUT = "Output";
+ /**
+ * This string parameter specifies the absolute path of the resulting file named
+ * <code>output.txt</code>. However, if an annotation of the type
+ * <code>org.apache.uima.examples.SourceDocumentInformation</code> is given, then the value of
+ * this parameter is interpreted to be relative to the URI stored in the annotation and the name
+ * of the file will be adapted to the name of the source file. The default value of this parameter
+ * is <code>/../output/</code>.
+ */
+ public static final String PARAM_OUTPUT = "Output";
- private static final String ENCODING = "Encoding";
+ @ConfigurationParameter(name = PARAM_OUTPUT, mandatory = false, defaultValue = "/../output/")
+ private String output;
- private static final String TYPE = "Type";
+ /**
+ * This string parameter specifies the encoding of the resulting file. The default value of this
+ * parameter is <code>UTF-8</code>.
+ */
+ public static final String PARAM_ENCODING = "Encoding";
- private UimaContext context;
+ @ConfigurationParameter(name = PARAM_ENCODING, mandatory = false, defaultValue = "UTF-8")
+ private String encoding;
- private String output;
+ /**
+ * Only the covered texts of annotations of the type specified with this parameter are stored in
+ * the resulting file. The default value of this parameter is
+ * <code>uima.tcas.DocumentAnnotation</code>, which will store the complete document in a new
+ * file.
+ */
+ public static final String PARAM_TYPE = "Type";
+ @ConfigurationParameter(name = PARAM_TYPE, mandatory = false, defaultValue = ">uima.tcas.DocumentAnnotation")
private String type;
- private String encoding;
+ private UimaContext context;
@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
@@ -56,10 +86,10 @@ public class AnnotationWriter extends JC
if (aContext == null && context != null) {
aContext = context;
}
- if(aContext != null) {
- output = (String) aContext.getConfigParameterValue(OUTPUT);
- type = (String) aContext.getConfigParameterValue(TYPE);
- encoding = (String) aContext.getConfigParameterValue(ENCODING);
+ if (aContext != null) {
+ output = (String) aContext.getConfigParameterValue(PARAM_OUTPUT);
+ type = (String) aContext.getConfigParameterValue(PARAM_TYPE);
+ encoding = (String) aContext.getConfigParameterValue(PARAM_ENCODING);
this.context = aContext;
}
}
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlAnnotator.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlAnnotator.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlAnnotator.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlAnnotator.java Thu Jan 15 14:50:10 2015
@@ -23,26 +23,43 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.uima.UimaContext;
-import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
+/**
+ * This Analysis Engine provides support for HTML files by adding annotations for the HTML elements.
+ * Using the default values, the HTML Annotator creates annotations for each HTML element spanning
+ * the content of the element, whereas the most common elements are represented by own types. The
+ * document <code>This text is <b>bold</b>.</code>, for example, would be annotated
+ * with an annotation of the type <code>org.apache.uima.ruta.type.html.B</code> for the word
+ * <code>bold</code>. The HTML annotator can be configured in order to include the start and end
+ * elements in the created annotations. A descriptor file for this Analysis Engine is located in the
+ * folder <code>descriptor/utils</code> of a UIMA Ruta project.
+ *
+ */
public class HtmlAnnotator extends JCasAnnotator_ImplBase {
public static final String NAMESPACE = "org.apache.uima.ruta.type.html.";
- public static final String ONLY_CONTENT = "onlyContent";
+ /**
+ * This parameter specifies whether created annotations should cover only the content of the HTML
+ * elements or also their start and end elements. The default value is <code>true</code>.
+ */
+ public static final String PARAM_ONLY_CONTENT = "onlyContent";
+ @ConfigurationParameter(name = PARAM_ONLY_CONTENT, mandatory = false, defaultValue = "true")
private Boolean onlyContent;
@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
- onlyContent = (Boolean) aContext.getConfigParameterValue(ONLY_CONTENT);
+ onlyContent = (Boolean) aContext.getConfigParameterValue(PARAM_ONLY_CONTENT);
onlyContent = onlyContent == null ? true : onlyContent;
}
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverter.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverter.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverter.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverter.java Thu Jan 15 14:50:10 2015
@@ -30,13 +30,14 @@ import java.util.regex.Pattern;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.uima.UimaContext;
-import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
@@ -48,133 +49,172 @@ import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
/**
- * <p>
- * This is a basic html/xml to text converter that maintains annotations. <br />
- * Note that it is recommended to preprocess/prettify the html <i>before</i> any annotations are
- * added to the document.
- * </p>
- * <p>
- * how to handle: <br />
- * - TODO tables? <br />
- * - TODO lists (ul, ol) <br />
- * - TODO ... <br />
- * </p>
+ * This Analysis Engine is able to convert html content from a source view into a plain string
+ * representation stored in an output view. Especially, the Analysis Engine transfers annotations
+ * under consideration of the changed document text and annotation offsets in the new view. The copy
+ * process also sets features, however, features of type annotation are currently not supported.
+ * Note that if an annotation would have the same start and end positions in the new view, i.e., if
+ * it would be mapped to an annotation of length 0, it is not moved to the new view.
+ *
+ * The HTML Converter also supports heuristic and explicit conversion patterns which default to
+ * html4 decoding, e.g., "<![CDATA[ ]]>", "<![CDATA[<]]>", etc. Concepts like tables or
+ * lists are not supported.
+ *
+ * Note that in general it is suggested to run an html cleaner before any further processing to
+ * avoid problems with malformed html.
+ *
+ * A descriptor file for this Analysis Engine is located in the folder <code>descriptor/utils</code>
+ * of a UIMA Ruta project.
*
*/
public class HtmlConverter extends JCasAnnotator_ImplBase {
- // parameter names:
- public static final String NAMESPACE = "org.apache.uima.ruta.type.html.";
-
- public static final String OUTPUT_VIEW = "outputView";
- public static final String INPUT_VIEW = "inputView";
+ public static final String NAMESPACE = "org.apache.uima.ruta.type.html.";
- public static final String REPLACE_LINEBREAKS = "replaceLinebreaks";
+ private static final String DEFAULT_MODIFIED_VIEW = "plaintext";
- public static final String LINEBREAK_REPLACEMENT = "linebreakReplacement";
-
public static final String LINEBREAK = "\n";
+
+ /**
+ * This string parameter specifies the name of the new view. The default value is
+ * <code>plaintext</code>.
+ */
+ public static final String PARAM_OUTPUT_VIEW = "outputView";
- public static final String NEWLINE_INDUCING_TAGS = "newlineInducingTags";
+ @ConfigurationParameter(name = PARAM_OUTPUT_VIEW, mandatory = false, defaultValue = DEFAULT_MODIFIED_VIEW)
+ private String modifiedViewName;
- public static final String CONVERSION_POLICY = "conversionPolicy";
+ /**
+ * This string parameter can optionally be set to specify the name of the input view.
+ */
+ public static final String PARAM_INPUT_VIEW = "inputView";
- public static final String CONVERSION_PATTERNS = "conversionPatterns";
+ @ConfigurationParameter(name = PARAM_INPUT_VIEW, mandatory = false)
+ private String inputViewName;
- public static final String CONVERSION_REPLACEMENTS = "conversionReplacements";
-
- public static final String SKIP_WHITESPACES = "skipWhitespaces";
-
+ /**
+ * This boolean parameter determines if linebreaks inside the text nodes are kept or removed. The
+ * default behavior is <code>true</code>.
+ */
+ public static final String PARAM_REPLACE_LINEBREAKS = "replaceLinebreaks";
- // default values:
- private static final String DEFAULT_MODIFIED_VIEW = "plaintext";
+ @ConfigurationParameter(name = PARAM_REPLACE_LINEBREAKS, mandatory = false, defaultValue = "true")
+ private Boolean replaceLinebreaks;
- // variables:
- private String inputViewName;
+ /**
+ * This boolean parameter determines if the converter should skip whitespaces. Html documents
+ * often contains whitespaces for indentation and formatting, which should not be reproduced in
+ * the converted plain text document. If the parameter is set to false, then the whitespces are
+ * not removed. This behavior is useful, if not Html documents are converted, but XMl files. The
+ * default value is true.
+ */
+ public static final String PARAM_SKIP_WHITESPACES = "skipWhitespaces";
- private String modifiedViewName;
+ @ConfigurationParameter(name = PARAM_SKIP_WHITESPACES, mandatory = false, defaultValue = "true")
+ private Boolean skipWhitespaces;
- private Set<String> newlineInducingTags;
+ /**
+ * This string parameter determines the character sequence that replaces a linebreak. The default
+ * behavior is the empty string.
+ */
+ public static final String PARAM_LINEBREAK_REPLACEMENT = "linebreakReplacement";
+ @ConfigurationParameter(name = PARAM_LINEBREAK_REPLACEMENT, mandatory = false, defaultValue = "")
+ private String linebreakReplacement;
+
+ /**
+ * This string array parameter sets the names of the html tags that create linebreaks in the
+ * output view. The default is <code>br, p, div, ul, ol, dl, li, h1, ..., h6, blockquote</code>.
+ */
+ public static final String PARAM_NEWLINE_INDUCING_TAGS = "newlineInducingTags";
+
+ @ConfigurationParameter(name = PARAM_NEWLINE_INDUCING_TAGS, mandatory = false, defaultValue = {
+ "br", "p", "div", "ul", "ol", "dl", "li", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote" })
+ private String[] newlineInducingTags;
+
+ /**
+ * This string array parameter can be used to apply custom conversions. It defaults to a list of
+ * commonly used codes, e.g., <![CDATA[ ]]>, which are converted using html 4 entity
+ * unescaping. However, explicit conversion strings can also be passed via the parameter
+ * <code>conversionReplacements</code>. Remember to enable explicit conversion via
+ * <code>conversionPolicy</code> first.
+ */
+ public static final String PARAM_CONVERSION_PATTERNS = "conversionPatterns";
+
+ @ConfigurationParameter(name = PARAM_CONVERSION_PATTERNS, mandatory = false, defaultValue = {
+ " ", "«", "»", """, "&", "<", ">", "'", "§",
+ "¨", "©", "™", "®", "ö", "ä", "ü", " " })
private String[] conversionPatterns;
+ /**
+ * This string parameter determines the conversion policy used, either "heuristic", "explicit", or
+ * "none". When the value is "explicit", the parameters <code>conversionPatterns</code> and
+ * optionally <code>conversionReplacements</code> are considered. The "heuristic" conversion
+ * policy uses simple regular expressions to decode html4 entities such as "<![CDATA[ ]]>".
+ * The default behavior is "heuristic".
+ */
+ public static final String PARAM_CONVERSION_POLICY = "conversionPolicy";
+
+ @ConfigurationParameter(name = PARAM_CONVERSION_POLICY, mandatory = false, defaultValue = "heuristic")
+ private String conversionPolicy;
+
+ /**
+ * This string array parameter corresponds to <code>conversionPatterns</code> such that
+ * <code>conversionPatterns[i]</code> will be replaced by
+ * <code>conversionReplacements[i]</code>; replacements should be shorter than the source
+ * pattern. Per default, the replacement strings are computed using Html4 decoding. Remember to
+ * enable explicit conversion via <code>conversionPolicy</code> first.
+ */
+ public static final String PARAM_CONVERSION_REPLACEMENTS = "conversionReplacements";
+
+ @ConfigurationParameter(name = PARAM_CONVERSION_REPLACEMENTS, mandatory = false)
private String[] conversionReplacements;
- private Boolean replaceLinebreaks;
-
- private String linebreakReplacement;
-
- private Boolean skipWhitespaces;
- enum StringConversionPolicy {
- HEURISTIC, EXPLICIT, NONE
- }
-
- private StringConversionPolicy conversionPolicy;
private int[] map;
@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
- inputViewName = (String) aContext.getConfigParameterValue(INPUT_VIEW);
+ inputViewName = (String) aContext.getConfigParameterValue(PARAM_INPUT_VIEW);
inputViewName = StringUtils.isBlank(inputViewName) ? null : inputViewName;
- modifiedViewName = (String) aContext.getConfigParameterValue(OUTPUT_VIEW);
+ modifiedViewName = (String) aContext.getConfigParameterValue(PARAM_OUTPUT_VIEW);
modifiedViewName = StringUtils.isBlank(modifiedViewName) ? DEFAULT_MODIFIED_VIEW
: modifiedViewName;
- replaceLinebreaks = (Boolean) aContext.getConfigParameterValue(REPLACE_LINEBREAKS);
+ replaceLinebreaks = (Boolean) aContext.getConfigParameterValue(PARAM_REPLACE_LINEBREAKS);
replaceLinebreaks = replaceLinebreaks == null ? true : replaceLinebreaks;
- skipWhitespaces = (Boolean) aContext.getConfigParameterValue(SKIP_WHITESPACES);
+ skipWhitespaces = (Boolean) aContext.getConfigParameterValue(PARAM_SKIP_WHITESPACES);
skipWhitespaces = skipWhitespaces == null ? true : skipWhitespaces;
- linebreakReplacement = (String) aContext.getConfigParameterValue(LINEBREAK_REPLACEMENT);
+ linebreakReplacement = (String) aContext.getConfigParameterValue(PARAM_LINEBREAK_REPLACEMENT);
linebreakReplacement = linebreakReplacement == null ? "" : linebreakReplacement;
- String conversionPolicyString = (String) aContext.getConfigParameterValue(CONVERSION_POLICY);
- conversionPolicyString = conversionPolicyString == null ? null : conversionPolicyString
- .toLowerCase();
- if (StringUtils.isBlank(conversionPolicyString) || conversionPolicyString.equals("heuristic")) {
- conversionPolicy = StringConversionPolicy.HEURISTIC;
- } else if (conversionPolicyString.equals("explicit")) {
- conversionPolicy = StringConversionPolicy.EXPLICIT;
- } else if (conversionPolicyString.equals("none")) {
- conversionPolicy = StringConversionPolicy.NONE;
+ String conversionPolicy = (String) aContext.getConfigParameterValue(PARAM_CONVERSION_POLICY);
+ if (StringUtils.isBlank(conversionPolicy) || conversionPolicy.equals("heuristic")) {
+ conversionPolicy = "heuristic";
+ } else if (conversionPolicy.equals("explicit")) {
+ } else if (conversionPolicy.equals("none")) {
} else {
throw new ResourceInitializationException("illegal conversionPolicy parameter value",
new Object[0]);
}
- newlineInducingTags = new HashSet<String>();
- String[] nlTags = (String[]) aContext.getConfigParameterValue(NEWLINE_INDUCING_TAGS);
- if (nlTags == null || nlTags.length == 0) {
- newlineInducingTags.add("br");
- newlineInducingTags.add("p");
- newlineInducingTags.add("div");
- newlineInducingTags.add("ul");
- newlineInducingTags.add("ol");
- newlineInducingTags.add("dl");
- newlineInducingTags.add("li");
- newlineInducingTags.add("h1");
- newlineInducingTags.add("h2");
- newlineInducingTags.add("h3");
- newlineInducingTags.add("h4");
- newlineInducingTags.add("h5");
- newlineInducingTags.add("h6");
- newlineInducingTags.add("blockquote");
- } else {
- for (String nlTag : nlTags) {
- newlineInducingTags.add(nlTag);
- }
- // check assertions
- if (modifiedViewName.equals(inputViewName)) {
- throw new ResourceInitializationException("input and output view names must differ!",
- new Object[0]);
- }
+ String[] nlTags = (String[]) aContext.getConfigParameterValue(PARAM_NEWLINE_INDUCING_TAGS);
+ if (nlTags == null) {
+ newlineInducingTags = new String[] { "br", "p", "div", "ul", "ol", "dl", "li", "h1", "h2",
+ "h3", "h4", "h5", "h6", "blockquote" };
+
+ }
+ // check assertions
+ if (modifiedViewName.equals(inputViewName)) {
+ throw new ResourceInitializationException("input and output view names must differ!",
+ new Object[0]);
}
- conversionPatterns = (String[]) aContext.getConfigParameterValue(CONVERSION_PATTERNS);
+ conversionPatterns = (String[]) aContext.getConfigParameterValue(PARAM_CONVERSION_PATTERNS);
if (conversionPatterns == null) {
conversionPatterns = new String[] { " ", "«", "»", """, "&",
"<", ">", "'", "§", "¨", "©", "™", "®", "ö",
"ä", "ü", " " };
}
- conversionReplacements = (String[]) aContext.getConfigParameterValue(CONVERSION_REPLACEMENTS);
+ conversionReplacements = (String[]) aContext.getConfigParameterValue(PARAM_CONVERSION_REPLACEMENTS);
if (conversionReplacements == null) {
conversionReplacements = new String[conversionPatterns.length];
for (int i = 0; i < conversionPatterns.length; i++) {
@@ -237,9 +277,9 @@ public class HtmlConverter extends JCasA
if (replaceLinebreaks) {
visibleSpansSoFar = this.handleLinebreaksInDocumentText(visibleSpansSoFar, splitSeq);
}
- if (conversionPolicy == StringConversionPolicy.HEURISTIC) {
+ if (conversionPolicy.equals("heuristic")) {
visibleSpansSoFar = this.htmlDecoding(visibleSpansSoFar);
- } else if (conversionPolicy == StringConversionPolicy.EXPLICIT) {
+ } else if (conversionPolicy.equals("explicit")) {
for (int i = 0; i < conversionPatterns.length; i++) {
String pat = conversionPatterns[i];
String rep = conversionReplacements[i];
@@ -345,7 +385,7 @@ public class HtmlConverter extends JCasA
TreeSet<HtmlConverterPSpan> copy = new TreeSet<HtmlConverterPSpan>(visibleSpansSoFar);
Pattern patt = Pattern.compile("(&[a-zA-Z0-9]{2,6};)|(&#\\d{2,5};)");
-
+
for (HtmlConverterPSpan pSpan : visibleSpansSoFar) {
String spanTxt = pSpan.getTxt();
Matcher matcher = patt.matcher(spanTxt);
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverterVisitor.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverterVisitor.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverterVisitor.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/HtmlConverterVisitor.java Thu Jan 15 14:50:10 2015
@@ -19,7 +19,8 @@
package org.apache.uima.ruta.engine;
-import java.util.Set;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;
@@ -29,11 +30,7 @@ import org.htmlparser.Text;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.visitors.TextExtractingVisitor;
-/**
- * TODO comment / describe <br>
- * TODO write test(s)
- *
- */
+
public class HtmlConverterVisitor extends TextExtractingVisitor {
private boolean inBody = false;
@@ -46,10 +43,10 @@ public class HtmlConverterVisitor extend
private SortedSet<HtmlConverterPSpan> linebreaksFromHtmlTags = new TreeSet<HtmlConverterPSpan>();
- private Set<String> newlineInducingTags;
+ private Collection<String> newlineInducingTags;
- public HtmlConverterVisitor(Set<String> newlineInducingTags, boolean skipWhitespace) {
- this.newlineInducingTags = newlineInducingTags;
+ public HtmlConverterVisitor(String[] newlineInducingTags, boolean skipWhitespace) {
+ this.newlineInducingTags = Arrays.asList(newlineInducingTags);
this.skipWhitespace = skipWhitespace;
}
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaCutter.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaCutter.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaCutter.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaCutter.java Thu Jan 15 14:50:10 2015
@@ -27,7 +27,6 @@ import java.util.TreeSet;
import org.apache.commons.lang3.StringUtils;
import org.apache.uima.UimaContext;
-import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
@@ -35,6 +34,8 @@ import org.apache.uima.cas.Type;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
@@ -42,35 +43,55 @@ import org.apache.uima.ruta.UIMAConstant
import org.apache.uima.util.CasCopier;
import org.apache.uima.util.Level;
+/**
+ * This Analysis Engine is able to cut the document of the CAS. Only the text covered by annotations
+ * of the specified type will be retained and all other parts of the documents will be removed. The
+ * offsets of annotations in the index will be updated, but not feature structures nested as feature
+ * values.
+ *
+ */
public class RutaCutter extends JCasAnnotator_ImplBase {
- public static final String OUTPUT_VIEW = "outputView";
+ /**
+ * The name of the view, which will contain the modified CAS. The default value is
+ * <code>cutted</code>.
+ */
+ public static final String PARAM_OUTPUT_VIEW = "outputView";
- public static final String INPUT_VIEW = "inputView";
-
- public static final String KEEP = "keep";
-
- private static final String DEFAULT_OUTPUT_VIEW = "cutted";
+ @ConfigurationParameter(name = PARAM_OUTPUT_VIEW, mandatory = false, defaultValue = "cutted")
+ private String outputViewName;
- private static final String DEFAULT_INPUT_VIEW = "_InitialView";
+ /**
+ * The name of the view that should be processed. The default value is <code>_InitialView</code>.
+ */
+ public static final String PARAM_INPUT_VIEW = "inputView";
- // variables:
+ @ConfigurationParameter(name = PARAM_INPUT_VIEW, mandatory = false, defaultValue = "_InitialView")
private String inputViewName;
- private String outputViewName;
+ /**
+ * This string parameter specifies the complete name of a type. Only the text covered by
+ * annotations of this type will be retained and all other parts of the documents will be removed.
+ */
+ public static final String PARAM_KEEP = "keep";
+ @ConfigurationParameter(name = PARAM_KEEP, mandatory = false, defaultValue = "_InitialView")
private String keep;
+ private static final String DEFAULT_OUTPUT_VIEW = "cutted";
+
+ private static final String DEFAULT_INPUT_VIEW = "_InitialView";
+
private int[] map;
@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
- inputViewName = (String) aContext.getConfigParameterValue(INPUT_VIEW);
+ inputViewName = (String) aContext.getConfigParameterValue(PARAM_INPUT_VIEW);
inputViewName = StringUtils.isBlank(inputViewName) ? DEFAULT_INPUT_VIEW : inputViewName;
- outputViewName = (String) aContext.getConfigParameterValue(OUTPUT_VIEW);
+ outputViewName = (String) aContext.getConfigParameterValue(PARAM_OUTPUT_VIEW);
outputViewName = StringUtils.isBlank(outputViewName) ? DEFAULT_OUTPUT_VIEW : outputViewName;
- keep = (String) aContext.getConfigParameterValue(KEEP);
+ keep = (String) aContext.getConfigParameterValue(PARAM_KEEP);
}
@Override
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java Thu Jan 15 14:50:10 2015
@@ -23,7 +23,6 @@ import java.io.File;
import java.io.FileOutputStream;
import org.apache.uima.UimaContext;
-import org.apache.uima.analysis_component.JCasMultiplier_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.AbstractCas;
import org.apache.uima.cas.CAS;
@@ -32,28 +31,65 @@ import org.apache.uima.cas.Feature;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.impl.XmiCasSerializer;
import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.component.JCasMultiplier_ImplBase;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.CasCopier;
import org.apache.uima.util.XMLSerializer;
+/**
+ * This Analysis Engine is able to serialize the processed CAS to an XMI file whereas the the source
+ * and destination view can be specified A descriptor file for this Analysis Engine is located in
+ * the folder <quote>descriptor/utils</quote> of a UIMA Ruta project.
+ *
+ */
public class ViewWriter extends JCasMultiplier_ImplBase {
- public static final String OUTPUT = "output";
- public static final String INPUT_VIEW = "inputView";
- public static final String OUTPUT_VIEW = "outputView";
+ /**
+ * This string parameter specifies the absolute path of the resulting file named
+ * <quote>output.xmi</quote>. However, if an annotation of the type
+ * <quote>org.apache.uima.examples.SourceDocumentInformation</quote> is given, then the value of
+ * this parameter is interpreted to be relative to the URI stored in the annotation and the name
+ * of the file will be adapted to the name of the source file. If this functionality is activated
+ * in the preferences, then the UIMA Ruta Workbench adds the SourceDocumentInformation annotation
+ * when the user launches a script file.
+ */
+ public static final String PARAM_OUTPUT = "output";
+
+ @ConfigurationParameter(name = PARAM_OUTPUT, mandatory = false, defaultValue = "")
+ private String output;
+
- private String outputLocation;
+ /**
+ * The name of the view that should be stored in a file. The default value is "_InitialView".
+ */
+ public static final String PARAM_INPUT_VIEW = "inputView";
+
+ @ConfigurationParameter(name = PARAM_INPUT_VIEW, mandatory = false, defaultValue = "_InitialView")
private String inputView;
+
+
+ /**
+ * The name, which should be used, to store the view in the file. The default value is "_InitialView".
+ */
+ public static final String PARAM_OUTPUT_VIEW = "outputView";
+
+ @ConfigurationParameter(name = PARAM_OUTPUT_VIEW, mandatory = false, defaultValue = "_InitialView")
private String outputView;
+
+
+
+
+
private CAS outView;
@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
- outputLocation = (String) aContext.getConfigParameterValue(OUTPUT);
- inputView= (String) aContext.getConfigParameterValue(INPUT_VIEW);
- outputView= (String) aContext.getConfigParameterValue(OUTPUT_VIEW);
+ output = (String) aContext.getConfigParameterValue(PARAM_OUTPUT);
+ inputView = (String) aContext.getConfigParameterValue(PARAM_INPUT_VIEW);
+ outputView = (String) aContext.getConfigParameterValue(PARAM_OUTPUT_VIEW);
}
private static void writeXmi(CAS aCas, File name) throws Exception {
@@ -79,10 +115,10 @@ public class ViewWriter extends JCasMult
CAS cas = jcas.getCas();
Type sdiType = cas.getTypeSystem().getType(RutaEngine.SOURCE_DOCUMENT_INFORMATION);
- File file = new File(outputLocation);
- if(file.isDirectory()) {
+ File file = new File(output);
+ if (file.isDirectory()) {
String filename = "output.xmi";
- file = new File(outputLocation, filename);
+ file = new File(output, filename);
}
if (sdiType != null) {
FSIterator<AnnotationFS> sdiit = cas.getAnnotationIndex(sdiType).iterator();
@@ -96,13 +132,13 @@ public class ViewWriter extends JCasMult
name = name + ".xmi";
}
String parent = f.getParent().endsWith("/") ? f.getParent() : f.getParent() + "/";
- file = new File(parent + outputLocation, name);
+ file = new File(parent + output, name);
file.getParentFile().mkdirs();
}
}
CAS inView = cas.getView(inputView);
- if(outView == null) {
+ if (outView == null) {
outView = getContext().getEmptyCas(CAS.class);
}
outView.reset();
@@ -114,15 +150,15 @@ public class ViewWriter extends JCasMult
throw new AnalysisEngineProcessException(e);
}
}
-
+
@Override
public void collectionProcessComplete() {
- if(outView != null) {
+ if (outView != null) {
outView.release();
}
outView = null;
}
-
+
public boolean hasNext() throws AnalysisEngineProcessException {
return false;
}
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java Thu Jan 15 14:50:10 2015
@@ -23,7 +23,6 @@ import java.io.File;
import java.io.FileOutputStream;
import org.apache.uima.UimaContext;
-import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.FSIterator;
@@ -31,26 +30,46 @@ import org.apache.uima.cas.Feature;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.impl.XmiCasSerializer;
import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.XMLSerializer;
+/**
+ * This Analysis Engine is able to serialize the processed CAS to an XMI file. One use case for the
+ * XMI Writer is, for example, a rule-based sort, which stores the processed XMI files in different
+ * folder, dependent on the execution of the rules, e.g., whether a pattern of annotations occurs or
+ * not. A descriptor file for this Analysis Engine is located in the folder
+ * <code>descriptor/utils</code> of a UIMA Ruta project.
+ *
+ */
public class XMIWriter extends JCasAnnotator_ImplBase {
- private static final String OUTPUT = "Output";
-
- private UimaContext context;
+ /**
+ * This string parameter specifies the absolute path of the resulting file named
+ * <code>output.xmi</code>. However, if an annotation of the type
+ * <code>org.apache.uima.examples.SourceDocumentInformation</code> is given, then the value of
+ * this parameter is interpreted to be relative to the URI stored in the annotation and the name
+ * of the file will be adapted to the name of the source file. If this functionality is activated
+ * in the preferences, then the UIMA Ruta Workbench adds the SourceDocumentInformation annotation
+ * when the user launches a script file. The default value is <code>/../output/</code>
+ */
+ public static final String PARAM_OUTPUT = "Output";
+ @ConfigurationParameter(name = PARAM_OUTPUT, mandatory = false, defaultValue = "_InitialView")
private String output;
+ private UimaContext context;
+
@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
if (aContext == null && context != null) {
aContext = context;
}
- if(aContext != null) {
- output = (String) aContext.getConfigParameterValue(OUTPUT);
+ if (aContext != null) {
+ output = (String) aContext.getConfigParameterValue(PARAM_OUTPUT);
this.context = aContext;
}
}
Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HtmlConverterTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HtmlConverterTest.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HtmlConverterTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HtmlConverterTest.java Thu Jan 15 14:50:10 2015
@@ -111,10 +111,10 @@ public class HtmlConverterTest {
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
CAS cas = ae.newCAS();
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
- ae.setConfigParameterValue(HtmlConverter.CONVERSION_POLICY, "explicit");
- ae.setConfigParameterValue(HtmlConverter.CONVERSION_PATTERNS, new String[] { " " });
- ae.setConfigParameterValue(HtmlConverter.CONVERSION_REPLACEMENTS, new String[] { " " });
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_CONVERSION_POLICY, "explicit");
+ ae.setConfigParameterValue(HtmlConverter.PARAM_CONVERSION_PATTERNS, new String[] { " " });
+ ae.setConfigParameterValue(HtmlConverter.PARAM_CONVERSION_REPLACEMENTS, new String[] { " " });
ae.reconfigure();
cas.reset();
cas.setDocumentText(htmlDecoding);
@@ -152,7 +152,7 @@ public class HtmlConverterTest {
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
CAS cas = ae.newCAS();
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
ae.reconfigure();
cas.reset();
cas.setDocumentText(htmlDecoding);
@@ -190,7 +190,7 @@ public class HtmlConverterTest {
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
CAS cas = ae.newCAS();
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
ae.reconfigure();
cas.reset();
cas.setDocumentText(htmlUnix);
@@ -227,7 +227,7 @@ public class HtmlConverterTest {
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
CAS cas = ae.newCAS();
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
ae.reconfigure();
cas.reset();
cas.setDocumentText(htmlWin);
@@ -264,7 +264,7 @@ public class HtmlConverterTest {
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
CAS cas = ae.newCAS();
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
ae.reconfigure();
cas.reset();
cas.setDocumentText(htmlWithComments);
@@ -301,8 +301,8 @@ public class HtmlConverterTest {
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
CAS cas = ae.newCAS();
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
- ae.setConfigParameterValue(HtmlConverter.NEWLINE_INDUCING_TAGS, new String[] { "br" });
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_NEWLINE_INDUCING_TAGS, new String[] { "br" });
ae.reconfigure();
cas.reset();
cas.setDocumentText(htmlWin);
@@ -339,7 +339,7 @@ public class HtmlConverterTest {
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
CAS cas = ae.newCAS();
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
ae.reconfigure();
cas.reset();
cas.setDocumentText(htmlWithCommentsAndScript);
@@ -517,8 +517,8 @@ public class HtmlConverterTest {
String inputViewName = "inview";
- ae.setConfigParameterValue(HtmlConverter.INPUT_VIEW, inputViewName);
- ae.setConfigParameterValue(HtmlConverter.OUTPUT_VIEW, outputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_INPUT_VIEW, inputViewName);
+ ae.setConfigParameterValue(HtmlConverter.PARAM_OUTPUT_VIEW, outputViewName);
ae.reconfigure();
cas.reset();
CAS inview = cas.createView(inputViewName);
Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/ViewWriterTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/ViewWriterTest.java?rev=1652118&r1=1652117&r2=1652118&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/ViewWriterTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/ViewWriterTest.java Thu Jan 15 14:50:10 2015
@@ -66,9 +66,9 @@ public class ViewWriterTest {
ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
- ae.setConfigParameterValue(ViewWriter.INPUT_VIEW, NEW_VIEW);
- ae.setConfigParameterValue(ViewWriter.OUTPUT_VIEW, CAS.NAME_DEFAULT_SOFA);
- ae.setConfigParameterValue(ViewWriter.OUTPUT, tempFile.getAbsolutePath());
+ ae.setConfigParameterValue(ViewWriter.PARAM_INPUT_VIEW, NEW_VIEW);
+ ae.setConfigParameterValue(ViewWriter.PARAM_OUTPUT_VIEW, CAS.NAME_DEFAULT_SOFA);
+ ae.setConfigParameterValue(ViewWriter.PARAM_OUTPUT, tempFile.getAbsolutePath());
ae.reconfigure();
CAS cas = ae.newCAS();