Search in sources :

Example 21 with TextConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint 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 22 with TextConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint 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)

Example 23 with TextConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.

the class FieldQueryReader method parseTextConstraint.

/**
     * @param jConstraint
     * @return
     * @throws JSONException
     */
private static Constraint parseTextConstraint(JSONObject jConstraint) throws JSONException {
    final TextConstraint constraint;
    boolean caseSensitive = jConstraint.optBoolean("caseSensitive", false);
    //parse patternType
    PatternType patternType;
    String jPatternType = jConstraint.optString("patternType", null);
    if (jPatternType == null) {
        patternType = PatternType.none;
    } else {
        try {
            patternType = PatternType.valueOf(jPatternType);
        } catch (IllegalArgumentException e) {
            log.warn("Encountered unknown patternType for TextConstraint!", e);
            patternType = PatternType.none;
            StringBuilder message = new StringBuilder();
            message.append("Illegal value for field 'patternType'.\n");
            message.append("Supported values are: ");
            message.append(Arrays.toString(PatternType.values()));
            message.append('\n');
            message.append("Parsed Constraint: \n");
            message.append(jConstraint.toString(4));
            throw new IllegalArgumentException(message.toString());
        }
    }
    //parse languages
    Collection<String> languages;
    //support both "languages" and "language"
    String languageKey = null;
    if (jConstraint.has("language")) {
        languageKey = "language";
    } else if (jConstraint.has("languages")) {
        log.warn("The key \"languages\" is deprecated. Use \"language\" instead.");
        languageKey = "languages";
    }
    if (languageKey != null) {
        JSONArray jLanguages = jConstraint.optJSONArray(languageKey);
        if (jLanguages != null && jLanguages.length() > 0) {
            languages = new ArrayList<String>(jLanguages.length());
            for (int i = 0; i < jLanguages.length(); i++) {
                String lang = jLanguages.getString(i);
                if (lang != null && !lang.isEmpty()) {
                    languages.add(lang);
                } else if (!languages.contains(null)) {
                    languages.add(null);
                }
            }
            if (languages.isEmpty()) {
                //if no one was successfully added set the list back to null
                languages = null;
            }
        } else {
            String language = jConstraint.getString(languageKey);
            if (language.isEmpty()) {
                languages = null;
            } else {
                //add the single language
                languages = Collections.singletonList(language);
            }
        }
    } else {
        languages = null;
    }
    //parse text and create constraint
    if (jConstraint.has("text") && !jConstraint.isNull("text")) {
        List<String> textConstraints;
        JSONArray jTextConstraints = jConstraint.optJSONArray("text");
        if (jTextConstraints != null) {
            textConstraints = new ArrayList<String>(jTextConstraints.length());
            for (int i = 0; i < jTextConstraints.length(); i++) {
                String text = jTextConstraints.getString(i);
                if (text != null && !text.isEmpty()) {
                    textConstraints.add(jTextConstraints.getString(i));
                }
            }
        } else {
            String text = jConstraint.getString("text");
            if (text == null || text.isEmpty()) {
                textConstraints = Collections.emptyList();
            } else {
                textConstraints = Collections.singletonList(text);
            }
        }
        if (textConstraints.isEmpty()) {
            StringBuilder message = new StringBuilder();
            message.append("Parsed TextConstraint doese not define a valid (none empty) value for the 'text' property !\n");
            message.append("Parsed Constraint: \n");
            message.append(jConstraint.toString(4));
            throw new IllegalArgumentException(message.toString());
        }
        constraint = new TextConstraint(textConstraints, patternType, caseSensitive, languages == null ? null : languages.toArray(new String[languages.size()]));
        //finally parse the optional termProximity
        if (jConstraint.has("proximityRanking")) {
            constraint.setProximityRanking(jConstraint.optBoolean("proximityRanking", false));
        }
    } else {
        StringBuilder message = new StringBuilder();
        message.append("Parsed TextConstraint doese not define the required field 'text'!\n");
        message.append("Parsed Constraint: \n");
        message.append(jConstraint.toString(4));
        throw new IllegalArgumentException(message.toString());
    }
    return constraint;
}
Also used : PatternType(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint.PatternType) JSONArray(org.codehaus.jettison.json.JSONArray) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)

Example 24 with TextConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.

the class SparqlQueryUtilsTest method generateSparqlQueryString.

/**
     * Utility function for generating SparqlQuery
     * @param textWithDoubleQuote
     * @param patternType
     */
private String generateSparqlQueryString(String textWithDoubleQuote, PatternType patternType) {
    int limit = 10;
    FieldQueryFactory qf = DefaultQueryFactory.getInstance();
    FieldQuery query = qf.createFieldQuery();
    String DEFAULT_AUTOCOMPLETE_SEARCH_FIELD = NamespaceEnum.rdfs + "label";
    Collection<String> selectedFields = new ArrayList<String>();
    selectedFields.add(DEFAULT_AUTOCOMPLETE_SEARCH_FIELD);
    query.addSelectedFields(selectedFields);
    query.setConstraint(DEFAULT_AUTOCOMPLETE_SEARCH_FIELD, new TextConstraint(textWithDoubleQuote, patternType, true, "en", null));
    query.setLimit(10);
    query.setOffset(0);
    final SparqlFieldQuery sparqlQuery = SparqlFieldQueryFactory.getSparqlFieldQuery(query);
    return SparqlQueryUtils.createSparqlSelectQuery(sparqlQuery, false, limit, SparqlEndpointTypeEnum.Standard);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) ArrayList(java.util.ArrayList) SparqlFieldQueryFactory(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQueryFactory) FieldQueryFactory(org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 25 with TextConstraint

use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.

the class YardTest method testFindRepresentationText.

/**
     * Same as {@link #testFindText()} but using 
     * {@link Yard#findRepresentation(FieldQuery)} to execute the queries
     */
@Test
public void testFindRepresentationText() {
    //init the test data
    FieldQueryTestData data = getFieldQueryTestData();
    //query for all languages and value1
    FieldQuery query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText()));
    validateQueryResults(query, getYard().findRepresentation(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId()), Arrays.asList(data.textField, data.refField, data.intField));
    //same for value2
    query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.textField, new TextConstraint(data.textValue2.getText()));
    validateQueryResults(query, getYard().findRepresentation(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.textField, data.refField, data.intField));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Aggregations

TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)31 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)17 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)8 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)7 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)7 RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)5 HashSet (java.util.HashSet)3 DataTypeEnum (org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum)3 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)3 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)3 FieldQueryFactory (org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory)3 ReconcileValue (org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue)2 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)2 FieldMapping (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)2 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)2 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)2