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);
}
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);
}
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);
}
}
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;
}
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;
}
Aggregations