You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2006/11/21 20:19:05 UTC
svn commit: r477832 [2/4] - in
/incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima:
examples/ examples/cas/ examples/casMultiplier/ examples/cpe/
examples/flow/ examples/tokenizer/ examples/xmi/ tutorial/ex1/
tutorial/ex2/ tutorial...
Modified: incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cas/RegExAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cas/RegExAnnotator.java?view=diff&rev=477832&r1=477831&r2=477832
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cas/RegExAnnotator.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cas/RegExAnnotator.java Tue Nov 21 11:19:03 2006
@@ -39,7 +39,6 @@
import org.apache.uima.analysis_engine.annotator.AnnotatorProcessException;
import org.apache.uima.analysis_engine.annotator.TextAnnotator;
import org.apache.uima.cas.CAS;
-import org.apache.uima.cas.CASException;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.FSTypeConstraint;
import org.apache.uima.cas.FeatureStructure;
@@ -50,423 +49,336 @@
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
-
/**
- * Annotator that find substrings of the input document that match
- * regular expressions.
+ * Annotator that find substrings of the input document that match regular expressions.
* <p>
- * There are two ways to specify the regular expressions - via configuration
- * parameters or via an external resource file.
+ * There are two ways to specify the regular expressions - via configuration parameters or via an
+ * external resource file.
* <p>
* This annotator takes the following optional configuration parameters:
* <ul>
- * <li><code>Patterns</code> - array of Strings indicating regular expressions
- * to match. The pattern language is described at
- * <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html">
- * http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html</a>)
- * </li>
- * <li><code>TypeNames</code> - array of Strings indicating names of Types to
- * be created from the patterns.
- * </li>
- * <li><code>ContainingAnnotationTypes</code> - an array of input annotation
- * types. This annotator will only produce new annotations that are
- * contained within existing annotaions of these types.
- * (This is optional.)
- * </li>
- * <li><code>AnnotateEntireContainedAnnotation</code> - When the ContainingAnnoationTypes
- * parameter is specified, a value of true for this parameter will cause the entire
- * containing annotation to be used as the span of the new annotation, rather than just
- * the span of the regular expression match. This can be used to "classify" previously created
- * annotations according to whether or not they contain text matching a regular expression.
- * </li>
+ * <li><code>Patterns</code> - array of Strings indicating regular expressions to match. The
+ * pattern language is described at <a
+ * href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html">
+ * http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html</a>) </li>
+ * <li><code>TypeNames</code> - array of Strings indicating names of Types to be created from the
+ * patterns. </li>
+ * <li><code>ContainingAnnotationTypes</code> - an array of input annotation types. This
+ * annotator will only produce new annotations that are contained within existing annotaions of
+ * these types. (This is optional.) </li>
+ * <li><code>AnnotateEntireContainedAnnotation</code> - When the ContainingAnnoationTypes
+ * parameter is specified, a value of true for this parameter will cause the entire containing
+ * annotation to be used as the span of the new annotation, rather than just the span of the regular
+ * expression match. This can be used to "classify" previously created annotations according to
+ * whether or not they contain text matching a regular expression. </li>
* </ul>
* <p>
- * The indices of the <code>Patterns</code> and <code>TypeNames</code> arrays
- * correspond, so that a substring that matches <code>Patterns[i]</code> will
- * result in an annotation of type <code>TypeNames[i]</code>.
+ * The indices of the <code>Patterns</code> and <code>TypeNames</code> arrays correspond, so
+ * that a substring that matches <code>Patterns[i]</code> will result in an annotation of type
+ * <code>TypeNames[i]</code>.
* <p>
- * It is also possible to provide an external resource file that declares
- * the annotation type names and the regular expressions to match. The
- * annotator will look for this file under the resource key "PatternFile".
- * The file format is as follows:
+ * It is also possible to provide an external resource file that declares the annotation type names
+ * and the regular expressions to match. The annotator will look for this file under the resource
+ * key "PatternFile". The file format is as follows:
* <ul>
- * <li>Lines starting with # or whitepsace are ignored</li>
- * <li>Lines starting with % indicate an annotation type</li>
- * <li>All other lines are regular expressions, using the same syntax
- * described for the <code>Patterns</code> configuration parameter.</li>
+ * <li>Lines starting with # or whitepsace are ignored</li>
+ * <li>Lines starting with % indicate an annotation type</li>
+ * <li>All other lines are regular expressions, using the same syntax described for the
+ * <code>Patterns</code> configuration parameter.</li>
* </ul>
- * If a regular expression is matched, it will be annotated with the last
- * annotation type declared (the nearest preceding line starting with %).
- *
+ * If a regular expression is matched, it will be annotated with the last annotation type declared
+ * (the nearest preceding line starting with %).
+ *
*
*/
-public class RegExAnnotator extends CasAnnotator_ImplBase
-{
- public static final String MESSAGE_DIGEST =
- "org.apache.uima.examples.cas.RegExAnnotator_Messages";
-
+public class RegExAnnotator extends CasAnnotator_ImplBase {
+ public static final String MESSAGE_DIGEST = "org.apache.uima.examples.cas.RegExAnnotator_Messages";
+
/**
- * Performs any startup tasks required by this annotator. This implementation
- * reads the configuration parmaeters and compiles the regular expressions.
+ * Performs any startup tasks required by this annotator. This implementation reads the
+ * configuration parmaeters and compiles the regular expressions.
*
* @see TextAnnotator#initialize(AnnotatorContext)
*/
- public void initialize(UimaContext aContext)
- throws ResourceInitializationException
- {
+ public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
- try
- {
- //Retrieve configuration parameters
- String[] patternStrings = (String[])
- getContext().getConfigParameterValue("Patterns");
- String[] typeNames =(String[])
- getContext().getConfigParameterValue("TypeNames");
- mContainingAnnotationTypeNames = (String[])
- getContext().getConfigParameterValue("ContainingAnnotationTypes");
- if (mContainingAnnotationTypeNames != null &&
- mContainingAnnotationTypeNames.length > 0)
- {
- mAnnotateEntireContainingAnnotation = (Boolean)
- getContext().getConfigParameterValue("AnnotateEntireContainingAnnotation");
- }
- else
- {
- mAnnotateEntireContainingAnnotation = Boolean.FALSE;
- }
-
- //create an ArrayList of type names and an ArrayList of pattern arrays,
- //where the indexes of the two lists corespond so that the patterns
- //at patternArray[i] correspond to the annotation type at
- //mTypeNames[i].
+ try {
+ // Retrieve configuration parameters
+ String[] patternStrings = (String[]) getContext().getConfigParameterValue("Patterns");
+ String[] typeNames = (String[]) getContext().getConfigParameterValue("TypeNames");
+ mContainingAnnotationTypeNames = (String[]) getContext().getConfigParameterValue(
+ "ContainingAnnotationTypes");
+ if (mContainingAnnotationTypeNames != null && mContainingAnnotationTypeNames.length > 0) {
+ mAnnotateEntireContainingAnnotation = (Boolean) getContext().getConfigParameterValue(
+ "AnnotateEntireContainingAnnotation");
+ } else {
+ mAnnotateEntireContainingAnnotation = Boolean.FALSE;
+ }
+
+ // create an ArrayList of type names and an ArrayList of pattern arrays,
+ // where the indexes of the two lists corespond so that the patterns
+ // at patternArray[i] correspond to the annotation type at
+ // mTypeNames[i].
mTypeNames = new ArrayList();
ArrayList patternArray = new ArrayList();
- if (patternStrings != null)
- {
- if (typeNames == null || typeNames.length != patternStrings.length)
- {
- //throw exception - error message in external message digest
- throw new ResourceInitializationException(
- MESSAGE_DIGEST, "type_pattern_array_length_mismatch",
- new Object[0]);
+ if (patternStrings != null) {
+ if (typeNames == null || typeNames.length != patternStrings.length) {
+ // throw exception - error message in external message digest
+ throw new ResourceInitializationException(MESSAGE_DIGEST,
+ "type_pattern_array_length_mismatch", new Object[0]);
}
- mTypeNames.addAll(Arrays.asList(typeNames));
+ mTypeNames.addAll(Arrays.asList(typeNames));
- for (int i = 0; i < patternStrings.length; i++)
- {
- patternArray.add(new String[]{patternStrings[i]});
- }
+ for (int i = 0; i < patternStrings.length; i++) {
+ patternArray.add(new String[] { patternStrings[i] });
+ }
}
-
- //if PatternFile resource exists, parse it and add to patternArray
+
+ // if PatternFile resource exists, parse it and add to patternArray
InputStream in = getContext().getResourceAsStream("PatternFile");
- if (in != null)
- {
- try
- {
+ if (in != null) {
+ try {
ArrayList patternsForCurrentType = new ArrayList();
boolean foundFirstType = false;
- //get buffered reader
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(in));
-
- //read lines from file
+ // get buffered reader
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+
+ // read lines from file
String line = reader.readLine();
- while (line != null)
- {
- if (!line.startsWith("#") && line.length() > 0 &&
- !Character.isWhitespace(line.charAt(0)))
- {
- //line is not a comment
- if (line.startsWith("%")) //annotation type name
+ while (line != null) {
+ if (!line.startsWith("#") && line.length() > 0
+ && !Character.isWhitespace(line.charAt(0))) {
+ // line is not a comment
+ if (line.startsWith("%")) // annotation type name
{
- //add pattern array for previous type (if any) to list
- if (foundFirstType)
- {
+ // add pattern array for previous type (if any) to list
+ if (foundFirstType) {
String[] pats = new String[patternsForCurrentType.size()];
patternsForCurrentType.toArray(pats);
patternArray.add(pats);
patternsForCurrentType.clear();
}
- //add new type name to mTypeNames list
+ // add new type name to mTypeNames list
mTypeNames.add(line.substring(1));
foundFirstType = true;
- }
- else //treat as regular expression
+ } else // treat as regular expression
{
patternsForCurrentType.add(line);
- }
+ }
}
line = reader.readLine();
}
- //add last group of pattersn to patternArray
+ // add last group of pattersn to patternArray
String[] pats = new String[patternsForCurrentType.size()];
patternsForCurrentType.toArray(pats);
patternArray.add(pats);
- }
- finally
- {
- if (in != null)
- {
+ } finally {
+ if (in != null) {
in.close();
}
- }
+ }
}
-
- //make sure there is at least one pattern
- if (patternArray.isEmpty())
- {
+
+ // make sure there is at least one pattern
+ if (patternArray.isEmpty()) {
throw new ResourceInitializationException(
- AnnotatorConfigurationException.ONE_PARAM_REQUIRED,
- new Object[]{"Patterns, Pattern File"});
+ AnnotatorConfigurationException.ONE_PARAM_REQUIRED,
+ new Object[] { "Patterns, Pattern File" });
}
-
- //compile regular expression patterns
+
+ // compile regular expression patterns
mPatterns = new Pattern[patternArray.size()][];
- for (int i = 0; i < patternArray.size(); i++)
- {
- String[] pats = (String[])patternArray.get(i);
+ for (int i = 0; i < patternArray.size(); i++) {
+ String[] pats = (String[]) patternArray.get(i);
mPatterns[i] = new Pattern[pats.length];
- for(int j = 0; j < mPatterns[i].length; j++)
- {
- try
- {
+ for (int j = 0; j < mPatterns[i].length; j++) {
+ try {
mPatterns[i][j] = Pattern.compile(pats[j]);
- //make sure no pattern matches the empty string - as this
- //would lead to infinite loops during processing
- if (mPatterns[i][j].matcher("").matches())
- {
- throw new ResourceInitializationException(
- MESSAGE_DIGEST,"regex_matches_empty_string",
- new Object[]{pats[j]});
+ // make sure no pattern matches the empty string - as this
+ // would lead to infinite loops during processing
+ if (mPatterns[i][j].matcher("").matches()) {
+ throw new ResourceInitializationException(MESSAGE_DIGEST,
+ "regex_matches_empty_string", new Object[] { pats[j] });
}
+ } catch (PatternSyntaxException e) {
+ throw new ResourceInitializationException(MESSAGE_DIGEST, "regex_syntax_error",
+ new Object[] { pats[j] }, e);
}
- catch(PatternSyntaxException e)
- {
- throw new ResourceInitializationException(
- MESSAGE_DIGEST,"regex_syntax_error",
- new Object[]{pats[j]}, e);
- }
- }
- }
- }
- catch(ResourceAccessException e)
- {
- throw new ResourceInitializationException(e);
- }
- catch(IOException e)
- {
- throw new ResourceInitializationException(e);
+ }
+ }
+ } catch (ResourceAccessException e) {
+ throw new ResourceInitializationException(e);
+ } catch (IOException e) {
+ throw new ResourceInitializationException(e);
}
- }
+ }
/**
- * Acquires references to CAS Type and Feature objects that are later used
- * during the {@link #process(CAS)} method.
+ * Acquires references to CAS Type and Feature objects that are later used during the
+ * {@link #process(CAS)} method.
*
* @see TextAnnotator#typeSystemInit(TypeSystem)
*/
- public void typeSystemInit(TypeSystem aTypeSystem)
- throws AnalysisEngineProcessException
- {
- //get references to annotation types we will create
+ public void typeSystemInit(TypeSystem aTypeSystem) throws AnalysisEngineProcessException {
+ // get references to annotation types we will create
mCASTypes = new Type[mTypeNames.size()];
- for (int i = 0; i < mTypeNames.size(); i++)
- {
- String curTypeName = (String)mTypeNames.get(i);
+ for (int i = 0; i < mTypeNames.size(); i++) {
+ String curTypeName = (String) mTypeNames.get(i);
mCASTypes[i] = aTypeSystem.getType(curTypeName);
- if (mCASTypes[i] == null)
- {
- throw new AnalysisEngineProcessException(
- AnnotatorInitializationException.TYPE_NOT_FOUND,
- new Object[]{this.getClass().getName(), curTypeName});
- }
+ if (mCASTypes[i] == null) {
+ throw new AnalysisEngineProcessException(AnnotatorInitializationException.TYPE_NOT_FOUND,
+ new Object[] { this.getClass().getName(), curTypeName });
+ }
}
- //get references to Containing Annotation Types
- if (mContainingAnnotationTypeNames == null)
- {
+ // get references to Containing Annotation Types
+ if (mContainingAnnotationTypeNames == null) {
mContainingAnnotationTypes = null;
- }
- else
- {
- mContainingAnnotationTypes =
- new Type[mContainingAnnotationTypeNames.length];
- for (int i = 0; i < mContainingAnnotationTypes.length; i++)
- {
- mContainingAnnotationTypes[i] =
- aTypeSystem.getType(mContainingAnnotationTypeNames[i]);
- if (mContainingAnnotationTypes[i] == null)
- {
- throw new AnalysisEngineProcessException(
- AnnotatorInitializationException.TYPE_NOT_FOUND,
- new Object[]{getClass().getName(),
- mContainingAnnotationTypeNames[i]});
+ } else {
+ mContainingAnnotationTypes = new Type[mContainingAnnotationTypeNames.length];
+ for (int i = 0; i < mContainingAnnotationTypes.length; i++) {
+ mContainingAnnotationTypes[i] = aTypeSystem.getType(mContainingAnnotationTypeNames[i]);
+ if (mContainingAnnotationTypes[i] == null) {
+ throw new AnalysisEngineProcessException(AnnotatorInitializationException.TYPE_NOT_FOUND,
+ new Object[] { getClass().getName(), mContainingAnnotationTypeNames[i] });
}
}
- }
+ }
}
-
-
+
/**
- * Invokes this annotator's analysis logic. This annotator uses the
- * java regular expression package to find annotations using the regular
- * expressions defined by its configuration parameters.
+ * Invokes this annotator's analysis logic. This annotator uses the java regular expression
+ * package to find annotations using the regular expressions defined by its configuration
+ * parameters.
*
- * @param aCAS the CAS to process
- * @param aResultSpec A list of outputs that this annotator should produce.
+ * @param aCAS
+ * the CAS to process
+ * @param aResultSpec
+ * A list of outputs that this annotator should produce.
*
- * @throws AnnotatorProcessException if a failure occurs during processing.
+ * @throws AnnotatorProcessException
+ * if a failure occurs during processing.
*
* @see CasAnnotator_ImplBase#process(CAS)
*/
- public void process(CAS aCAS)
- throws AnalysisEngineProcessException
- {
- try
- {
+ public void process(CAS aCAS) throws AnalysisEngineProcessException {
+ try {
String docText = aCAS.getDocumentText();
- //Determine which regions of the document we are going to annotate
+ // Determine which regions of the document we are going to annotate
int[] rangesToAnnotate = getRangesToAnnotate(aCAS);
-
- //We treat the rangesToAnnotate array as a list of (start,end) offset
- //pairs. Iterate through all of these pairs.
- for (int i = 0; i < rangesToAnnotate.length; i += 2)
- {
+
+ // We treat the rangesToAnnotate array as a list of (start,end) offset
+ // pairs. Iterate through all of these pairs.
+ for (int i = 0; i < rangesToAnnotate.length; i += 2) {
int startPos = rangesToAnnotate[i];
- int endPos = rangesToAnnotate[i+1];
- //get the substring of text to be annotated
- String subText = docText.substring(startPos,endPos);
-
- //iterate over all annotation types for which we have patterns
- for (int j = 0; j < mCASTypes.length; j++)
- {
- //see if the ResultSpec contains this type
- if (getResultSpecification().containsType(mCASTypes[j].getName()))
- {
- //try to match each pattern that we have for this annotation type
- for (int k = 0; k < mPatterns[j].length; k++)
- {
+ int endPos = rangesToAnnotate[i + 1];
+ // get the substring of text to be annotated
+ String subText = docText.substring(startPos, endPos);
+
+ // iterate over all annotation types for which we have patterns
+ for (int j = 0; j < mCASTypes.length; j++) {
+ // see if the ResultSpec contains this type
+ if (getResultSpecification().containsType(mCASTypes[j].getName())) {
+ // try to match each pattern that we have for this annotation type
+ for (int k = 0; k < mPatterns[j].length; k++) {
int pos = 0;
Matcher matcher = mPatterns[j][k].matcher(subText);
- while (pos < subText.length() && matcher.find(pos))
- {
+ while (pos < subText.length() && matcher.find(pos)) {
getContext().getLogger().log(Level.FINER,
- "RegEx match found: [" + matcher.group() + "]");
- //match found; extract locations of start and end of match
- //(or of entire containing annotation, if that option is on)
+ "RegEx match found: [" + matcher.group() + "]");
+ // match found; extract locations of start and end of match
+ // (or of entire containing annotation, if that option is on)
int annotStart, annotEnd;
- if (mAnnotateEntireContainingAnnotation.booleanValue())
- {
- annotStart = startPos;
- annotEnd = endPos;
- }
- else
- {
- annotStart = startPos + matcher.start();
+ if (mAnnotateEntireContainingAnnotation.booleanValue()) {
+ annotStart = startPos;
+ annotEnd = endPos;
+ } else {
+ annotStart = startPos + matcher.start();
annotEnd = startPos + matcher.end();
- }
- //create Annotation in CAS
- FeatureStructure fs = aCAS.createAnnotation(mCASTypes[j],
- annotStart, annotEnd);
- aCAS.getIndexRepository().addFS(fs);
+ }
+ // create Annotation in CAS
+ FeatureStructure fs = aCAS.createAnnotation(mCASTypes[j], annotStart, annotEnd);
+ aCAS.getIndexRepository().addFS(fs);
pos = annotEnd - startPos;
- }
- }
- }
- }
- }
- }
- catch(Exception e)
- {
+ }
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
throw new AnalysisEngineProcessException(e);
}
}
-
/**
- * Utility method that determines which subranges of the document text
- * should be annotated by this annotator. This is done as follows:
+ * Utility method that determines which subranges of the document text should be annotated by this
+ * annotator. This is done as follows:
* <ul>
- * <li>If <code>mContainingAnnotationTypes</code> is <code>null</code>, the
- * entire document is eligible for annotation.</li>
- * <li>If <code>mContainingAnnotationTypes</code> is not <code>null</code>,
- * then each of its elements is expected to be an Annotation Type name.
- * The CAS is queried for existing annotations of any of these Types,
- * and the only subranges of the document eligible for annotation are
- * those subranges contained within such annotations.</li>
+ * <li>If <code>mContainingAnnotationTypes</code> is <code>null</code>, the entire document
+ * is eligible for annotation.</li>
+ * <li>If <code>mContainingAnnotationTypes</code> is not <code>null</code>, then each of its
+ * elements is expected to be an Annotation Type name. The CAS is queried for existing annotations
+ * of any of these Types, and the only subranges of the document eligible for annotation are those
+ * subranges contained within such annotations.</li>
* </ul>
*
- * @param aCAS CAS currently being processed
+ * @param aCAS
+ * CAS currently being processed
*
- * @return an array of integers indicating the document subranges eligible
- * for annotation. Begin and end positions of the subranges are stored in
- * successive elements of the array. For example, elements 0 and 1 are
- * the start and end of the first subrange; elements 2 and 3 are the
- * start and end of the second subrange, and so on.
+ * @return an array of integers indicating the document subranges eligible for annotation. Begin
+ * and end positions of the subranges are stored in successive elements of the array. For
+ * example, elements 0 and 1 are the start and end of the first subrange; elements 2 and 3
+ * are the start and end of the second subrange, and so on.
*/
- protected int[] getRangesToAnnotate(CAS aCAS)
- throws CASException
- {
- if (mContainingAnnotationTypes == null ||
- mContainingAnnotationTypes.length == 0)
- {
- //ContainingAnnotationTypes is not set - the whole document is eligible
- return new int[]{0, aCAS.getDocumentText().length()};
- }
- else
- {
- //get iterator over all annotations in the CAS
+ protected int[] getRangesToAnnotate(CAS aCAS) {
+ if (mContainingAnnotationTypes == null || mContainingAnnotationTypes.length == 0) {
+ // ContainingAnnotationTypes is not set - the whole document is eligible
+ return new int[] { 0, aCAS.getDocumentText().length() };
+ } else {
+ // get iterator over all annotations in the CAS
FSIterator iterator = aCAS.getAnnotationIndex().iterator();
- //filter the iterator so that only instances of Types in the
- //mContainingAnnotationTypes array are returned
- FSTypeConstraint constraint =
- aCAS.getConstraintFactory().createTypeConstraint();
- for (int i = 0; i < mContainingAnnotationTypes.length; i++)
- {
- constraint.add(mContainingAnnotationTypes[i]);
+ // filter the iterator so that only instances of Types in the
+ // mContainingAnnotationTypes array are returned
+ FSTypeConstraint constraint = aCAS.getConstraintFactory().createTypeConstraint();
+ for (int i = 0; i < mContainingAnnotationTypes.length; i++) {
+ constraint.add(mContainingAnnotationTypes[i]);
}
iterator = aCAS.createFilteredIterator(iterator, constraint);
-
- //iterate over annotations and add them to an ArrayList
+
+ // iterate over annotations and add them to an ArrayList
List annotationList = new ArrayList();
- while (iterator.isValid())
- {
+ while (iterator.isValid()) {
annotationList.add(iterator.get());
iterator.moveToNext();
}
-
- //For each Annotation in the list, add its start and end
- //positions to the result array.
+
+ // For each Annotation in the list, add its start and end
+ // positions to the result array.
int numRanges = annotationList.size();
int[] result = new int[numRanges * 2];
- for (int j = 0; j < numRanges; j++)
- {
- AnnotationFS curFS = (AnnotationFS)annotationList.get(j);
- result[j*2] = curFS.getBegin();
- result[j*2 + 1] = curFS.getEnd();
- }
+ for (int j = 0; j < numRanges; j++) {
+ AnnotationFS curFS = (AnnotationFS) annotationList.get(j);
+ result[j * 2] = curFS.getBegin();
+ result[j * 2 + 1] = curFS.getEnd();
+ }
return result;
- }
+ }
}
-
-
+
/**
* The regular expression Patterns to be matched.
*/
private Pattern[][] mPatterns;
-
+
/**
- * The names of the CAS types that this annotator produces from the patterns
- * in {@link #mPatterns}.
+ * The names of the CAS types that this annotator produces from the patterns in {@link #mPatterns}.
*/
private ArrayList mTypeNames;
-
+
/**
- * The names of the CAS types within which this annotator will search for
- * new annotations. This may be null, indicating that the entire document
- * will be searched.
+ * The names of the CAS types within which this annotator will search for new annotations. This
+ * may be null, indicating that the entire document will be searched.
*/
private String[] mContainingAnnotationTypeNames;
@@ -480,9 +392,9 @@
*/
private Type[] mContainingAnnotationTypes;
- /**
- * Whether to annotate the entire span of the containing annotation when a match is found.
- */
+ /**
+ * Whether to annotate the entire span of the containing annotation when a match is found.
+ */
private Boolean mAnnotateEntireContainingAnnotation;
}
Modified: incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/CasMultiplierExampleApplication.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/CasMultiplierExampleApplication.java?view=diff&rev=477832&r1=477831&r2=477832
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/CasMultiplierExampleApplication.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/CasMultiplierExampleApplication.java Tue Nov 21 11:19:03 2006
@@ -32,79 +32,71 @@
import org.apache.uima.util.XMLInputSource;
/**
- * An example application that shows how to interact with a CasMultiplier.
- * A CasMultiplier is a type of Analysis Engine that outputs new CASes. One
- * use of a CasMultiplier is to divide a large CAS into smaller pieces - a
- * CasMultiplier that does this is called a "Segmenter".
+ * An example application that shows how to interact with a CasMultiplier. A CasMultiplier is a type
+ * of Analysis Engine that outputs new CASes. One use of a CasMultiplier is to divide a large CAS
+ * into smaller pieces - a CasMultiplier that does this is called a "Segmenter".
* <p>
- * This program takes two arguments -
+ * This program takes two arguments -
* <ul>
- * <li>The path to the Analysis Engine Descriptor
- * for the CasMultiplier to run (such as descriptors/cas_multiplier/SimpleTextSegmenter.xml
- * or descriptors/cas_multiplier/SegmenterAndTokenizerAE.xml)</li>
- * <li>The file name of a text document to analyze (to see the effect of segmentation,
- * choose a document larger than 100k characters, which is the default segment size
- * produced by the SimpleTextSegmenter.</li>
+ * <li>The path to the Analysis Engine Descriptor for the CasMultiplier to run (such as
+ * descriptors/cas_multiplier/SimpleTextSegmenter.xml or
+ * descriptors/cas_multiplier/SegmenterAndTokenizerAE.xml)</li>
+ * <li>The file name of a text document to analyze (to see the effect of segmentation, choose a
+ * document larger than 100k characters, which is the default segment size produced by the
+ * SimpleTextSegmenter.</li>
* </ul>
*/
-public class CasMultiplierExampleApplication
-{
+public class CasMultiplierExampleApplication {
static PrintStream outputStream;
-
- /**
- * Main program.
- *
- * @param args Command-line arguments - see class description
- */
- public static void main(String[] args)
- {
- try
- {
- //get Resource Specifier from XML file
+
+ /**
+ * Main program.
+ *
+ * @param args
+ * Command-line arguments - see class description
+ */
+ public static void main(String[] args) {
+ try {
+ // get Resource Specifier from XML file
XMLInputSource in = new XMLInputSource(args[0]);
- ResourceSpecifier specifier =
- UIMAFramework.getXMLParser().parseResourceSpecifier(in);
-
- //create AnalysisEngine
+ ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
+
+ // create AnalysisEngine
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
- //read input text file
+ // read input text file
File textFile = new File(args[1]);
FileInputStream fis = new FileInputStream(textFile);
- byte[] contents = new byte[(int)textFile.length()];
- fis.read( contents );
+ byte[] contents = new byte[(int) textFile.length()];
+ fis.read(contents);
String document = new String(contents, "UTF-8");
-
- //create a new CAS and set the document text
+
+ // create a new CAS and set the document text
CAS initialCas = ae.newCAS();
initialCas.setDocumentText(document);
-
- //pass the CAS to the AnalysisEngine and get back
- //a CasIterator for stepping over the output CASes that are produced.
+
+ // pass the CAS to the AnalysisEngine and get back
+ // a CasIterator for stepping over the output CASes that are produced.
CasIterator casIterator = ae.processAndOutputNewCASes(initialCas);
- while (casIterator.hasNext())
- {
+ while (casIterator.hasNext()) {
CAS outCas = casIterator.next();
-
- //dump the document text and annotations for this segment
+
+ // dump the document text and annotations for this segment
System.out.println("********* NEW SEGMENT *********");
System.out.println(outCas.getDocumentText());
- PrintAnnotations.printAnnotations(outCas, System.out);
+ PrintAnnotations.printAnnotations(outCas, System.out);
- //release the CAS (important)
+ // release the CAS (important)
outCas.release();
}
-
- //If there's a CAS Consumer inside this aggregate and we want
- //it's collectionProcessComplete method to be called, we need to
- //call it ourselves. If run inside a CPE this would get called
- //automatically.
+
+ // If there's a CAS Consumer inside this aggregate and we want
+ // it's collectionProcessComplete method to be called, we need to
+ // call it ourselves. If run inside a CPE this would get called
+ // automatically.
ae.collectionProcessComplete();
- }
- catch(Exception e)
- {
+ } catch (Exception e) {
e.printStackTrace();
- }
+ }
}
}
-
Modified: incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/SimpleTextSegmenter.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/SimpleTextSegmenter.java?view=diff&rev=477832&r1=477831&r2=477832
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/SimpleTextSegmenter.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/casMultiplier/SimpleTextSegmenter.java Tue Nov 21 11:19:03 2006
@@ -29,98 +29,95 @@
import org.apache.uima.resource.ResourceInitializationException;
/**
- * An example CasMultiplier, which breaks large text documents into
- * smaller segments. The minimum size of the segments as determined
- * by the "SegmentSize" configuration parameter, but the break between
- * segments will always occur at the next newline character, so segments will
- * not be exactly that size.
+ * An example CasMultiplier, which breaks large text documents into smaller segments. The minimum
+ * size of the segments as determined by the "SegmentSize" configuration parameter, but the break
+ * between segments will always occur at the next newline character, so segments will not be exactly
+ * that size.
*/
-public class SimpleTextSegmenter extends JCasMultiplier_ImplBase
-{
+public class SimpleTextSegmenter extends JCasMultiplier_ImplBase {
private String mDoc;
+
private int mPos;
+
private int mSegmentSize;
+
private String mDocUri;
-
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.uima.analysis_component.AnalysisComponent_ImplBase#initialize(org.apache.uima.UimaContext)
*/
- public void initialize(UimaContext aContext) throws ResourceInitializationException
- {
+ public void initialize(UimaContext aContext) throws ResourceInitializationException {
super.initialize(aContext);
- mSegmentSize = ((Integer)aContext.getConfigParameterValue("SegmentSize")).intValue();
+ mSegmentSize = ((Integer) aContext.getConfigParameterValue("SegmentSize")).intValue();
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see JCasMultiplier_ImplBase#process(JCas)
*/
- public void process(JCas aJCas) throws AnalysisEngineProcessException
- {
+ public void process(JCas aJCas) throws AnalysisEngineProcessException {
mDoc = aJCas.getDocumentText();
mPos = 0;
// retreive the filename of the input file from the CAS so that it can be added
// to each segment
- FSIterator it = aJCas.getJFSIndexRepository().getAnnotationIndex(SourceDocumentInformation.type)
- .iterator();
- if (it.hasNext())
- {
+ FSIterator it = aJCas.getJFSIndexRepository()
+ .getAnnotationIndex(SourceDocumentInformation.type).iterator();
+ if (it.hasNext()) {
SourceDocumentInformation fileLoc = (SourceDocumentInformation) it.next();
mDocUri = fileLoc.getUri();
- }
- else
- {
+ } else {
mDocUri = null;
}
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.uima.analysis_component.AnalysisComponent#hasNext()
*/
- public boolean hasNext() throws AnalysisEngineProcessException
- {
+ public boolean hasNext() throws AnalysisEngineProcessException {
return mPos < mDoc.length();
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.uima.analysis_component.AnalysisComponent#next()
*/
- public AbstractCas next() throws AnalysisEngineProcessException
- {
+ public AbstractCas next() throws AnalysisEngineProcessException {
int breakAt = mPos + mSegmentSize;
if (breakAt > mDoc.length())
breakAt = mDoc.length();
- //search for the next newline character. Note: this example segmenter implementation
- //assumes that the document contains many newlines. In the worst case, if this segmenter
- //is runon a document with no newlines, it will produce only one segment containing the
- //entire document text. A better implementation might specify a maximum segment size as
- //well as a minimum.
- while (breakAt < mDoc.length() && mDoc.charAt(breakAt-1) != '\n')
+ // search for the next newline character. Note: this example segmenter implementation
+ // assumes that the document contains many newlines. In the worst case, if this segmenter
+ // is runon a document with no newlines, it will produce only one segment containing the
+ // entire document text. A better implementation might specify a maximum segment size as
+ // well as a minimum.
+ while (breakAt < mDoc.length() && mDoc.charAt(breakAt - 1) != '\n')
breakAt++;
-
+
JCas jcas = getEmptyJCas();
- try
- {
+ try {
jcas.setDocumentText(mDoc.substring(mPos, breakAt));
- //if original CAS had SourceDocumentInformation, also add SourceDocumentInformatio
- //to each segment
- if (mDocUri != null)
- {
+ // if original CAS had SourceDocumentInformation, also add SourceDocumentInformatio
+ // to each segment
+ if (mDocUri != null) {
SourceDocumentInformation sdi = new SourceDocumentInformation(jcas);
sdi.setUri(mDocUri);
sdi.setOffsetInSource(mPos);
sdi.setDocumentSize(breakAt - mPos);
sdi.addToIndexes();
}
-
+
mPos = breakAt;
return jcas;
- }
- catch(Exception e)
- {
+ } catch (Exception e) {
jcas.release();
throw new AnalysisEngineProcessException(e);
}
}
-
-
+
}
Modified: incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/AnnotationPrinter.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/AnnotationPrinter.java?view=diff&rev=477832&r1=477831&r2=477832
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/AnnotationPrinter.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/AnnotationPrinter.java Tue Nov 21 11:19:03 2006
@@ -37,212 +37,203 @@
import org.apache.uima.util.ProcessTrace;
/**
- * An example of CAS Consumer. <br> AnnotationPrinter prints to an output file
- * all annotations in the CAS.
- * <br>
- * Parameters needed by the AnnotationPrinter are
+ * An example of CAS Consumer. <br>
+ * AnnotationPrinter prints to an output file all annotations in the CAS. <br>
+ * Parameters needed by the AnnotationPrinter are
* <ol>
* <li> "outputFile" : file to which the output files should be written.</li>
* </ol>
* <br>
- * These parameters are set in the initialize method to the values specified in
- * the descriptor file.
- * <br>These may also be set by the application by using the
- * setConfigParameterValue methods.
+ * These parameters are set in the initialize method to the values specified in the descriptor file.
+ * <br>
+ * These may also be set by the application by using the setConfigParameterValue methods.
*
*
*/
-public class AnnotationPrinter extends CasConsumer_ImplBase implements CasObjectProcessor {
- File outFile;
- FileWriter fileWriter ;
-
- public AnnotationPrinter( ) {
- }
-
- /**
- * Initializes this CAS Consumer with the parameters specified in the
- * descriptor.
- *
- * @throws ResourceInitializationException if there is error in
- * initializing the resources
- */
- public void initialize()
- throws ResourceInitializationException {
-
- // extract configuration parameter settings
- String oPath = (String) getUimaContext().getConfigParameterValue("outputFile");
-
- //Output file should be specified in the descriptor
- if(oPath ==null) {
- throw new ResourceInitializationException(ResourceInitializationException.CONFIG_SETTING_ABSENT, new Object []{"outputFile"} );
- }
- // If specified output directory does not exist, try to create it
- outFile = new File(oPath.trim());
- if(outFile.getParentFile() != null && !outFile.getParentFile().exists())
- {
- if (!outFile.getParentFile().mkdirs())
- throw new ResourceInitializationException(ResourceInitializationException.RESOURCE_DATA_NOT_VALID, new Object []{oPath ,"outputFile"} );
- }
- try
- {
- fileWriter = new FileWriter(outFile);
- }
- catch (IOException e)
- {
- throw new ResourceInitializationException(e);
- }
- }
-
-
- /** Processes the CasContainer which was populated by the TextAnalysisEngines.
- * <br> In this case, the CAS index is iterated over selected annotations
- * and printed out into an output file
- *
- * @param aCAS CasContainer which has been populated by the TAEs
- *
- * @throws ResourceProcessException if there is an error in processing the
- * Resource
- *
- * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(CAS)
- */
- public synchronized void processCas(CAS aCAS) throws ResourceProcessException
- {
+public class AnnotationPrinter extends CasConsumer_ImplBase implements CasObjectProcessor {
+ File outFile;
+
+ FileWriter fileWriter;
+
+ public AnnotationPrinter() {
+ }
+
+ /**
+ * Initializes this CAS Consumer with the parameters specified in the descriptor.
+ *
+ * @throws ResourceInitializationException
+ * if there is error in initializing the resources
+ */
+ public void initialize() throws ResourceInitializationException {
+
+ // extract configuration parameter settings
+ String oPath = (String) getUimaContext().getConfigParameterValue("outputFile");
+
+ // Output file should be specified in the descriptor
+ if (oPath == null) {
+ throw new ResourceInitializationException(
+ ResourceInitializationException.CONFIG_SETTING_ABSENT,
+ new Object[] { "outputFile" });
+ }
+ // If specified output directory does not exist, try to create it
+ outFile = new File(oPath.trim());
+ if (outFile.getParentFile() != null && !outFile.getParentFile().exists()) {
+ if (!outFile.getParentFile().mkdirs())
+ throw new ResourceInitializationException(
+ ResourceInitializationException.RESOURCE_DATA_NOT_VALID, new Object[] {
+ oPath, "outputFile" });
+ }
+ try {
+ fileWriter = new FileWriter(outFile);
+ } catch (IOException e) {
+ throw new ResourceInitializationException(e);
+ }
+ }
+
+ /**
+ * Processes the CasContainer which was populated by the TextAnalysisEngines. <br>
+ * In this case, the CAS index is iterated over selected annotations and printed out into an
+ * output file
+ *
+ * @param aCAS
+ * CasContainer which has been populated by the TAEs
+ *
+ * @throws ResourceProcessException
+ * if there is an error in processing the Resource
+ *
+ * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(CAS)
+ */
+ public synchronized void processCas(CAS aCAS) throws ResourceProcessException {
JCas jcas;
- try
- {
+ try {
jcas = aCAS.getJCas();
- }
- catch (CASException e)
- {
- throw new ResourceProcessException(e);
+ } catch (CASException e) {
+ throw new ResourceProcessException(e);
}
-
+
boolean titleP = false;
- String docUri = null;
- Iterator it = jcas.getJFSIndexRepository().getAnnotationIndex(SourceDocumentInformation.type).iterator();
- if (it.hasNext())
- {
- SourceDocumentInformation srcDocInfo = (SourceDocumentInformation)it.next();
- docUri = srcDocInfo.getUri();
- }
-
-
- //iterate and print annotations
- Iterator annotationIter = jcas.getJFSIndexRepository().getAnnotationIndex().iterator();
- while (annotationIter.hasNext()) {
- Annotation annot = (Annotation)annotationIter.next();
- if(titleP == false ) {
- try {
- fileWriter.write("\n\n<++++NEW DOCUMENT++++>\n");
- if (docUri != null)
- fileWriter.write("DOCUMENT URI:" + docUri + "\n");
- fileWriter.write("\n");
- } catch (IOException e) {
- throw new ResourceProcessException(e);
- }
- titleP = true;
- }
- // get the text that is enclosed within the annotation in the CAS
- String aText = annot.getCoveredText();
- aText = aText.replace('\n',' ');
- aText = aText.replace('\r',' ');
- //System.out.println( annot.getType().getName() + " "+aText);
- try {
- fileWriter.write(annot.getType().getName() + " "+aText+"\n");
- fileWriter.flush();
- } catch (IOException e) {
- throw new ResourceProcessException(e);
- }
- }
- }
-
-
- /**
- * Called when a batch of processing is completed.
- *
- * @param aTrace ProcessTrace object that will log events in this method.
- * @throws ResourceProcessException if there is an error in processing the
- * Resource
- * @throws IOException if there is an IO Error
- *
- * @see org.apache.uima.collection.CasConsumer#batchProcessComplete(ProcessTrace)
- **/
- public void batchProcessComplete(ProcessTrace aTrace)
- throws ResourceProcessException, IOException {
- // nothing to do in this case as AnnotationPrinter doesnot do
- //anything cumulatively
- }
-
-
-
- /**
- * Called when the entire collection is completed.
- *
- * @param aTrace ProcessTrace object that will log events in this method.
- * @throws ResourceProcessException if there is an error in processing the
- * Resource
- * @throws IOException if there is an IO Error
- * @see org.apache.uima.collection.CasConsumer#collectionProcessComplete(ProcessTrace)
- **/
- public void collectionProcessComplete(ProcessTrace aTrace)
- throws ResourceProcessException, IOException {
- if(fileWriter != null ) {
- fileWriter.close();
- }
- }
-
- /**
- * Reconfigures the parameters of this Consumer. <br>This is used in
- * conjunction with the setConfigurationParameterValue to set the
- * configuration parameter values to values other than the ones specified in
- * the descriptor.
- *
- * @throws ResourceConfigurationException if the configuration parameter
- * settings are invalid
- *
- * @see org.apache.uima.resource.ConfigurableResource#reconfigure()
- **/
- public void reconfigure() throws ResourceConfigurationException
- {
+ String docUri = null;
+ Iterator it = jcas.getJFSIndexRepository().getAnnotationIndex(SourceDocumentInformation.type)
+ .iterator();
+ if (it.hasNext()) {
+ SourceDocumentInformation srcDocInfo = (SourceDocumentInformation) it.next();
+ docUri = srcDocInfo.getUri();
+ }
+
+ // iterate and print annotations
+ Iterator annotationIter = jcas.getJFSIndexRepository().getAnnotationIndex().iterator();
+ while (annotationIter.hasNext()) {
+ Annotation annot = (Annotation) annotationIter.next();
+ if (titleP == false) {
+ try {
+ fileWriter.write("\n\n<++++NEW DOCUMENT++++>\n");
+ if (docUri != null)
+ fileWriter.write("DOCUMENT URI:" + docUri + "\n");
+ fileWriter.write("\n");
+ } catch (IOException e) {
+ throw new ResourceProcessException(e);
+ }
+ titleP = true;
+ }
+ // get the text that is enclosed within the annotation in the CAS
+ String aText = annot.getCoveredText();
+ aText = aText.replace('\n', ' ');
+ aText = aText.replace('\r', ' ');
+ // System.out.println( annot.getType().getName() + " "+aText);
+ try {
+ fileWriter.write(annot.getType().getName() + " " + aText + "\n");
+ fileWriter.flush();
+ } catch (IOException e) {
+ throw new ResourceProcessException(e);
+ }
+ }
+ }
+
+ /**
+ * Called when a batch of processing is completed.
+ *
+ * @param aTrace
+ * ProcessTrace object that will log events in this method.
+ * @throws ResourceProcessException
+ * if there is an error in processing the Resource
+ * @throws IOException
+ * if there is an IO Error
+ *
+ * @see org.apache.uima.collection.CasConsumer#batchProcessComplete(ProcessTrace)
+ */
+ public void batchProcessComplete(ProcessTrace aTrace) throws ResourceProcessException,
+ IOException {
+ // nothing to do in this case as AnnotationPrinter doesnot do
+ // anything cumulatively
+ }
+
+ /**
+ * Called when the entire collection is completed.
+ *
+ * @param aTrace
+ * ProcessTrace object that will log events in this method.
+ * @throws ResourceProcessException
+ * if there is an error in processing the Resource
+ * @throws IOException
+ * if there is an IO Error
+ * @see org.apache.uima.collection.CasConsumer#collectionProcessComplete(ProcessTrace)
+ */
+ public void collectionProcessComplete(ProcessTrace aTrace) throws ResourceProcessException,
+ IOException {
+ if (fileWriter != null) {
+ fileWriter.close();
+ }
+ }
+
+ /**
+ * Reconfigures the parameters of this Consumer. <br>
+ * This is used in conjunction with the setConfigurationParameterValue to set the configuration
+ * parameter values to values other than the ones specified in the descriptor.
+ *
+ * @throws ResourceConfigurationException
+ * if the configuration parameter settings are invalid
+ *
+ * @see org.apache.uima.resource.ConfigurableResource#reconfigure()
+ */
+ public void reconfigure() throws ResourceConfigurationException {
super.reconfigure();
- // extract configuration parameter settings
- String oPath =(String) getUimaContext().getConfigParameterValue("outputFile");
- File oFile = new File(oPath.trim());
- //if output file has changed, close exiting file and open new
- if(!oFile.equals(this.outFile)) {
- this.outFile = oFile;
- try {
- fileWriter.close();
-
- // If specified output directory does not exist, try to create it
- if(oFile.getParentFile() != null && !oFile.getParentFile().exists())
- {
- if (!oFile.getParentFile().mkdirs())
- throw new ResourceConfigurationException(ResourceInitializationException.RESOURCE_DATA_NOT_VALID, new Object []{oPath ,"outputFile"} );
- }
- fileWriter = new FileWriter(oFile);
- } catch (IOException e) {
- throw new ResourceConfigurationException();
- }
- }
- }
-
- /**
- * Called if clean up is needed in case of exit under error conditions.
- *
- * @see org.apache.uima.resource.Resource#destroy()
- **/
- public void destroy() {
- if(fileWriter != null ) {
- try
- {
+ // extract configuration parameter settings
+ String oPath = (String) getUimaContext().getConfigParameterValue("outputFile");
+ File oFile = new File(oPath.trim());
+ // if output file has changed, close exiting file and open new
+ if (!oFile.equals(this.outFile)) {
+ this.outFile = oFile;
+ try {
fileWriter.close();
- } catch (IOException e)
- {
+
+ // If specified output directory does not exist, try to create it
+ if (oFile.getParentFile() != null && !oFile.getParentFile().exists()) {
+ if (!oFile.getParentFile().mkdirs())
+ throw new ResourceConfigurationException(
+ ResourceInitializationException.RESOURCE_DATA_NOT_VALID, new Object[] {
+ oPath, "outputFile" });
+ }
+ fileWriter = new FileWriter(oFile);
+ } catch (IOException e) {
+ throw new ResourceConfigurationException();
+ }
+ }
+ }
+
+ /**
+ * Called if clean up is needed in case of exit under error conditions.
+ *
+ * @see org.apache.uima.resource.Resource#destroy()
+ */
+ public void destroy() {
+ if (fileWriter != null) {
+ try {
+ fileWriter.close();
+ } catch (IOException e) {
// ignore IOException on destroy
- }
- }
- }
+ }
+ }
+ }
}
Modified: incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/FileSystemCollectionReader.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/FileSystemCollectionReader.java?view=diff&rev=477832&r1=477831&r2=477832
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/FileSystemCollectionReader.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/FileSystemCollectionReader.java Tue Nov 21 11:19:03 2006
@@ -37,70 +37,65 @@
import org.apache.uima.util.ProgressImpl;
/**
- * A simple collection reader that reads documents from a directory
- * in the filesystem. It can be configured with the following parameters:
+ * A simple collection reader that reads documents from a directory in the filesystem. It can be
+ * configured with the following parameters:
* <ul>
- * <li><code>InputDirectory</code> - path to directory containing files</li>
- * <li><code>Encoding</code> (optional) - character encoding of the input
- * files</li>
- * <li><code>Language</code> (optional) - language of the input documents</li>
- * </ul>
+ * <li><code>InputDirectory</code> - path to directory containing files</li>
+ * <li><code>Encoding</code> (optional) - character encoding of the input files</li>
+ * <li><code>Language</code> (optional) - language of the input documents</li>
+ * </ul>
*
*
*/
-public class FileSystemCollectionReader extends CollectionReader_ImplBase
-{
+public class FileSystemCollectionReader extends CollectionReader_ImplBase {
/**
- * Name of configuration parameter that must be set to the path of
- * a directory containing input files.
+ * Name of configuration parameter that must be set to the path of a directory containing input
+ * files.
*/
public static final String PARAM_INPUTDIR = "InputDirectory";
/**
- * Name of configuration parameter that contains the character encoding used
- * by the input files. If not specified, the default system encoding will
- * be used.
+ * Name of configuration parameter that contains the character encoding used by the input files.
+ * If not specified, the default system encoding will be used.
*/
public static final String PARAM_ENCODING = "Encoding";
/**
- * Name of optional configuration parameter that contains the language of
- * the documents in the input directory. If specified this information will
- * be added to the CAS.
+ * Name of optional configuration parameter that contains the language of the documents in the
+ * input directory. If specified this information will be added to the CAS.
*/
public static final String PARAM_LANGUAGE = "Language";
-
+
private ArrayList mFiles;
+
private String mEncoding;
+
private String mLanguage;
+
private int mCurrentIndex;
/**
* @see org.apache.uima.collection.CollectionReader_ImplBase#initialize()
*/
- public void initialize() throws ResourceInitializationException
- {
- File directory = new File(((String)getConfigParameterValue(PARAM_INPUTDIR)).trim());
- mEncoding = (String)getConfigParameterValue(PARAM_ENCODING);
- mLanguage = (String)getConfigParameterValue(PARAM_LANGUAGE);
+ public void initialize() throws ResourceInitializationException {
+ File directory = new File(((String) getConfigParameterValue(PARAM_INPUTDIR)).trim());
+ mEncoding = (String) getConfigParameterValue(PARAM_ENCODING);
+ mLanguage = (String) getConfigParameterValue(PARAM_LANGUAGE);
mCurrentIndex = 0;
- //if input directory does not exist or is not a directory, throw exception
- if (!directory.exists() || !directory.isDirectory())
- {
- throw new ResourceInitializationException(
- ResourceConfigurationException.DIRECTORY_NOT_FOUND,
- new Object[]{PARAM_INPUTDIR, this.getMetaData().getName(), directory.getPath()});
- }
-
- //get list of files (not subdirectories) in the specified directory
+ // if input directory does not exist or is not a directory, throw exception
+ if (!directory.exists() || !directory.isDirectory()) {
+ throw new ResourceInitializationException(ResourceConfigurationException.DIRECTORY_NOT_FOUND,
+ new Object[] { PARAM_INPUTDIR, this.getMetaData().getName(),
+ directory.getPath() });
+ }
+
+ // get list of files (not subdirectories) in the specified directory
mFiles = new ArrayList();
File[] files = directory.listFiles();
- for (int i = 0; i < files.length; i++)
- {
- if (!files[i].isDirectory())
- {
- mFiles.add(files[i]);
+ for (int i = 0; i < files.length; i++) {
+ if (!files[i].isDirectory()) {
+ mFiles.add(files[i]);
}
}
}
@@ -108,104 +103,84 @@
/**
* @see org.apache.uima.collection.CollectionReader#hasNext()
*/
- public boolean hasNext()
- {
+ public boolean hasNext() {
return mCurrentIndex < mFiles.size();
}
/**
* @see org.apache.uima.collection.CollectionReader#getNext(org.apache.uima.cas.CAS)
*/
- public void getNext(CAS aCAS) throws IOException, CollectionException
- {
- JCas jcas;
- try
- {
+ public void getNext(CAS aCAS) throws IOException, CollectionException {
+ JCas jcas;
+ try {
jcas = aCAS.getJCas();
- }
- catch (CASException e)
- {
+ } catch (CASException e) {
throw new CollectionException(e);
}
-
- //open input stream to file
- File file = (File)mFiles.get(mCurrentIndex++);
- FileInputStream fis = new FileInputStream(file);
- try
- {
- //if there's a CAS Initializer, call it
- if (getCasInitializer() != null)
- {
- getCasInitializer().initializeCas(fis, aCAS);
- }
- else //No CAS Initiliazer, so read file and set document text ourselves
- {
- byte[] contents = new byte[(int)file.length() ];
- fis.read( contents );
- String text;
- if (mEncoding != null)
- {
- text = new String(contents, mEncoding);
- }
- else
- {
- text = new String(contents);
- }
- //put document in CAS
- jcas.setDocumentText(text);
- }
- }
- finally
- {
- if (fis != null)
- fis.close();
- }
-
- //set language if it was explicitly specified as a configuration parameter
- if (mLanguage != null)
- {
- ((DocumentAnnotation)jcas.getDocumentAnnotationFs()).setLanguage(mLanguage);
+
+ // open input stream to file
+ File file = (File) mFiles.get(mCurrentIndex++);
+ FileInputStream fis = new FileInputStream(file);
+ try {
+ // if there's a CAS Initializer, call it
+ if (getCasInitializer() != null) {
+ getCasInitializer().initializeCas(fis, aCAS);
+ } else // No CAS Initiliazer, so read file and set document text ourselves
+ {
+ byte[] contents = new byte[(int) file.length()];
+ fis.read(contents);
+ String text;
+ if (mEncoding != null) {
+ text = new String(contents, mEncoding);
+ } else {
+ text = new String(contents);
+ }
+ // put document in CAS
+ jcas.setDocumentText(text);
+ }
+ } finally {
+ if (fis != null)
+ fis.close();
}
-
- //Also store location of source document in CAS. This information is critical
- //if CAS Consumers will need to know where the original document contents are located.
- //For example, the Semantic Search CAS Indexer writes this information into the
- //search index that it creates, which allows applications that use the search index to
- //locate the documents that satisfy their semantic queries.
+
+ // set language if it was explicitly specified as a configuration parameter
+ if (mLanguage != null) {
+ ((DocumentAnnotation) jcas.getDocumentAnnotationFs()).setLanguage(mLanguage);
+ }
+
+ // Also store location of source document in CAS. This information is critical
+ // if CAS Consumers will need to know where the original document contents are located.
+ // For example, the Semantic Search CAS Indexer writes this information into the
+ // search index that it creates, which allows applications that use the search index to
+ // locate the documents that satisfy their semantic queries.
SourceDocumentInformation srcDocInfo = new SourceDocumentInformation(jcas);
srcDocInfo.setUri(file.getAbsoluteFile().toURL().toString());
srcDocInfo.setOffsetInSource(0);
- srcDocInfo.setDocumentSize((int)file.length());
+ srcDocInfo.setDocumentSize((int) file.length());
srcDocInfo.addToIndexes();
}
/**
* @see org.apache.uima.collection.base_cpm.BaseCollectionReader#close()
*/
- public void close() throws IOException
- {
+ public void close() throws IOException {
}
/**
* @see org.apache.uima.collection.base_cpm.BaseCollectionReader#getProgress()
*/
- public Progress[] getProgress()
- {
- return new Progress[]{
- new ProgressImpl(mCurrentIndex,mFiles.size(),Progress.ENTITIES)};
+ public Progress[] getProgress() {
+ return new Progress[] { new ProgressImpl(mCurrentIndex, mFiles.size(), Progress.ENTITIES) };
}
/**
- * Gets the total number of documents that will be returned by this
- * collection reader. This is not part of the general collection reader
- * interface.
+ * Gets the total number of documents that will be returned by this collection reader. This is not
+ * part of the general collection reader interface.
*
* @return the number of documents in the collection
*/
- public int getNumberOfDocuments()
- {
+ public int getNumberOfDocuments() {
return mFiles.size();
}
-
}
Modified: incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/InlineXmlCasConsumer.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/InlineXmlCasConsumer.java?view=diff&rev=477832&r1=477831&r2=477832
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/InlineXmlCasConsumer.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-examples/src/main/java/org/apache/uima/examples/cpe/InlineXmlCasConsumer.java Tue Nov 21 11:19:03 2006
@@ -36,101 +36,85 @@
import org.apache.uima.util.CasToInlineXml;
/**
- * A simple CAS consumer that generates inline XML and writes it to a file.
- * UTF-8 encoding is used.
- * <p>
+ * A simple CAS consumer that generates inline XML and writes it to a file. UTF-8 encoding is used.
+ * <p>
* This CAS Consumer takes one parameter:
* <ul>
- * <li><code>OutputDirectory</code> - path to directory into which output
- * files will be written</li>
- * </ul>
+ * <li><code>OutputDirectory</code> - path to directory into which output files will be written</li>
+ * </ul>
*
*
*/
-public class InlineXmlCasConsumer extends CasConsumer_ImplBase
-{
+public class InlineXmlCasConsumer extends CasConsumer_ImplBase {
/**
- * Name of configuration parameter that must be set to the path of
- * a directory into which the output files will be written.
+ * Name of configuration parameter that must be set to the path of a directory into which the
+ * output files will be written.
*/
public static final String PARAM_OUTPUTDIR = "OutputDirectory";
-
- private File mOutputDir;
- private CasToInlineXml cas2xml;
+ private File mOutputDir;
+
+ private CasToInlineXml cas2xml;
+
private int mDocNum;
-
-
- public void initialize()
- throws ResourceInitializationException
- {
- mDocNum = 0;
- mOutputDir = new File(((String)getConfigParameterValue(PARAM_OUTPUTDIR)).trim());
- if (!mOutputDir.exists())
- {
- mOutputDir.mkdirs();
- }
- cas2xml = new CasToInlineXml();
- }
-
- /** Processes the CasContainer which was populated by the TextAnalysisEngines.
- * <br> In this case, the CAS is converted to XML and written into the
- * output file .
- *
- * @param aCAS CasContainer which has been populated by the TAEs
- *
- * @throws ResourceProcessException if there is an error in processing the
- * Resource
- *
- * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(org.apache.uima.cas.CAS)
- */
- public void processCas(CAS aCAS) throws ResourceProcessException
- {
- JCas jcas;
- try
- {
+
+ public void initialize() throws ResourceInitializationException {
+ mDocNum = 0;
+ mOutputDir = new File(((String) getConfigParameterValue(PARAM_OUTPUTDIR)).trim());
+ if (!mOutputDir.exists()) {
+ mOutputDir.mkdirs();
+ }
+ cas2xml = new CasToInlineXml();
+ }
+
+ /**
+ * Processes the CasContainer which was populated by the TextAnalysisEngines. <br>
+ * In this case, the CAS is converted to XML and written into the output file .
+ *
+ * @param aCAS
+ * CasContainer which has been populated by the TAEs
+ *
+ * @throws ResourceProcessException
+ * if there is an error in processing the Resource
+ *
+ * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(org.apache.uima.cas.CAS)
+ */
+ public void processCas(CAS aCAS) throws ResourceProcessException {
+ JCas jcas;
+ try {
jcas = aCAS.getJCas();
- }
- catch (CASException e)
- {
- throw new ResourceProcessException(e);
- }
-
- // retreive the filename of the input file from the CAS
- FSIterator it = jcas.getJFSIndexRepository().getAnnotationIndex(SourceDocumentInformation.type).iterator();
- File outFile = null;
- if (it.hasNext())
- {
- SourceDocumentInformation fileLoc = (SourceDocumentInformation)it.next();
- File inFile;
- try
- {
- inFile = new File(new URL(fileLoc.getUri()).getPath());
- outFile = new File(mOutputDir, inFile.getName());
- }
- catch (MalformedURLException e1)
- {
- //invalid URL, use default processing below
- }
- }
- if (outFile == null)
- {
- outFile = new File(mOutputDir, "doc"+ mDocNum++);
- }
- // convert CAS to xml format and write to output file in UTF-8
- try
- {
+ } catch (CASException e) {
+ throw new ResourceProcessException(e);
+ }
+
+ // retreive the filename of the input file from the CAS
+ FSIterator it = jcas.getJFSIndexRepository().getAnnotationIndex(SourceDocumentInformation.type)
+ .iterator();
+ File outFile = null;
+ if (it.hasNext()) {
+ SourceDocumentInformation fileLoc = (SourceDocumentInformation) it.next();
+ File inFile;
+ try {
+ inFile = new File(new URL(fileLoc.getUri()).getPath());
+ outFile = new File(mOutputDir, inFile.getName());
+ } catch (MalformedURLException e1) {
+ // invalid URL, use default processing below
+ }
+ }
+ if (outFile == null) {
+ outFile = new File(mOutputDir, "doc" + mDocNum++);
+ }
+ // convert CAS to xml format and write to output file in UTF-8
+ try {
String xmlAnnotations = cas2xml.generateXML(aCAS);
- FileOutputStream outStream = new FileOutputStream(outFile);
- outStream.write( xmlAnnotations.getBytes("UTF-8") );
+ FileOutputStream outStream = new FileOutputStream(outFile);
+ outStream.write(xmlAnnotations.getBytes("UTF-8"));
outStream.close();
- }
- catch (CASException e) {
- throw new ResourceProcessException(e);
- }
- catch (IOException e) {
- throw new ResourceProcessException(e);
- }
- }
+ } catch (CASException e) {
+ throw new ResourceProcessException(e);
+ } catch (IOException e) {
+ throw new ResourceProcessException(e);
+ }
+ }
}