use of org.dkpro.tc.api.features.meta.MetaDependent 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();
}
use of org.dkpro.tc.api.features.meta.MetaDependent 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);
}
use of org.dkpro.tc.api.features.meta.MetaDependent 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);
}
}
Aggregations