use of org.dkpro.tc.api.features.TcFeature in project dkpro-tc by dkpro.
the class FeatureResourceLoader method loadExternalResourceDescriptionOfFeatures.
public List<ExternalResourceDescription> loadExternalResourceDescriptionOfFeatures() throws Exception {
List<ExternalResourceDescription> erd = new ArrayList<>();
File file = new File(tcModelLocation, MODEL_FEATURE_EXTRACTOR_CONFIGURATION);
assertModelFolderExists(file);
for (String l : FileUtils.readLines(file, "utf-8")) {
String[] split = l.split("\t");
String name = split[0];
Object[] parameters = getParameters(split);
Class<? extends Resource> feClass = urlClassLoader.loadClass(name).asSubclass(Resource.class);
List<Object> idRemovedParameters = filterId(parameters);
String id = getId(parameters);
idRemovedParameters = addModelPathAsPrefixIfParameterIsExistingFile(idRemovedParameters, tcModelLocation.getAbsolutePath());
TcFeature feature = TcFeatureFactory.create(id, feClass, idRemovedParameters.toArray());
ExternalResourceDescription exRes = feature.getActualValue();
// Skip feature extractors that are not dependent on meta collectors
if (!MetaDependent.class.isAssignableFrom(feClass)) {
erd.add(exRes);
continue;
}
Map<String, String> overrides = loadOverrides(tcModelLocation, META_COLLECTOR_OVERRIDE);
configureOverrides(tcModelLocation, exRes, overrides);
overrides = loadOverrides(tcModelLocation, META_EXTRACTOR_OVERRIDE);
configureOverrides(tcModelLocation, exRes, overrides);
erd.add(exRes);
}
urlClassLoader.close();
return erd;
}
use of org.dkpro.tc.api.features.TcFeature 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.TcFeature in project dkpro-tc by dkpro.
the class ModelSerializationTask method writeModelParameters.
private void writeModelParameters(TaskContext aContext, File aOutputFolder, List<TcFeature> featureSet) throws Exception {
StringBuilder sb = new StringBuilder();
for (TcFeature f : featureSet) {
copyLuceneMetaResourcesAndGetOverrides(aContext, f, aOutputFolder);
persistsFeatureClassObject(f, aOutputFolder);
sb = copyParameters(f, sb, aOutputFolder);
}
writeFeatureParameters(sb, aOutputFolder);
}
use of org.dkpro.tc.api.features.TcFeature 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