Search in sources :

Example 1 with Annotations

use of com.yahoo.text.interpretation.Annotations in project vespa by vespa-engine.

the class RewriterUtils method getSpellCorrected.

/**
 * Retrieve spell corrected query with highest score from QLAS
 *
 * @param query Query object from the searcher
 * @param qss_rw Whether to consider qss_rw modification
 * @param qss_sugg Whether ot consider qss_sugg modification
 * @return Spell corrected query or null if not found
 */
public static String getSpellCorrected(Query query, boolean qss_rw, boolean qss_sugg) throws RuntimeException {
    log(utilsLogger, query, "Retrieving spell corrected query");
    // Retrieve Intent Model
    IntentModel intentModel = IntentModel.getFrom(query);
    if (intentModel == null) {
        error(utilsLogger, query, "Unable to retrieve intent model");
        throw new RuntimeException("Not able to retrieve intent model");
    }
    double max_score = 0;
    String spellCorrected = null;
    // query with highest score
    for (InterpretationNode interpretationNode : intentModel.children()) {
        Modification modification = interpretationNode.getInterpretation().getModification();
        Annotations annotations = modification.getAnnotation();
        Double score = annotations.getDouble("score");
        // Check if it's higher than the max score
        if (score != null && score > max_score) {
            Boolean isQSSRewrite = annotations.getBoolean("qss_rw");
            Boolean isQSSSuggest = annotations.getBoolean("qss_sugg");
            // Check if it's qss_rw or qss_sugg
            if ((qss_rw && isQSSRewrite != null && isQSSRewrite) || (qss_sugg && isQSSSuggest != null && isQSSSuggest)) {
                max_score = score;
                spellCorrected = modification.getText();
            }
        }
    }
    if (spellCorrected != null) {
        log(utilsLogger, query, "Successfully retrieved spell corrected query: " + spellCorrected);
    } else {
        log(utilsLogger, query, "No spell corrected query is retrieved");
    }
    return spellCorrected;
}
Also used : Modification(com.yahoo.text.interpretation.Modification) IntentModel(com.yahoo.search.intent.model.IntentModel) Annotations(com.yahoo.text.interpretation.Annotations) InterpretationNode(com.yahoo.search.intent.model.InterpretationNode)

Example 2 with Annotations

use of com.yahoo.text.interpretation.Annotations in project vespa by vespa-engine.

the class QueryRewriteSearcherTestUtils method createInterpretation.

/**
 * Create a new interpretation with modification that
 * contains the passed in query and score
 * @param spellRewrite query to be used as modification
 * @param score score to be used as modification score
 * @param isQSSRW whether the modification is qss_rw
 * @param isQSSSugg whether the modification is qss_sugg
 * @return newly created interpretation with modification
 */
public Interpretation createInterpretation(String spellRewrite, double score, boolean isQSSRW, boolean isQSSSugg) {
    Modification modification = new Modification(spellRewrite);
    Annotations annotation = modification.getAnnotation();
    annotation.put("score", score);
    if (isQSSRW)
        annotation.put("qss_rw", true);
    if (isQSSSugg)
        annotation.put("qss_sugg", true);
    Interpretation interpretation = new Interpretation(modification);
    return interpretation;
}
Also used : Modification(com.yahoo.text.interpretation.Modification) Annotations(com.yahoo.text.interpretation.Annotations) Interpretation(com.yahoo.text.interpretation.Interpretation)

Example 3 with Annotations

use of com.yahoo.text.interpretation.Annotations in project vespa by vespa-engine.

the class AnnotationTestCase method testUsability.

// The following testcase is a test with the api on a use_case, no cornercases here
public void testUsability() {
    Interpretation interpretation = new Interpretation("new york crab pizza");
    interpretation.annotate(0, 8, "place_name").put("^taxonomy:place_category", "city");
    interpretation.annotate(0, 8, "place_name").put("woe_id", 2459115);
    interpretation.annotate(9, 13, "food");
    interpretation.annotate(14, 19, "food");
    // Here we want to write code that finds out if the interpretation
    // matches pizza and toppings.
    List<Span> pizzaSpans = interpretation.getTermSpans("pizza");
    if (pizzaSpans.size() > 0) {
        // We know that we have pizza, now we want to get some topping
        // In a perfect world, pizza topping would have its own annotation class
        // but for now, we'll just accept terms that have been tokenized with food
        List<String> toppings = new ArrayList<>();
        for (Annotations annotations : interpretation.getAll("food")) {
            if (!annotations.getSubString().equalsIgnoreCase("pizza")) {
                toppings.add(annotations.getSubString());
            }
        }
        // We also want to find out where we should search for pizza places
        // Since we know that our interpreter engine is smart, we know
        // that all spans that has the annotation "place_name" has a "woe_id".
        int woe_id = 0;
        for (Annotations annotations : interpretation.getAll("place_name")) {
            // This will return either 0 or throw a bad exception
            // if a number is not found
            woe_id = annotations.getInteger("woe_id");
        }
        assertEquals(Arrays.asList("crab"), toppings);
        assertEquals(2459115, woe_id);
    }
}
Also used : Annotations(com.yahoo.text.interpretation.Annotations) ArrayList(java.util.ArrayList) Interpretation(com.yahoo.text.interpretation.Interpretation) Span(com.yahoo.text.interpretation.Span)

Aggregations

Annotations (com.yahoo.text.interpretation.Annotations)3 Interpretation (com.yahoo.text.interpretation.Interpretation)2 Modification (com.yahoo.text.interpretation.Modification)2 IntentModel (com.yahoo.search.intent.model.IntentModel)1 InterpretationNode (com.yahoo.search.intent.model.InterpretationNode)1 Span (com.yahoo.text.interpretation.Span)1 ArrayList (java.util.ArrayList)1