Search in sources :

Example 1 with MetaCollectorConfiguration

use of org.dkpro.tc.api.features.meta.MetaCollectorConfiguration in project dkpro-tc by dkpro.

the class MetaInfoTask method injectContextMetaCollector.

private AnalysisEngineDescription injectContextMetaCollector(TaskContext aContext) throws ResourceInitializationException {
    try {
        if (featureMode.equals(FM_UNIT) || featureMode.equals(FM_DOCUMENT)) {
            // add additional unit context meta collector that extracts the
            // context around text
            // classification units
            // mainly used for error analysis purposes
            Map<String, Object> empty = new HashMap<>();
            MetaCollectorConfiguration conf = new MetaCollectorConfiguration(UnitContextMetaCollector.class, empty);
            int rnd = r.nextInt();
            String val = "" + (rnd > 0 ? rnd : rnd * -1);
            conf.collectorOverrides.put(UnitContextMetaCollector.PARAM_UNIQUE_EXTRACTOR_NAME, val);
            conf.collectorOverrides.put(UnitContextMetaCollector.PARAM_CONTEXT_FOLDER, "");
            configureStorageLocations(aContext, conf.descriptor, null, conf.collectorOverrides, AccessMode.READWRITE);
            return conf.descriptor;
        }
        if (featureMode.equals(FM_SEQUENCE)) {
            Map<String, Object> empty = new HashMap<>();
            MetaCollectorConfiguration conf = new MetaCollectorConfiguration(SequenceContextMetaCollector.class, empty);
            int rnd = r.nextInt();
            String val = "" + (rnd > 0 ? rnd : rnd * -1);
            conf.collectorOverrides.put(UnitContextMetaCollector.PARAM_UNIQUE_EXTRACTOR_NAME, val);
            conf.collectorOverrides.put(UnitContextMetaCollector.PARAM_CONTEXT_FOLDER, "");
            configureStorageLocations(aContext, conf.descriptor, null, conf.collectorOverrides, AccessMode.READWRITE);
            return conf.descriptor;
        }
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new ResourceInitializationException(e);
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) MetaCollectorConfiguration(org.dkpro.tc.api.features.meta.MetaCollectorConfiguration)

Example 2 with MetaCollectorConfiguration

use of org.dkpro.tc.api.features.meta.MetaCollectorConfiguration 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 3 with MetaCollectorConfiguration

use of org.dkpro.tc.api.features.meta.MetaCollectorConfiguration 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 4 with MetaCollectorConfiguration

use of org.dkpro.tc.api.features.meta.MetaCollectorConfiguration 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)

Aggregations

MetaCollectorConfiguration (org.dkpro.tc.api.features.meta.MetaCollectorConfiguration)4 ExternalResourceDescription (org.apache.uima.resource.ExternalResourceDescription)3 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)3 MetaDependent (org.dkpro.tc.api.features.meta.MetaDependent)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TcFeature (org.dkpro.tc.api.features.TcFeature)2 IOException (java.io.IOException)1 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)1 AggregateBuilder (org.apache.uima.fit.factory.AggregateBuilder)1 CustomResourceSpecifier (org.apache.uima.resource.CustomResourceSpecifier)1 TextClassificationException (org.dkpro.tc.api.exception.TextClassificationException)1