use of org.apache.stanbol.enhancer.topic.api.ClassifierException in project stanbol by apache.
the class TopicClassificationEngine method getBroaderConcepts.
@Override
public Set<String> getBroaderConcepts(String id) throws ClassifierException {
LinkedHashSet<String> broaderConcepts = new LinkedHashSet<String>();
if (broaderField == null) {
return broaderConcepts;
}
SolrServer solrServer = getActiveSolrServer();
SolrQuery query = new SolrQuery("*:*");
query.addFilterQuery(conceptUriField + ":" + ClientUtils.escapeQueryChars(id));
query.addField(broaderField);
try {
for (SolrDocument result : solrServer.query(query).getResults()) {
// there should be only one results
Collection<Object> broaderFieldValues = result.getFieldValues(broaderField);
if (broaderFieldValues == null) {
continue;
}
for (Object value : broaderFieldValues) {
broaderConcepts.add(value.toString());
}
}
} catch (SolrServerException e) {
String msg = String.format("Error while fetching broader topics of '%s' on Solr Core '%s'.", id, solrCoreId);
throw new ClassifierException(msg, e);
}
return broaderConcepts;
}
use of org.apache.stanbol.enhancer.topic.api.ClassifierException in project stanbol by apache.
the class TopicClassificationEngine method getPerformanceEstimates.
@Override
public ClassificationReport getPerformanceEstimates(String conceptId) throws ClassifierException {
SolrServer solrServer = getActiveSolrServer();
SolrQuery query = new SolrQuery("*:*");
query.addFilterQuery(entryTypeField + ":" + METADATA_ENTRY);
query.addFilterQuery(conceptUriField + ":" + ClientUtils.escapeQueryChars(conceptId));
try {
SolrDocumentList results = solrServer.query(query).getResults();
if (results.isEmpty()) {
throw new ClassifierException(String.format("'%s' is not a registered topic", conceptId));
}
SolrDocument metadata = results.get(0);
Float precision = computeMeanValue(metadata, precisionField);
Float recall = computeMeanValue(metadata, recallField);
int positiveSupport = computeSumValue(metadata, positiveSupportField);
int negativeSupport = computeSumValue(metadata, negativeSupportField);
Date evaluationDate = (Date) metadata.getFirstValue(modelEvaluationDateField);
boolean uptodate = evaluationDate != null;
ClassificationReport report = new ClassificationReport(precision, recall, positiveSupport, negativeSupport, uptodate, evaluationDate);
if (metadata.getFieldValues(falsePositivesField) == null) {
metadata.setField(falsePositivesField, new ArrayList<Object>());
}
for (Object falsePositiveId : metadata.getFieldValues(falsePositivesField)) {
report.falsePositiveExampleIds.add(falsePositiveId.toString());
}
if (metadata.getFieldValues(falseNegativesField) == null) {
metadata.setField(falseNegativesField, new ArrayList<Object>());
}
for (Object falseNegativeId : metadata.getFieldValues(falseNegativesField)) {
report.falseNegativeExampleIds.add(falseNegativeId.toString());
}
return report;
} catch (SolrServerException e) {
throw new ClassifierException(String.format("Error fetching the performance report for topic " + conceptId));
}
}
use of org.apache.stanbol.enhancer.topic.api.ClassifierException in project stanbol by apache.
the class TopicClassificationEngine method removeConcept.
@Override
public void removeConcept(String conceptId) throws ClassifierException {
if (conceptId == null || conceptId.isEmpty()) {
throw new ClassifierException("conceptId must not be null or empty");
}
SolrServer solrServer = getActiveSolrServer();
try {
solrServer.deleteByQuery(conceptUriField + ":" + ClientUtils.escapeQueryChars(conceptId));
solrServer.commit();
} catch (Exception e) {
String msg = String.format("Error removing concept '%s' on Solr Core '%s'", conceptId, solrCoreId);
throw new ClassifierException(msg, e);
}
}
use of org.apache.stanbol.enhancer.topic.api.ClassifierException in project stanbol by apache.
the class TopicClassificationEngine method getNarrowerConcepts.
@Override
public Set<String> getNarrowerConcepts(String broadTopicId) throws ClassifierException {
LinkedHashSet<String> narrowerConcepts = new LinkedHashSet<String>();
if (broaderField == null) {
return narrowerConcepts;
}
SolrServer solrServer = getActiveSolrServer();
SolrQuery query = new SolrQuery("*:*");
query.addFilterQuery(entryTypeField + ":" + METADATA_ENTRY);
query.addFilterQuery(broaderField + ":" + ClientUtils.escapeQueryChars(broadTopicId));
query.addField(conceptUriField);
query.addSortField(conceptUriField, SolrQuery.ORDER.asc);
try {
for (SolrDocument result : solrServer.query(query).getResults()) {
narrowerConcepts.add(result.getFirstValue(conceptUriField).toString());
}
} catch (SolrServerException e) {
String msg = String.format("Error while fetching narrower topics of '%s' on Solr Core '%s'.", broadTopicId, solrCoreId);
throw new ClassifierException(msg, e);
}
return narrowerConcepts;
}
use of org.apache.stanbol.enhancer.topic.api.ClassifierException in project stanbol by apache.
the class TopicClassificationEngine method getRootConcepts.
@Override
public Set<String> getRootConcepts() throws ClassifierException {
LinkedHashSet<String> rootConcepts = new LinkedHashSet<String>();
SolrServer solrServer = getActiveSolrServer();
SolrQuery query = new SolrQuery("*:*");
// TODO: this can be very big on flat thesauri: should we enable a paging API instead?
query.setRows(MAX_ROOTS);
query.setFields(conceptUriField);
query.setSortField(conceptUriField, SolrQuery.ORDER.asc);
query.addFilterQuery(entryTypeField + ":" + METADATA_ENTRY);
if (broaderField != null) {
// find any topic with an empty broaderField
query.addFilterQuery(" -" + broaderField + ":" + SOLR_NON_EMPTY_FIELD);
}
try {
QueryResponse response = solrServer.query(query);
if (response.getResults().size() >= MAX_ROOTS) {
log.warn(String.format("TopicClassifier '%s' has more than %d registered topic roots." + " Some roots might be ignored.", engineName, MAX_ROOTS));
}
for (SolrDocument result : response.getResults()) {
rootConcepts.add(result.getFirstValue(conceptUriField).toString());
}
} catch (SolrServerException e) {
String msg = String.format("Error while fetching root topics on Solr Core '%s'.", solrCoreId);
throw new ClassifierException(msg, e);
}
return rootConcepts;
}
Aggregations