use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.
the class ExtractFeaturesConnectorTest method extractFeaturesConnectorSingleLabelTest.
@Test
public void extractFeaturesConnectorSingleLabelTest() throws Exception {
File outputPath = folder.newFolder();
// we do not need parameters here, but in case we do :)
Object[] parameters = new Object[] { NoopFeatureExtractor.PARAM_UNIQUE_EXTRACTOR_NAME, "123" };
ExternalResourceDescription featureExtractor = ExternalResourceFactory.createExternalResourceDescription(NoopFeatureExtractor.class, parameters);
List<ExternalResourceDescription> fes = new ArrayList<>();
fes.add(featureExtractor);
CollectionReaderDescription reader = CollectionReaderFactory.createReaderDescription(TestReaderSingleLabel.class, TestReaderSingleLabel.PARAM_SOURCE_LOCATION, "src/test/resources/data/*.txt");
AnalysisEngineDescription segmenter = AnalysisEngineFactory.createEngineDescription(BreakIteratorSegmenter.class);
AnalysisEngineDescription doc = AnalysisEngineFactory.createEngineDescription(DocumentModeAnnotator.class, DocumentModeAnnotator.PARAM_FEATURE_MODE, Constants.FM_DOCUMENT);
AnalysisEngineDescription featExtractorConnector = TaskUtils.getFeatureExtractorConnector(outputPath.getAbsolutePath(), JsonDataWriter.class.getName(), Constants.LM_REGRESSION, Constants.FM_DOCUMENT, false, false, false, false, Collections.emptyList(), fes, new String[] {});
SimplePipeline.runPipeline(reader, segmenter, doc, featExtractorConnector);
Gson gson = new Gson();
System.out.println(FileUtils.readFileToString(new File(outputPath, JsonDataWriter.JSON_FILE_NAME), "utf-8"));
List<String> lines = FileUtils.readLines(new File(outputPath, JsonDataWriter.JSON_FILE_NAME), "utf-8");
List<Instance> instances = new ArrayList<>();
for (String l : lines) {
instances.add(gson.fromJson(l, Instance.class));
}
assertEquals(2, instances.size());
assertEquals(1, getUniqueOutcomes(instances));
}
use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.
the class ExtractFeaturesConnectorTest method extractFeaturesConnectorMultiLabelTest.
@Test
public void extractFeaturesConnectorMultiLabelTest() throws Exception {
File outputPath = folder.newFolder();
// we do not need parameters here, but in case we do :)
Object[] parameters = new Object[] { NoopFeatureExtractor.PARAM_UNIQUE_EXTRACTOR_NAME, "123" };
ExternalResourceDescription featureExtractor = ExternalResourceFactory.createExternalResourceDescription(NoopFeatureExtractor.class, parameters);
List<ExternalResourceDescription> fes = new ArrayList<>();
fes.add(featureExtractor);
CollectionReaderDescription reader = CollectionReaderFactory.createReaderDescription(TestReaderMultiLabel.class, TestReaderMultiLabel.PARAM_SOURCE_LOCATION, "src/test/resources/data/*.txt");
AnalysisEngineDescription segmenter = AnalysisEngineFactory.createEngineDescription(BreakIteratorSegmenter.class);
AnalysisEngineDescription doc = AnalysisEngineFactory.createEngineDescription(DocumentModeAnnotator.class, DocumentModeAnnotator.PARAM_FEATURE_MODE, Constants.FM_DOCUMENT);
AnalysisEngineDescription featExtractorConnector = TaskUtils.getFeatureExtractorConnector(outputPath.getAbsolutePath(), JsonDataWriter.class.getName(), Constants.LM_REGRESSION, Constants.FM_DOCUMENT, false, false, false, false, Collections.emptyList(), fes, new String[] {});
SimplePipeline.runPipeline(reader, segmenter, doc, featExtractorConnector);
Gson gson = new Gson();
List<String> lines = FileUtils.readLines(new File(outputPath, JsonDataWriter.JSON_FILE_NAME), "utf-8");
List<Instance> instances = new ArrayList<>();
for (String l : lines) {
instances.add(gson.fromJson(l, Instance.class));
}
assertEquals(2, instances.size());
assertEquals(3, getUniqueOutcomes(instances));
}
use of org.apache.uima.resource.ExternalResourceDescription in project dkpro-tc by dkpro.
the class ModelSerializationTask method copyParameters.
private StringBuilder copyParameters(TcFeature f, StringBuilder sb, File aOutputFolder) throws IOException {
sb.append(f.getFeatureName() + "\t");
ExternalResourceDescription feDesc = f.getActualValue();
Map<String, Object> parameterSettings = ConfigurationParameterFactory.getParameterSettings(feDesc.getResourceSpecifier());
List<String> keySet = new ArrayList<>(parameterSettings.keySet());
for (int i = 0; i < keySet.size(); i++) {
String key = keySet.get(i);
String value = parameterSettings.get(key).toString();
if (valueExistAsFileOrFolderInTheFileSystem(value)) {
String name = new File(value).getName();
String destination = aOutputFolder + "/" + name;
copyToTargetLocation(new File(value), new File(destination));
sb = record(i, keySet, name, sb);
continue;
}
sb = record(i, keySet, parameterSettings, sb);
}
sb.append("\n");
return sb;
}
use of org.apache.uima.resource.ExternalResourceDescription 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.apache.uima.resource.ExternalResourceDescription 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();
}
Aggregations