Search in sources :

Example 16 with ClassifierException

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.);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) Example(org.apache.stanbol.enhancer.topic.api.training.Example) 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)

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