blob: d251047c9ce4667099526177bac073c34a6517fa [file] [log] [blame]
[email protected]acf9f272014-04-15 23:04:001// Copyright 2014 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]acf9f272014-04-15 23:04:005#ifndef COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_
6#define COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_
initial.commit09911bf2008-07-26 23:55:297
initial.commit09911bf2008-07-26 23:55:298#include <vector>
9
[email protected]13f698d2011-05-12 21:55:4510#include "base/basictypes.h"
[email protected]d8830562013-06-10 22:01:5411#include "base/strings/string16.h"
[email protected]acf9f272014-04-15 23:04:0012#include "components/query_parser/snippet.h"
13
14namespace query_parser {
[email protected]6956cd62008-08-29 19:48:5815
initial.commit09911bf2008-07-26 23:55:2916class QueryNodeList;
17
[email protected]6956cd62008-08-29 19:48:5818// Used by HasMatchIn.
19struct QueryWord {
20 // The work to match against.
[email protected]439f1e32013-12-09 20:09:0921 base::string16 word;
[email protected]6956cd62008-08-29 19:48:5822
23 // The starting position of the word in the original text.
[email protected]c29962f22008-12-03 00:47:5824 size_t position;
[email protected]6956cd62008-08-29 19:48:5825};
26
kkimlabsf1a7a3732014-11-04 10:30:4627enum class MatchingAlgorithm {
28 // Only words long enough are considered for prefix search. Shorter words are
29 // considered for exact matches.
30 DEFAULT,
31 // All words are considered for a prefix search.
32 ALWAYS_PREFIX_SEARCH,
33};
34
[email protected]b3a84892014-04-23 04:28:0735typedef std::vector<query_parser::QueryWord> QueryWordVector;
36
[email protected]13f698d2011-05-12 21:55:4537// QueryNode is used by QueryParser to represent the elements that constitute a
38// query. While QueryNode is exposed by way of ParseQuery, it really isn't meant
39// for external usage.
initial.commit09911bf2008-07-26 23:55:2940class QueryNode {
41 public:
42 virtual ~QueryNode() {}
43
44 // Serialize ourselves out to a string that can be passed to SQLite. Returns
45 // the number of words in this node.
[email protected]439f1e32013-12-09 20:09:0946 virtual int AppendToSQLiteQuery(base::string16* query) const = 0;
initial.commit09911bf2008-07-26 23:55:2947
[email protected]13f698d2011-05-12 21:55:4548 // Return true if this is a QueryNodeWord, false if it's a QueryNodeList.
initial.commit09911bf2008-07-26 23:55:2949 virtual bool IsWord() const = 0;
50
[email protected]13f698d2011-05-12 21:55:4551 // Returns true if this node matches |word|. If |exact| is true, the string
52 // must exactly match. Otherwise, this uses a starts with comparison.
[email protected]439f1e32013-12-09 20:09:0953 virtual bool Matches(const base::string16& word, bool exact) const = 0;
initial.commit09911bf2008-07-26 23:55:2954
[email protected]25320602012-10-18 22:05:5655 // Returns true if this node matches at least one of the words in |words|. An
56 // entry is added to |match_positions| for all matching words giving the
57 // matching regions.
[email protected]b3a84892014-04-23 04:28:0758 virtual bool HasMatchIn(const QueryWordVector& words,
[email protected]6956cd62008-08-29 19:48:5859 Snippet::MatchPositions* match_positions) const = 0;
[email protected]7de99592008-12-09 19:16:0260
[email protected]5d592f02013-06-22 16:59:2261 // Returns true if this node matches at least one of the words in |words|.
[email protected]b3a84892014-04-23 04:28:0762 virtual bool HasMatchIn(const QueryWordVector& words) const = 0;
[email protected]5d592f02013-06-22 16:59:2263
[email protected]7de99592008-12-09 19:16:0264 // Appends the words that make up this node in |words|.
[email protected]439f1e32013-12-09 20:09:0965 virtual void AppendWords(std::vector<base::string16>* words) const = 0;
initial.commit09911bf2008-07-26 23:55:2966};
67
[email protected]b3a84892014-04-23 04:28:0768typedef std::vector<query_parser::QueryNode*> QueryNodeStarVector;
69
[email protected]13f698d2011-05-12 21:55:4570// This class is used to parse queries entered into the history search into more
71// normalized queries that can be passed to the SQLite backend.
initial.commit09911bf2008-07-26 23:55:2972class QueryParser {
73 public:
74 QueryParser();
75
[email protected]85d911c2009-05-19 03:59:4276 // For CJK ideographs and Korean Hangul, even a single character
77 // can be useful in prefix matching, but that may give us too many
78 // false positives. Moreover, the current ICU word breaker gives us
79 // back every single Chinese character as a word so that there's no
80 // point doing anything for them and we only adjust the minimum length
81 // to 2 for Korean Hangul while using 3 for others. This is a temporary
82 // hack until we have a segmentation support.
kkimlabsf1a7a3732014-11-04 10:30:4683 static bool IsWordLongEnoughForPrefixSearch(
84 const base::string16& word,
85 MatchingAlgorithm matching_algorithm);
[email protected]85d911c2009-05-19 03:59:4286
initial.commit09911bf2008-07-26 23:55:2987 // Parse a query into a SQLite query. The resulting query is placed in
[email protected]13f698d2011-05-12 21:55:4588 // |sqlite_query| and the number of words is returned.
kkimlabsf1a7a3732014-11-04 10:30:4689 int ParseQuery(const base::string16& query,
90 MatchingAlgorithm matching_algorithm,
91 base::string16* sqlite_query);
initial.commit09911bf2008-07-26 23:55:2992
[email protected]8c793c82011-05-19 00:41:3393 // Parses |query|, returning the words that make up it. Any words in quotes
94 // are put in |words| without the quotes. For example, the query text
95 // "foo bar" results in two entries being added to words, one for foo and one
96 // for bar.
[email protected]439f1e32013-12-09 20:09:0997 void ParseQueryWords(const base::string16& query,
kkimlabsf1a7a3732014-11-04 10:30:4698 MatchingAlgorithm matching_algorithm,
[email protected]d2065e062013-12-12 23:49:5299 std::vector<base::string16>* words);
initial.commit09911bf2008-07-26 23:55:29100
[email protected]8c793c82011-05-19 00:41:33101 // Parses |query|, returning the nodes that constitute the valid words in the
102 // query. This is intended for later usage with DoesQueryMatch. Ownership of
103 // the nodes passes to the caller.
[email protected]439f1e32013-12-09 20:09:09104 void ParseQueryNodes(const base::string16& query,
kkimlabsf1a7a3732014-11-04 10:30:46105 MatchingAlgorithm matching_algorithm,
[email protected]b3a84892014-04-23 04:28:07106 QueryNodeStarVector* nodes);
[email protected]7de99592008-12-09 19:16:02107
initial.commit09911bf2008-07-26 23:55:29108 // Returns true if the string text matches the query nodes created by a call
[email protected]13f698d2011-05-12 21:55:45109 // to ParseQuery. If the query does match, each of the matching positions in
[email protected]6956cd62008-08-29 19:48:58110 // the text is added to |match_positions|.
[email protected]439f1e32013-12-09 20:09:09111 bool DoesQueryMatch(const base::string16& text,
[email protected]b3a84892014-04-23 04:28:07112 const QueryNodeStarVector& nodes,
[email protected]6956cd62008-08-29 19:48:58113 Snippet::MatchPositions* match_positions);
initial.commit09911bf2008-07-26 23:55:29114
[email protected]5d592f02013-06-22 16:59:22115 // Returns true if all of the |words| match the query |nodes| created by a
116 // call to ParseQuery.
[email protected]b3a84892014-04-23 04:28:07117 bool DoesQueryMatch(const QueryWordVector& words,
118 const QueryNodeStarVector& nodes);
[email protected]5d592f02013-06-22 16:59:22119
120 // Extracts the words from |text|, placing each word into |words|.
[email protected]439f1e32013-12-09 20:09:09121 void ExtractQueryWords(const base::string16& text,
[email protected]b3a84892014-04-23 04:28:07122 QueryWordVector* words);
123
124 // Sorts the match positions in |matches| by their first index, then
125 // coalesces any match positions that intersect each other.
126 static void SortAndCoalesceMatchPositions(Snippet::MatchPositions* matches);
[email protected]5d592f02013-06-22 16:59:22127
initial.commit09911bf2008-07-26 23:55:29128 private:
[email protected]13f698d2011-05-12 21:55:45129 // Does the work of parsing |query|; creates nodes in |root| as appropriate.
130 // This is invoked from both of the ParseQuery methods.
kkimlabsf1a7a3732014-11-04 10:30:46131 bool ParseQueryImpl(const base::string16& query,
132 MatchingAlgorithm matching_algorithm,
133 QueryNodeList* root);
initial.commit09911bf2008-07-26 23:55:29134
[email protected]13f698d2011-05-12 21:55:45135 DISALLOW_COPY_AND_ASSIGN(QueryParser);
initial.commit09911bf2008-07-26 23:55:29136};
137
[email protected]acf9f272014-04-15 23:04:00138} // namespace query_parser
139
140#endif // COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_