use of org.apache.uima.resource.metadata.TypeSystemDescription in project dkpro-lab by dkpro.
the class SimpleExecutionEngineTest method testInit.
@Test
public void testInit() throws Exception {
File repo = new File("target/repository");
FileUtils.deleteDirectory(repo);
((FileSystemStorageService) storageService).setStorageRoot(repo);
assertNotNull(executionService);
assertNotNull(contextFactory);
TypeSystemDescription tsd = createTypeSystemDescription(new String[0]);
AnalysisEngineDescription desc = createEngineDescription(DummyAE.class, tsd);
DefaultUimaTask cfg = new DefaultUimaTask();
cfg.setReaderDescription(createReaderDescription(TestReader.class, tsd));
cfg.setAnalysisEngineDescription(desc);
TaskExecutionEngine runner = executionService.createEngine(cfg);
String uuid = runner.run(cfg);
System.out.println("=== Experiments in repository ===");
List<TaskContextMetadata> experiments = storageService.getContexts();
for (TaskContextMetadata e : experiments) {
System.out.println(e);
}
final StringBuilder sb = new StringBuilder();
storageService.retrieveBinary(uuid, "test", new StreamReader() {
@Override
public void read(InputStream aInputStream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Util.shoveAndClose(aInputStream, bos);
sb.append(new String(bos.toByteArray(), "UTF-8"));
}
});
assertEquals("works", sb.toString());
}
use of org.apache.uima.resource.metadata.TypeSystemDescription 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.resource.metadata.TypeSystemDescription in project webanno by webanno.
the class ConstraintsGeneratorTest method makeJCasOneSentence.
private JCas makeJCasOneSentence() throws UIMAException {
TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
TypeSystemDescription local = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath("src/test/resources/desc/types/webannoTestTypes.xml");
TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));
JCas jcas = JCasFactory.createJCas(merged);
DocumentMetaData.create(jcas).setDocumentId("doc");
TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class);
tb.buildTokens(jcas, "This is a test .");
return jcas;
}
use of org.apache.uima.resource.metadata.TypeSystemDescription 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;
}
use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.
the class ComplexTypeTest method testCountryType.
@Test
public void testCountryType() throws Exception {
TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");
CAS cas = CasCreationUtils.createCas(tsd, null, null);
cas.setDocumentText("Asia is the largest continent on Earth. Asia is subdivided into 48 countries, two of them (Russia and Turkey) having part of their land in Europe. The most active place on Earth for tropical cyclone activity lies northeast of the Philippines and south of Japan. The Gobi Desert is in Mongolia and the Arabian Desert stretches across much of the Middle East. The Yangtze River in China is the longest river in the continent. The Himalayas between Nepal and China is the tallest mountain range in the world. Tropical rainforests stretch across much of southern Asia and coniferous and deciduous forests lie farther north.");
TypeSystem ts = cas.getTypeSystem();
Type continentType = ts.getType("de.Continent");
Feature continentName = continentType.getFeatureByBaseName("name");
AnnotationFS asiaContinent = cas.createAnnotation(continentType, 0, 4);
asiaContinent.setStringValue(continentName, "Asia");
cas.addFsToIndexes(asiaContinent);
Type countryType = ts.getType("de.Country");
Feature countryName = countryType.getFeatureByBaseName("name");
AnnotationFS russia = cas.createAnnotation(countryType, 56, 62);
russia.setStringValue(countryName, "Russian Federation");
Feature continentFeature = countryType.getFeatureByBaseName("continent");
russia.setFeatureValue(continentFeature, asiaContinent);
cas.addFsToIndexes(russia);
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/region.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
Evaluator constraintsEvaluator = new ValuesGenerator();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(russia, "regionType", constraints);
List<PossibleValue> exValues = new LinkedList<>();
exValues.add(new PossibleValue("cold", true));
assertEquals(possibleValues, exValues);
}
Aggregations