Search in sources :

Example 6 with SimilarityConstraint

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);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 7 with SimilarityConstraint

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;
}
Also used : SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) JSONObject(org.codehaus.jettison.json.JSONObject) DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Aggregations

SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)7 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)7 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)4 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)4 ArrayList (java.util.ArrayList)3 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)3 RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)3 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)2 JSONArray (org.codehaus.jettison.json.JSONArray)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 ReconcileProperty (org.apache.stanbol.entityhub.jersey.grefine.ReconcileProperty)1 ReconcileValue (org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue)1 DataTypeEnum (org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)1