Search in sources :

Example 6 with TrainingSetException

use of org.apache.stanbol.enhancer.topic.api.training.TrainingSetException 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

SolrServerException (org.apache.solr.client.solrj.SolrServerException)6 TrainingSetException (org.apache.stanbol.enhancer.topic.api.training.TrainingSetException)6 SolrServer (org.apache.solr.client.solrj.SolrServer)5 ManagedSolrServer (org.apache.stanbol.commons.solr.managed.ManagedSolrServer)5 ArrayList (java.util.ArrayList)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ConfigurationException (org.osgi.service.cm.ConfigurationException)4 IOException (java.io.IOException)3 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 SolrDocument (org.apache.solr.common.SolrDocument)3 ChainException (org.apache.stanbol.enhancer.servicesapi.ChainException)3 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)3 InvalidContentException (org.apache.stanbol.enhancer.servicesapi.InvalidContentException)3 ClassifierException (org.apache.stanbol.enhancer.topic.api.ClassifierException)3 Example (org.apache.stanbol.enhancer.topic.api.training.Example)3 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)3 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrInputDocument (org.apache.solr.common.SolrInputDocument)2 Batch (org.apache.stanbol.enhancer.topic.api.Batch)2