Search in sources :

Example 41 with AnalysisEngineDescription

use of org.apache.uima.analysis_engine.AnalysisEngineDescription in project dkpro-lab by dkpro.

the class UimaAsExecutionEngine method initializeService.

protected void initializeService() throws Exception {
    // Create Asynchronous Engine API
    uimaAsEngine = new BaseUIMAAsynchronousEngine_impl();
    // Save the AED to a file because UIMA-AS cannot have an AED direclty embedded in its
    // descriptor
    AnalysisEngineDescription topDescriptor = configuration.getAnalysisEngineDescription(ctx);
    ResourceMetaData topMetaData = topDescriptor.getMetaData();
    File topDescriptorFile = File.createTempFile(getClass().getSimpleName(), ".xml");
    topDescriptorFile.deleteOnExit();
    try (OutputStream os = new FileOutputStream(topDescriptorFile)) {
        topDescriptor.toXML(os);
    }
    // Create service descriptor
    ServiceContext context = new ServiceContextImpl(topMetaData.getName(), topMetaData.getDescription(), topDescriptorFile.getAbsolutePath(), endpoint, brokerUrl);
    UimaASPrimitiveDeploymentDescriptor dd = DeploymentDescriptorFactory.createPrimitiveDeploymentDescriptor(context);
    // Store service descriptor also to a temporary file
    File deploymentDescriptionFile = File.createTempFile(getClass().getSimpleName(), ".xml");
    deploymentDescriptionFile.deleteOnExit();
    dd.save(deploymentDescriptionFile);
    Map<String, Object> serviceCtx = new HashMap<String, Object>();
    serviceCtx.put(UimaAsynchronousEngine.DD2SpringXsltFilePath, getUrlAsFile(getClass().getResource("/uima-as/dd2spring.xsl"), true).getAbsolutePath());
    serviceCtx.put(UimaAsynchronousEngine.SaxonClasspath, getClass().getResource("/uima-as/saxon8.jar").toString());
    serviceId = uimaAsEngine.deploy(deploymentDescriptionFile.getAbsolutePath(), serviceCtx);
    ctx.message("Deployed experiment as UIMA-AS service: [" + serviceId + "]");
}
Also used : HashMap(java.util.HashMap) ServiceContext(org.apache.uima.resourceSpecifier.factory.ServiceContext) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BaseUIMAAsynchronousEngine_impl(org.apache.uima.adapter.jms.client.BaseUIMAAsynchronousEngine_impl) FileOutputStream(java.io.FileOutputStream) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) UimaASPrimitiveDeploymentDescriptor(org.apache.uima.resourceSpecifier.factory.UimaASPrimitiveDeploymentDescriptor) Util.getUrlAsFile(org.dkpro.lab.Util.getUrlAsFile) File(java.io.File) ResourceMetaData(org.apache.uima.resource.metadata.ResourceMetaData) ServiceContextImpl(org.apache.uima.resourceSpecifier.factory.impl.ServiceContextImpl)

Example 42 with AnalysisEngineDescription

use of org.apache.uima.analysis_engine.AnalysisEngineDescription in project dkpro-tc by dkpro.

the class MetaInfoTask method getAnalysisEngineDescription.

@Override
public AnalysisEngineDescription getAnalysisEngineDescription(TaskContext aContext) throws ResourceInitializationException, IOException {
    featureExtractorNames = new HashSet<>();
    // check for error conditions
    if (featureExtractors == null) {
        throw new ResourceInitializationException(new TextClassificationException("No feature extractors have been added to the experiment."));
    }
    List<AnalysisEngineDescription> metaCollectors = new ArrayList<>();
    if (recordContext) {
        AnalysisEngineDescription aed = injectContextMetaCollector(aContext);
        if (aed == null) {
            throw new NullPointerException("Initializing a ContextMetaCollector returned an AnalysisEngineDescription which was [NULL]");
        }
        metaCollectors.add(aed);
    }
    try {
        // Configure the meta collectors for each feature extractor individually
        for (TcFeature feClosure : featureExtractors) {
            ExternalResourceDescription feDesc = feClosure.getActualValue();
            Class<?> feClass = getClass(feDesc);
            // Skip feature extractors that are not dependent on meta collectors
            if (!MetaDependent.class.isAssignableFrom(feClass)) {
                continue;
            }
            MetaDependent feInstance = (MetaDependent) feClass.newInstance();
            Map<String, Object> parameterSettings = ConfigurationParameterFactory.getParameterSettings(feDesc.getResourceSpecifier());
            validateUniqueFeatureExtractorNames(parameterSettings);
            // Tell the meta collectors where to store their data
            for (MetaCollectorConfiguration conf : feInstance.getMetaCollectorClasses(parameterSettings)) {
                configureStorageLocations(aContext, conf.descriptor, (String) feClosure.getId(), conf.collectorOverrides, AccessMode.READWRITE);
                metaCollectors.add(conf.descriptor);
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new ResourceInitializationException(e);
    }
    // make sure that the meta key import can be resolved (even when no meta features have been
    // extracted, as in the regression demo)
    aContext.getFolder(META_KEY, AccessMode.READONLY);
    AggregateBuilder builder = new AggregateBuilder();
    for (AnalysisEngineDescription metaCollector : metaCollectors) {
        if (operativeViews != null) {
            for (String viewName : operativeViews) {
                builder.add(metaCollector, CAS.NAME_DEFAULT_SOFA, viewName);
            }
        } else {
            builder.add(metaCollector);
        }
    }
    return builder.createAggregateDescription();
}
Also used : TcFeature(org.dkpro.tc.api.features.TcFeature) TextClassificationException(org.dkpro.tc.api.exception.TextClassificationException) ArrayList(java.util.ArrayList) MetaDependent(org.dkpro.tc.api.features.meta.MetaDependent) AggregateBuilder(org.apache.uima.fit.factory.AggregateBuilder) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) MetaCollectorConfiguration(org.dkpro.tc.api.features.meta.MetaCollectorConfiguration) ExternalResourceDescription(org.apache.uima.resource.ExternalResourceDescription)

Example 43 with AnalysisEngineDescription

use of org.apache.uima.analysis_engine.AnalysisEngineDescription in project dkpro-tc by dkpro.

the class InitTaskDeep method getAnalysisEngineDescription.

// what should actually be done in this task
@Override
public AnalysisEngineDescription getAnalysisEngineDescription(TaskContext aContext) throws ResourceInitializationException, IOException {
    String output = isTesting ? OUTPUT_KEY_TEST : OUTPUT_KEY_TRAIN;
    File folder = aContext.getFolder(output, AccessMode.READWRITE);
    AnalysisEngineDescription xmiWriter = createEngineDescription(BinaryCasWriter.class, BinaryCasWriter.PARAM_TARGET_LOCATION, folder.getPath(), BinaryCasWriter.PARAM_FORMAT, "6+");
    // special connector that just checks whether there are no instances and outputs a
    // meaningful error message then
    // should be added before preprocessing
    AnalysisEngineDescription emptyProblemChecker = createEngineDescription(PreprocessConnector.class);
    if (operativeViews != null) {
        AggregateBuilder builder = new AggregateBuilder();
        for (String viewName : operativeViews) {
            builder.add(createEngineDescription(preprocessing), CAS.NAME_DEFAULT_SOFA, viewName);
        }
        preprocessing = builder.createAggregateDescription();
    }
    AggregateBuilder builder = new AggregateBuilder();
    if (dropVocabWithoutEmbedding) {
        builder.add(createEngineDescription(FilterVocabularyByEmbeddingAnnotator.class, FilterVocabularyByEmbeddingAnnotator.PARAM_EMBEDDING, embedding));
    }
    builder.add(createEngineDescription(AssignIdConnector.class));
    builder.add(emptyProblemChecker);
    builder.add(preprocessing);
    builder.add(xmiWriter);
    return builder.createAggregateDescription();
}
Also used : AssignIdConnector(org.dkpro.tc.core.task.uima.AssignIdConnector) AggregateBuilder(org.apache.uima.fit.factory.AggregateBuilder) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) FilterVocabularyByEmbeddingAnnotator(org.dkpro.tc.core.task.deep.anno.FilterVocabularyByEmbeddingAnnotator) File(java.io.File)

Example 44 with AnalysisEngineDescription

use of org.apache.uima.analysis_engine.AnalysisEngineDescription in project dkpro-tc by dkpro.

the class ExtractFeaturesConnectorTest method extractFeaturesConnectorRegressionTest.

@Test
public void extractFeaturesConnectorRegressionTest() throws Exception {
    File outputPath = folder.newFolder();
    // we do not need parameters here, but in case we do :)
    Object[] parameters = new Object[] { NoopFeatureExtractor.PARAM_UNIQUE_EXTRACTOR_NAME, "123", UnitContextMetaCollector.PARAM_CONTEXT_FOLDER, Constants.ID_CONTEXT_KEY };
    ExternalResourceDescription featureExtractor = ExternalResourceFactory.createExternalResourceDescription(NoopFeatureExtractor.class, parameters);
    List<ExternalResourceDescription> fes = new ArrayList<>();
    fes.add(featureExtractor);
    CollectionReaderDescription reader = CollectionReaderFactory.createReaderDescription(TestReaderRegression.class, TestReaderRegression.PARAM_SOURCE_LOCATION, "src/test/resources/data/*.txt");
    AnalysisEngineDescription segmenter = AnalysisEngineFactory.createEngineDescription(BreakIteratorSegmenter.class);
    AnalysisEngineDescription doc = AnalysisEngineFactory.createEngineDescription(DocumentModeAnnotator.class, DocumentModeAnnotator.PARAM_FEATURE_MODE, Constants.FM_DOCUMENT);
    AnalysisEngineDescription featExtractorConnector = TaskUtils.getFeatureExtractorConnector(outputPath.getAbsolutePath(), JsonDataWriter.class.getName(), Constants.LM_REGRESSION, Constants.FM_DOCUMENT, false, false, false, false, Collections.emptyList(), fes, new String[] {});
    SimplePipeline.runPipeline(reader, segmenter, doc, featExtractorConnector);
    Gson gson = new Gson();
    List<String> lines = FileUtils.readLines(new File(outputPath, JsonDataWriter.JSON_FILE_NAME), "utf-8");
    List<Instance> instances = new ArrayList<>();
    for (String l : lines) {
        instances.add(gson.fromJson(l, Instance.class));
    }
    assertEquals(2, instances.size());
    assertEquals(1, getUniqueOutcomes(instances));
    assertEquals("0.45", instances.get(0).getOutcome());
    System.out.println(FileUtils.readFileToString(new File(outputPath, JsonDataWriter.JSON_FILE_NAME), "utf-8"));
}
Also used : JsonDataWriter(org.dkpro.tc.core.io.JsonDataWriter) Instance(org.dkpro.tc.api.features.Instance) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) CollectionReaderDescription(org.apache.uima.collection.CollectionReaderDescription) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) File(java.io.File) ExternalResourceDescription(org.apache.uima.resource.ExternalResourceDescription) Test(org.junit.Test)

Example 45 with AnalysisEngineDescription

use of org.apache.uima.analysis_engine.AnalysisEngineDescription in project dkpro-tc by dkpro.

the class LuceneMetaCollectionBasedFeatureTestBase method runFeatureExtractor.

protected void runFeatureExtractor(File luceneFolder, AnalysisEngineDescription featureExtractor) throws Exception {
    CollectionReaderDescription reader = getFeatureReader();
    AnalysisEngineDescription segmenter = AnalysisEngineFactory.createEngineDescription(BreakIteratorSegmenter.class);
    SimplePipeline.runPipeline(reader, segmenter, featureExtractor);
}
Also used : CollectionReaderDescription(org.apache.uima.collection.CollectionReaderDescription) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription)

Aggregations

AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)62 Test (org.junit.Test)32 File (java.io.File)27 CollectionReaderDescription (org.apache.uima.collection.CollectionReaderDescription)25 ArrayList (java.util.ArrayList)22 AnalysisEngine (org.apache.uima.analysis_engine.AnalysisEngine)18 JCas (org.apache.uima.jcas.JCas)16 Feature (org.dkpro.tc.api.features.Feature)13 FeatureTestUtil.assertFeature (org.dkpro.tc.testing.FeatureTestUtil.assertFeature)11 ExternalResourceDescription (org.apache.uima.resource.ExternalResourceDescription)10 AggregateBuilder (org.apache.uima.fit.factory.AggregateBuilder)8 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)8 JsonDataWriter (org.dkpro.tc.core.io.JsonDataWriter)8 TextClassificationTarget (org.dkpro.tc.api.type.TextClassificationTarget)7 Gson (com.google.gson.Gson)6 IOException (java.io.IOException)6 Instance (org.dkpro.tc.api.features.Instance)6 OpenNlpPosTagger (de.tudarmstadt.ukp.dkpro.core.opennlp.OpenNlpPosTagger)4 BreakIteratorSegmenter (de.tudarmstadt.ukp.dkpro.core.tokit.BreakIteratorSegmenter)4 CAS (org.apache.uima.cas.CAS)4