use of org.apache.uima.collection.CollectionReader in project webanno by webanno.
the class ImportExportServiceImpl method importCasFromFile.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public JCas importCasFromFile(File aFile, Project aProject, String aFormat) throws UIMAException, IOException {
Class readerClass = getReadableFormats().get(aFormat);
if (readerClass == null) {
throw new IOException("No reader available for format [" + aFormat + "]");
}
// Prepare a CAS with the project type system
TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory.createTypeSystemDescription();
TypeSystemDescription projectTypes = annotationService.getProjectTypes(aProject);
TypeSystemDescription allTypes = CasCreationUtils.mergeTypeSystems(asList(projectTypes, builtInTypes));
CAS cas = JCasFactory.createJCas(allTypes).getCas();
// Convert the source document to CAS
CollectionReader reader = CollectionReaderFactory.createReader(readerClass, ResourceCollectionReaderBase.PARAM_SOURCE_LOCATION, aFile.getParentFile().getAbsolutePath(), ResourceCollectionReaderBase.PARAM_PATTERNS, new String[] { "[+]" + aFile.getName() });
if (!reader.hasNext()) {
throw new FileNotFoundException("Source file [" + aFile.getName() + "] not found in [" + aFile.getPath() + "]");
}
reader.getNext(cas);
JCas jCas = cas.getJCas();
// Create sentence / token annotations if they are missing
boolean hasTokens = JCasUtil.exists(jCas, Token.class);
boolean hasSentences = JCasUtil.exists(jCas, Sentence.class);
if (!hasSentences) {
splitSentences(jCas);
}
if (!hasTokens) {
tokenize(jCas);
}
if (!JCasUtil.exists(jCas, Token.class) || !JCasUtil.exists(jCas, Sentence.class)) {
throw new IOException("The document appears to be empty. Unable to detect any " + "tokens or sentences. Empty documents cannot be imported.");
}
return jCas;
}
use of org.apache.uima.collection.CollectionReader in project webanno by webanno.
the class SymbolicRulesTest method testSimpleSymbolicRules2.
@Test
public void testSimpleSymbolicRules2() throws Exception {
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/symbolic2.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
JCas jcas = JCasFactory.createJCas();
CollectionReader reader = createReader(Conll2006Reader.class, Conll2006Reader.PARAM_SOURCE_LOCATION, "src/test/resources/text/1.conll");
reader.getNext(jcas.getCas());
POS pos = new POS(jcas, 8, 9);
pos.setPosValue("pronoun");
pos.addToIndexes();
Evaluator constraintsEvaluator = new ValuesGenerator();
Lemma lemma = select(jcas, Lemma.class).iterator().next();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(lemma, "value", constraints);
List<PossibleValue> expectedOutput = new ArrayList<>();
expectedOutput.add(new PossibleValue("good", true));
assertEquals(expectedOutput, possibleValues);
}
use of org.apache.uima.collection.CollectionReader in project webanno by webanno.
the class SymbolicRulesTest method testSimpleSymbolicRules.
@Test
public void testSimpleSymbolicRules() throws Exception {
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/symbolic1.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
JCas jcas = JCasFactory.createJCas();
CollectionReader reader = createReader(Conll2006Reader.class, Conll2006Reader.PARAM_SOURCE_LOCATION, "src/test/resources/text/1.conll");
reader.getNext(jcas.getCas());
POS pos = new POS(jcas, 8, 9);
pos.setPosValue("pronoun");
pos.addToIndexes();
Evaluator constraintsEvaluator = new ValuesGenerator();
Lemma lemma = select(jcas, Lemma.class).iterator().next();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(lemma, "value", constraints);
List<PossibleValue> expectedOutput = new ArrayList<>();
expectedOutput.add(new PossibleValue("good", true));
assertEquals(expectedOutput, possibleValues);
}
use of org.apache.uima.collection.CollectionReader in project webanno by webanno.
the class DiffUtils method read.
public static JCas read(String aPath) throws UIMAException, IOException {
CollectionReader reader = createReader(Conll2006Reader.class, Conll2006Reader.PARAM_SOURCE_LOCATION, "src/test/resources/" + aPath);
JCas jcas = JCasFactory.createJCas();
reader.getNext(jcas.getCas());
return jcas;
}
use of org.apache.uima.collection.CollectionReader in project webanno by webanno.
the class DiffUtils method readWebAnnoTSV.
public static JCas readWebAnnoTSV(String aPath, TypeSystemDescription aType) throws UIMAException, IOException {
CollectionReader reader = createReader(WebannoTsv2Reader.class, WebannoTsv2Reader.PARAM_SOURCE_LOCATION, "src/test/resources/" + aPath);
JCas jcas;
if (aType != null) {
TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory.createTypeSystemDescription();
List<TypeSystemDescription> allTypes = new ArrayList<>();
allTypes.add(builtInTypes);
allTypes.add(aType);
jcas = JCasFactory.createJCas(CasCreationUtils.mergeTypeSystems(allTypes));
} else {
jcas = JCasFactory.createJCas();
}
reader.getNext(jcas.getCas());
return jcas;
}
Aggregations