Search in sources :

Example 11 with ClassifierException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrDocument(org.apache.solr.common.SolrDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 12 with ClassifierException

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));
    }
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Date(java.util.Date) SolrDocument(org.apache.solr.common.SolrDocument) ClassificationReport(org.apache.stanbol.enhancer.topic.api.ClassificationReport) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 13 with ClassifierException

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);
    }
}
Also used : EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ConfigurationException(org.osgi.service.cm.ConfigurationException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrainingSetException(org.apache.stanbol.enhancer.topic.api.training.TrainingSetException) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException) InvalidContentException(org.apache.stanbol.enhancer.servicesapi.InvalidContentException) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) IOException(java.io.IOException) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 14 with ClassifierException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrDocument(org.apache.solr.common.SolrDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 15 with ClassifierException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Aggregations

ClassifierException (org.apache.stanbol.enhancer.topic.api.ClassifierException)16 SolrServerException (org.apache.solr.client.solrj.SolrServerException)13 SolrServer (org.apache.solr.client.solrj.SolrServer)12 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)12 ManagedSolrServer (org.apache.stanbol.commons.solr.managed.ManagedSolrServer)12 IOException (java.io.IOException)9 SolrDocument (org.apache.solr.common.SolrDocument)8 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)8 InvalidContentException (org.apache.stanbol.enhancer.servicesapi.InvalidContentException)8 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)8 ConfigurationException (org.osgi.service.cm.ConfigurationException)8 SolrQuery (org.apache.solr.client.solrj.SolrQuery)7 ChainException (org.apache.stanbol.enhancer.servicesapi.ChainException)7 TrainingSetException (org.apache.stanbol.enhancer.topic.api.training.TrainingSetException)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 SolrInputDocument (org.apache.solr.common.SolrInputDocument)4 ClassificationReport (org.apache.stanbol.enhancer.topic.api.ClassificationReport)4 LinkedHashSet (java.util.LinkedHashSet)3 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)3 SolrDocumentList (org.apache.solr.common.SolrDocumentList)3