use of org.apache.stanbol.enhancer.topic.api.ClassifierException in project stanbol by apache.
the class TopicClassificationEngine method updateTopic.
/**
* @param conceptUri
* the topic model to update
* @param metadataEntryId
* of the metadata entry id of the topic
* @param modelEntryId
* of the model entry id of the topic
* @param impactedTopics
* the list of impacted topics (e.g. the topic node and direct children)
* @param primaryTopicUri
* @param broaderConcepts
* the collection of broader to re-add in the broader field
*/
protected void updateTopic(String conceptUri, String metadataId, String modelId, List<String> impactedTopics, String primaryTopicUri, Collection<Object> broaderConcepts) throws TrainingSetException, ClassifierException {
long start = System.currentTimeMillis();
Batch<Example> examples = Batch.emtpyBatch(Example.class);
StringBuffer sb = new StringBuffer();
int offset = 0;
do {
examples = getTrainingSet().getPositiveExamples(impactedTopics, examples.nextOffset);
for (Example example : examples.items) {
if ((cvFoldCount != 0) && (offset % cvFoldCount == cvFoldIndex)) {
// we are performing a cross validation session and this example belong to the test
// fold hence should be skipped
offset++;
continue;
}
offset++;
sb.append(StringUtils.join(example.contents, "\n\n"));
sb.append("\n\n");
}
} while (sb.length() < MAX_CHARS_PER_TOPIC && examples.hasMore);
// reindex the topic with the new text data collected from the examples
SolrInputDocument modelEntry = new SolrInputDocument();
modelEntry.addField(entryIdField, modelId);
modelEntry.addField(conceptUriField, conceptUri);
modelEntry.addField(entryTypeField, MODEL_ENTRY);
if (sb.length() > 0) {
modelEntry.addField(similarityField, sb);
}
// update the metadata of the topic model
SolrInputDocument metadataEntry = new SolrInputDocument();
metadataEntry.addField(entryIdField, metadataId);
metadataEntry.addField(modelEntryIdField, modelId);
metadataEntry.addField(entryTypeField, METADATA_ENTRY);
metadataEntry.addField(conceptUriField, conceptUri);
if (primaryTopicUriField != null) {
metadataEntry.addField(primaryTopicUriField, primaryTopicUri);
}
if (broaderConcepts != null && broaderField != null) {
metadataEntry.addField(broaderField, broaderConcepts);
}
if (modelUpdateDateField != null) {
metadataEntry.addField(modelUpdateDateField, UTCTimeStamper.nowUtcDate());
}
SolrServer solrServer = getActiveSolrServer();
try {
UpdateRequest request = new UpdateRequest();
request.add(metadataEntry);
request.add(modelEntry);
solrServer.request(request);
// the commit is done by the caller in batch
} catch (Exception e) {
String msg = String.format("Error updating topic with id '%s' on Solr Core '%s'", conceptUri, solrCoreId);
throw new ClassifierException(msg, e);
}
long stop = System.currentTimeMillis();
log.debug("Sucessfully updated topic {} in {}s", conceptUri, (double) (stop - start) / 1000.);
}
Aggregations