use of org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint in project stanbol by apache.
the class DisambiguatorEngine method query.
/*
* Is used to query the Dbpedia with a entity as main constraint and then add string of all other entities
* detected as similarity constraints
*/
protected QueryResultList<Entity> query(Site dbpediaSite, String savedEntityLabel, String language, String extractionContext) throws SiteException {
FieldQuery query = dbpediaSite.getQueryFactory().createFieldQuery();
if (savedEntityLabel != null && !savedEntityLabel.isEmpty()) {
Constraint labelConstraint;
if (language != null) {
labelConstraint = new TextConstraint(savedEntityLabel, false, language, null);
} else {
labelConstraint = new TextConstraint(savedEntityLabel, false);
}
// TODO: what happens if a recommendation was not based on rdfs:label?
query.setConstraint(RDFS_LABEL.getUnicodeString(), labelConstraint);
} else {
log.warn("parsed label {} was empty or NULL. Will use Similarity constraint only!", savedEntityLabel);
}
query.setConstraint(SpecialFieldEnum.fullText.getUri(), new SimilarityConstraint(extractionContext));
query.setLimit(25);
return dbpediaSite.findEntities(query);
}
use of org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint in project stanbol by apache.
the class FieldQueryToJsonUtils method convertConstraintToJSON.
/**
* Converts a {@link Constraint} to JSON
*
* @param constraint the {@link Constraint}
* @param nsPrefixService Optionally the service that is used to convert data type
* URIs to '{prefix}:{localname}'
* @return the JSON representation
* @throws JSONException
*/
private static JSONObject convertConstraintToJSON(Constraint constraint, NamespacePrefixService nsPrefixService) throws JSONException {
JSONObject jConstraint = new JSONObject();
jConstraint.put("type", constraint.getType().name());
switch(constraint.getType()) {
case //both ValueConstraint and ReferenceConstraint
value:
ValueConstraint valueConstraint = ((ValueConstraint) constraint);
if (valueConstraint.getValues() != null) {
if (valueConstraint.getValues().size() == 1) {
jConstraint.put("value", valueConstraint.getValues().iterator().next());
} else {
jConstraint.put("value", new JSONArray(valueConstraint.getValues()));
}
}
if (constraint instanceof ReferenceConstraint) {
//the type "reference" is not present in the ConstraintType
//enum, because internally ReferenceConstraints are just a
//ValueConstraint with a predefined data type, but "reference"
//is still a valid value of the type property in JSON
jConstraint.put("type", "reference");
} else {
// valueConstraint
jConstraint.put("type", constraint.getType().name());
//for valueConstraints we need to add also the dataType(s)
Collection<String> dataTypes = valueConstraint.getDataTypes();
if (dataTypes != null && !dataTypes.isEmpty()) {
if (dataTypes.size() == 1) {
String dataType = dataTypes.iterator().next();
jConstraint.put("datatype", nsPrefixService != null ? nsPrefixService.getShortName(dataType) : dataType);
} else {
ArrayList<String> dataTypeValues = new ArrayList<String>(dataTypes.size());
for (String dataType : dataTypes) {
dataTypeValues.add(nsPrefixService != null ? nsPrefixService.getShortName(dataType) : dataType);
}
jConstraint.put("datatype", dataTypeValues);
}
}
}
//finally write the MODE
if (valueConstraint.getMode() != null) {
jConstraint.put("mode", valueConstraint.getMode());
}
break;
case text:
TextConstraint textConstraint = (TextConstraint) constraint;
Collection<String> languages = textConstraint.getLanguages();
if (languages != null && !languages.isEmpty()) {
if (languages.size() == 1) {
jConstraint.put("language", languages.iterator().next());
} else {
jConstraint.put("language", new JSONArray(languages));
}
}
jConstraint.put("patternType", textConstraint.getPatternType().name());
if (textConstraint.getTexts() != null && !textConstraint.getTexts().isEmpty()) {
if (textConstraint.getTexts().size() == 1) {
//write a string
jConstraint.put("text", textConstraint.getTexts().get(0));
} else {
//write an array
jConstraint.put("text", textConstraint.getTexts());
}
}
if (textConstraint.isCaseSensitive()) {
jConstraint.put("caseSensitive", true);
}
//write the proximity ranking state (if defined)
if (textConstraint.isProximityRanking() != null) {
jConstraint.put("proximityRanking", textConstraint.isProximityRanking());
}
break;
case range:
RangeConstraint rangeConstraint = (RangeConstraint) constraint;
Set<DataTypeEnum> dataTypes = EnumSet.noneOf(DataTypeEnum.class);
if (rangeConstraint.getLowerBound() != null) {
jConstraint.put("lowerBound", rangeConstraint.getLowerBound());
dataTypes.addAll(DataTypeEnum.getPrimaryDataTypes(rangeConstraint.getLowerBound().getClass()));
}
if (rangeConstraint.getUpperBound() != null) {
jConstraint.put("upperBound", rangeConstraint.getUpperBound());
dataTypes.addAll(DataTypeEnum.getPrimaryDataTypes(rangeConstraint.getUpperBound().getClass()));
}
jConstraint.put("inclusive", rangeConstraint.isInclusive());
if (!dataTypes.isEmpty()) {
jConstraint.put("datatype", dataTypes.iterator().next().getShortName());
}
break;
case similarity:
SimilarityConstraint simConstraint = (SimilarityConstraint) constraint;
jConstraint.put("context", simConstraint.getContext());
if (!simConstraint.getAdditionalFields().isEmpty()) {
jConstraint.put("addFields", new JSONArray(simConstraint.getAdditionalFields()));
}
break;
default:
//unknown constraint type
log.warn("Unsupported Constriant Type " + constraint.getType() + " (implementing class=" + constraint.getClass() + "| toString=" + constraint + ") -> skiped");
break;
}
return jConstraint;
}
Aggregations