aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/rewrite/rewriters/MisspellRewriter.java
blob: e16891e352a827207661dcdcf5e48cb30983e593 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.rewrite.rewriters;

import java.io.*;
import java.util.*;
import java.util.logging.Logger;

import com.yahoo.component.annotation.Inject;
import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Provides;
import com.yahoo.search.query.rewrite.*;
import com.yahoo.search.*;
import com.yahoo.component.ComponentId;
import com.yahoo.filedistribution.fileacquirer.FileAcquirer;
import com.yahoo.search.query.rewrite.RewritesConfig;

/**
 * This rewriter would retrieve spell corrected query from QLAS and
 * add it to the original query tree as equiv<br>
 * - Features:<br>
 *   RewritesAsEquiv flag: add rewrites to original query as equiv
 *
 * @author Karen Sze Wing Lee
 */
@After("QLAS")
@Provides("MisspellRewriter")
public class MisspellRewriter extends QueryRewriteSearcher {

    // Flag for skipping this rewriter if the query has been rewritten
    private final boolean SKIP_REWRITER_IF_REWRITTEN = false;

    // Name of the rewriter
    public static final String REWRITER_NAME = "MisspellRewriter";

    private Logger logger = Logger.getLogger(MisspellRewriter.class.getName());

    /**
     * Constructor for MisspellRewriter
     */
    @Inject
    public MisspellRewriter(ComponentId id) {
        super(id);
    }

    /**
     * Constructor for MisspellRewriter unit test
     */
    public MisspellRewriter() {
        super();
    }

    /**
     * Instance creation time config loading besides FSA.
     * Empty for this rewriter
     */
    public boolean configure(FileAcquirer fileAcquirer,
                             RewritesConfig config,
                             HashMap<String, File> fileList) {
        return true;
    }

    /**
     * Main logic of rewriter<br>
     * - Retrieve spell corrected query from QLAS<br>
     * - Add spell corrected query as equiv
     */
    public HashMap<String, Object> rewrite(Query query,
                                           String dictKey) throws RuntimeException {

        Boolean rewritten = false;

        HashMap<String, Object> result = new HashMap<>();
        result.put(RewriterConstants.REWRITTEN, rewritten);
        result.put(RewriterConstants.DICT_KEY, dictKey);

        RewriterUtils.log(logger, query,
                         "In MisspellRewriter");

        // Retrieve flags for enabling the features
        String qssRw = getQPConfig(query, RewriterConstants.QSS_RW);
        String qssSugg = getQPConfig(query, RewriterConstants.QSS_SUGG);

        boolean isQSSRw = false;
        boolean isQSSSugg = false;

        if(qssRw!=null) {
            isQSSRw = qssRw.equalsIgnoreCase("true");
        }
        if(qssSugg!=null) {
            isQSSSugg = qssSugg.equalsIgnoreCase("true");
        }

        // Rewrite is not enabled
        if(!isQSSRw && !isQSSSugg) {
            return result;
        }

        // Retrieve spell corrected query from QLAS
        String rewrites = RewriterUtils.getSpellCorrected(query, isQSSRw, isQSSSugg);

        // No rewrites
        if(rewrites==null) {
            RewriterUtils.log(logger, query, "No rewrite is retrieved");
            return result;
        } else {
            RewriterUtils.log(logger, query, "Retrieved spell corrected query: " +
                              rewrites);
        }

        // Adding rewrite to the query tree
        query = RewriterFeatures.addRewritesAsEquiv(query, dictKey, rewrites, false, 0);

        rewritten = true;
        RewriterUtils.log(logger, query, "MisspellRewriter final query: " +
                          query.toDetailString());

        result.put(RewriterConstants.REWRITTEN, rewritten);
        result.put(RewriterConstants.DICT_KEY, rewrites);

        return result;
    }

    /**
     * Get the flag which specifies whether this rewriter
     * should be skipped if the query has been rewritten
     *
     * @return true if rewriter should be skipped, false
     *         otherwise
     */
    public boolean getSkipRewriterIfRewritten() {
        return SKIP_REWRITER_IF_REWRITTEN;
    }

   /**
    * Get the name of the rewriter
    *
    * @return Name of the rewriter
    */
   public String getRewriterName() {
       return REWRITER_NAME;
   }

   /**
    * Get default FSA dictionary names
    *
    * @return Pair of FSA dictionary name and filename
    */
   public HashMap<String, String> getDefaultFSAs() {
       return null;
   }
}