Search in sources :

Example 16 with ExternalResourceDescription

use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.

the class ModelSerializationTask method persistsFeatureClassObject.

private void persistsFeatureClassObject(TcFeature f, File aOutputFolder) throws Exception {
    ExternalResourceDescription feDesc = f.getActualValue();
    String implName;
    if (feDesc.getResourceSpecifier() instanceof CustomResourceSpecifier) {
        implName = ((CustomResourceSpecifier) feDesc.getResourceSpecifier()).getResourceClassName();
    } else {
        implName = feDesc.getImplementationName();
    }
    storeFeature(aOutputFolder, implName);
}
Also used : CustomResourceSpecifier(org.apache.uima.resource.CustomResourceSpecifier) ExternalResourceDescription(org.apache.uima.resource.ExternalResourceDescription)

Example 17 with ExternalResourceDescription

use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.

the class ModelSerializationTask method copyLuceneMetaResourcesAndGetOverrides.

private void copyLuceneMetaResourcesAndGetOverrides(TaskContext aContext, TcFeature f, File aOutputFolder) throws Exception {
    ExternalResourceDescription feDesc = f.getActualValue();
    Map<String, Object> parameterSettings = ConfigurationParameterFactory.getParameterSettings(feDesc.getResourceSpecifier());
    String implName;
    if (feDesc.getResourceSpecifier() instanceof CustomResourceSpecifier) {
        implName = ((CustomResourceSpecifier) feDesc.getResourceSpecifier()).getResourceClassName();
    } else {
        implName = feDesc.getImplementationName();
    }
    Class<?> feClass = Class.forName(implName);
    // Skip feature extractors that are not dependent on meta collectors
    if (!MetaDependent.class.isAssignableFrom(feClass)) {
        return;
    }
    MetaDependent feInstance = (MetaDependent) feClass.newInstance();
    Map<String, Object> metaOverrides = new HashMap<>();
    Map<String, Object> extractorOverrides = new HashMap<>();
    // Tell the meta collectors where to store their data
    for (MetaCollectorConfiguration conf : feInstance.getMetaCollectorClasses(parameterSettings)) {
        Map<String, String> collectorOverrides = conf.collectorOverrides;
        metaOverrides.putAll(collectorOverrides);
        extractorOverrides.putAll(conf.extractorOverrides);
        for (Entry<String, String> entry : collectorOverrides.entrySet()) {
            File file = new File(aContext.getFolder(META_KEY, AccessMode.READWRITE), entry.getValue().toString());
            String name = file.getName();
            String subFolder = aOutputFolder.getAbsoluteFile() + "/" + name;
            File targetFolder = new File(subFolder);
            copyToTargetLocation(file, targetFolder);
        }
    }
    writeOverrides(aOutputFolder, metaOverrides, META_COLLECTOR_OVERRIDE);
    writeOverrides(aOutputFolder, extractorOverrides, META_EXTRACTOR_OVERRIDE);
}
Also used : MetaDependent(org.dkpro.tc.api.features.meta.MetaDependent) CustomResourceSpecifier(org.apache.uima.resource.CustomResourceSpecifier) HashMap(java.util.HashMap) MetaCollectorConfiguration(org.dkpro.tc.api.features.meta.MetaCollectorConfiguration) File(java.io.File) ExternalResourceDescription(org.apache.uima.resource.ExternalResourceDescription)

Example 18 with ExternalResourceDescription

use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.

the class ExtractFeaturesTask method getAnalysisEngineDescription.

@Override
public AnalysisEngineDescription getAnalysisEngineDescription(TaskContext aContext) throws ResourceInitializationException {
    File outputDir = aContext.getFolder(OUTPUT_KEY, AccessMode.READWRITE);
    // Resolve the feature extractor closures to actual descritors
    List<ExternalResourceDescription> featureExtractorDescriptions = new ArrayList<>();
    // Configure the meta collectors for each feature extractor individually
    try {
        for (TcFeature feClosure : featureExtractors) {
            ExternalResourceDescription feDesc = feClosure.getActualValue();
            featureExtractorDescriptions.add(feDesc);
            Class<?> feClass = MetaInfoTask.getClass(feDesc);
            // collectors
            if (!MetaDependent.class.isAssignableFrom(feClass)) {
                continue;
            }
            MetaDependent feInstance = (MetaDependent) feClass.newInstance();
            Map<String, Object> parameterSettings = ConfigurationParameterFactory.getParameterSettings(feDesc.getResourceSpecifier());
            // Tell the meta collectors where to store their data
            for (MetaCollectorConfiguration conf : feInstance.getMetaCollectorClasses(parameterSettings)) {
                MetaInfoTask.configureStorageLocations(aContext, feDesc.getResourceSpecifier(), (String) feClosure.getId(), conf.extractorOverrides, AccessMode.READONLY);
            }
        }
        Set<String> requiredTypes = getRequiredTypesFromFeatureExtractors(featureExtractorDescriptions);
        // as feature filters are optional, check for null
        if (featureFilters == null) {
            featureFilters = Collections.<String>emptyList();
        }
        // ensure that outcomes file is copied into this folder
        File folder = aContext.getFolder(COLLECTION_INPUT_KEY, AccessMode.READONLY);
        File file = new File(folder, Constants.FILENAME_OUTCOMES);
        String[] outcomes = FileUtils.readLines(file, "utf-8").toArray(new String[0]);
        List<Object> parameters = new ArrayList<>();
        parameters.addAll(Arrays.asList(ExtractFeaturesConnector.PARAM_ADD_INSTANCE_ID, true, ExtractFeaturesConnector.PARAM_OUTPUT_DIRECTORY, outputDir, PARAM_APPLY_WEIGHTING, applyWeighting, PARAM_DATA_WRITER_CLASS, dataWriter, PARAM_FEATURE_FILTERS, featureFilters, PARAM_FEATURE_MODE, featureMode, PARAM_LEARNING_MODE, learningMode, PARAM_IS_TESTING, isTesting, PARAM_USE_SPARSE_FEATURES, useSparse, PARAM_OUTCOMES, outcomes, PARAM_FEATURE_EXTRACTORS, featureExtractorDescriptions, PARAM_REQUIRED_TYPES, requiredTypes));
        return AnalysisEngineFactory.createEngineDescription(ExtractFeaturesConnector.class, parameters.toArray());
    } catch (Exception e) {
        throw new ResourceInitializationException(e);
    }
}
Also used : TcFeature(org.dkpro.tc.api.features.TcFeature) ArrayList(java.util.ArrayList) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) IOException(java.io.IOException) MetaDependent(org.dkpro.tc.api.features.meta.MetaDependent) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) MetaCollectorConfiguration(org.dkpro.tc.api.features.meta.MetaCollectorConfiguration) File(java.io.File) ExternalResourceDescription(org.apache.uima.resource.ExternalResourceDescription)

Example 19 with ExternalResourceDescription

use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.

the class ExtractFeaturesTask method getRequiredTypesFromFeatureExtractors.

private Set<String> getRequiredTypesFromFeatureExtractors(List<ExternalResourceDescription> featureSet) throws Exception {
    Set<String> requirements = new HashSet<>();
    for (ExternalResourceDescription element : featureSet) {
        String implName;
        if (element.getResourceSpecifier() instanceof CustomResourceSpecifier) {
            implName = ((CustomResourceSpecifier) element.getResourceSpecifier()).getResourceClassName();
        } else {
            implName = element.getImplementationName();
        }
        TypeCapability annotation = ReflectionUtil.getAnnotation(Class.forName(implName), TypeCapability.class);
        if (annotation == null) {
            continue;
        }
        String[] inputs = annotation.inputs();
        StringBuilder sb = new StringBuilder();
        sb.append(implName + "|");
        for (int i = 0; i < inputs.length; i++) {
            sb.append(inputs[i]);
            if (i + 1 < inputs.length) {
                sb.append("|");
            }
        }
        requirements.add(sb.toString());
    }
    return requirements;
}
Also used : TypeCapability(org.apache.uima.fit.descriptor.TypeCapability) CustomResourceSpecifier(org.apache.uima.resource.CustomResourceSpecifier) HashSet(java.util.HashSet) ExternalResourceDescription(org.apache.uima.resource.ExternalResourceDescription)

Example 20 with ExternalResourceDescription

use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.

the class FeatureUtil method createResource.

@SuppressWarnings("unchecked")
public static <T extends Resource> T createResource(TcFeature feature) throws ResourceInitializationException, ResourceAccessException {
    // Configure external resource
    ExternalResourceDescription desc = feature.getActualValue();
    // Configure resource manager
    ResourceManagerConfiguration cfg = UIMAFramework.getResourceSpecifierFactory().createResourceManagerConfiguration();
    ExternalResourceFactory.bindExternalResource(cfg, "rootResource", desc);
    // Instantiate resource manager (internally instantiates resources)
    final ResourceManager manager = new ResourceManager_impl();
    manager.initializeExternalResources(cfg, "/", null);
    T res = (T) manager.getResource("/rootResource");
    // Initialize nested resources
    UimaContextAdmin ctx = new RootUimaContext_impl() {

        @Override
        public ResourceManager getResourceManager() {
            return manager;
        }
    };
    ExternalResourceInitializer.initialize(res, ctx);
    // Get resource instance
    return res;
}
Also used : RootUimaContext_impl(org.apache.uima.impl.RootUimaContext_impl) ResourceManagerConfiguration(org.apache.uima.resource.metadata.ResourceManagerConfiguration) UimaContextAdmin(org.apache.uima.UimaContextAdmin) ResourceManager(org.apache.uima.resource.ResourceManager) ResourceManager_impl(org.apache.uima.resource.impl.ResourceManager_impl) ExternalResourceDescription(org.apache.uima.resource.ExternalResourceDescription)

Aggregations

ExternalResourceDescription (org.apache.uima.resource.ExternalResourceDescription)27 ArrayList (java.util.ArrayList)17 File (java.io.File)10 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)10 JsonDataWriter (org.dkpro.tc.core.io.JsonDataWriter)8 CollectionReaderDescription (org.apache.uima.collection.CollectionReaderDescription)7 Instance (org.dkpro.tc.api.features.Instance)5 Test (org.junit.Test)5 Gson (com.google.gson.Gson)4 MetaDependent (org.dkpro.tc.api.features.meta.MetaDependent)4 CustomResourceSpecifier (org.apache.uima.resource.CustomResourceSpecifier)3 TcFeature (org.dkpro.tc.api.features.TcFeature)3 MetaCollectorConfiguration (org.dkpro.tc.api.features.meta.MetaCollectorConfiguration)3 HashMap (java.util.HashMap)2 UimaContextAdmin (org.apache.uima.UimaContextAdmin)2 RootUimaContext_impl (org.apache.uima.impl.RootUimaContext_impl)2 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)2 ResourceManager (org.apache.uima.resource.ResourceManager)2 ResourceManager_impl (org.apache.uima.resource.impl.ResourceManager_impl)2 ResourceManagerConfiguration (org.apache.uima.resource.metadata.ResourceManagerConfiguration)2