com.rosette.elasticsearch.EntitiesProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rosette-elasticsearch-plugin Show documentation
Show all versions of rosette-elasticsearch-plugin Show documentation
Elasticsearch analysis plugin powered by Rosette API
/*
* Copyright 2017 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rosette.elasticsearch;
import com.basistech.rosette.api.HttpRosetteAPIException;
import com.basistech.rosette.apimodel.DocumentRequest;
import com.basistech.rosette.apimodel.EntitiesOptions;
import com.basistech.rosette.apimodel.NameTranslationRequest;
import com.basistech.rosette.apimodel.NameTranslationResponse;
import com.basistech.rosette.apimodel.SentimentOptions;
import com.basistech.rosette.dm.AnnotatedText;
import com.basistech.rosette.dm.Entity;
import com.basistech.rosette.dm.Mention;
import com.basistech.util.LanguageCode;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.ingest.ConfigurationUtils;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.basistech.rosette.api.common.AbstractRosetteAPI.ENTITIES_SERVICE_PATH;
import static com.basistech.rosette.api.common.AbstractRosetteAPI.NAME_TRANSLATION_SERVICE_PATH;
import static com.basistech.rosette.api.common.AbstractRosetteAPI.SENTIMENT_SERVICE_PATH;
public class EntitiesProcessor extends RosetteAbstractProcessor {
public static final String TYPE = "ros_entities";
private static final Logger LOGGER = Loggers.getLogger(EntitiesProcessor.class, EntitiesProcessor.class.getName());
private boolean includeOffsets;
private boolean doTranslate;
private LanguageCode translateLanguage;
private boolean doSentiment;
EntitiesProcessor(RosetteApiWrapper rosAPI, String tag, String description, String inputField, String targetField,
boolean includeOffsets, boolean doTranslate, LanguageCode translateLanguage,
boolean doSentiment) {
super(rosAPI, tag, description, TYPE, inputField, targetField);
this.includeOffsets = includeOffsets;
this.doTranslate = doTranslate;
this.translateLanguage = translateLanguage;
this.doSentiment = doSentiment;
}
@Override
public void processDocument(String inputText, IngestDocument ingestDocument) throws Exception {
//Need to use the ADM for entities so we get offsets
AnnotatedText adm;
//If entity level sentiment is desired, use the entity information from the ASCENT call
try {
//SENTIMENT
if (doSentiment) {
DocumentRequest sentrequest = DocumentRequest.builder()
.content(inputText).build();
adm = AccessController.doPrivileged((PrivilegedAction) () ->
rosAPI.getHttpRosetteAPI().perform(SENTIMENT_SERVICE_PATH, sentrequest)
);
} else {
//REX
DocumentRequest entityrequest = DocumentRequest.builder()
.content(inputText).build();
adm = AccessController.doPrivileged((PrivilegedAction) () ->
rosAPI.getHttpRosetteAPI().perform(ENTITIES_SERVICE_PATH, entityrequest)
);
}
} catch (HttpRosetteAPIException ex) {
LOGGER.error(ex.getErrorResponse().getMessage());
throw new ElasticsearchException(ex.getErrorResponse().getMessage(), ex);
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy