[email protected] | acf9f27 | 2014-04-15 23:04:00 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | acf9f27 | 2014-04-15 23:04:00 | [diff] [blame] | 5 | #ifndef COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ |
| 6 | #define COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
avi | 8e000a7 | 2016-11-02 18:06:20 | [diff] [blame] | 10 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 14 | #include "base/strings/string16.h" |
[email protected] | acf9f27 | 2014-04-15 23:04:00 | [diff] [blame] | 15 | #include "components/query_parser/snippet.h" |
| 16 | |
| 17 | namespace query_parser { |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 18 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | class QueryNodeList; |
| 20 | |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 21 | // Used by HasMatchIn. |
| 22 | struct QueryWord { |
| 23 | // The work to match against. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 24 | base::string16 word; |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 25 | |
| 26 | // The starting position of the word in the original text. |
[email protected] | c29962f2 | 2008-12-03 00:47:58 | [diff] [blame] | 27 | size_t position; |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 28 | }; |
| 29 | |
kkimlabs | f1a7a373 | 2014-11-04 10:30:46 | [diff] [blame] | 30 | enum class MatchingAlgorithm { |
| 31 | // Only words long enough are considered for prefix search. Shorter words are |
| 32 | // considered for exact matches. |
| 33 | DEFAULT, |
| 34 | // All words are considered for a prefix search. |
| 35 | ALWAYS_PREFIX_SEARCH, |
| 36 | }; |
| 37 | |
avi | 8e000a7 | 2016-11-02 18:06:20 | [diff] [blame] | 38 | using QueryWordVector = std::vector<query_parser::QueryWord>; |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 39 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 40 | // QueryNode is used by QueryParser to represent the elements that constitute a |
| 41 | // query. While QueryNode is exposed by way of ParseQuery, it really isn't meant |
| 42 | // for external usage. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | class QueryNode { |
| 44 | public: |
| 45 | virtual ~QueryNode() {} |
| 46 | |
| 47 | // Serialize ourselves out to a string that can be passed to SQLite. Returns |
| 48 | // the number of words in this node. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 49 | virtual int AppendToSQLiteQuery(base::string16* query) const = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 51 | // Return true if this is a QueryNodeWord, false if it's a QueryNodeList. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | virtual bool IsWord() const = 0; |
| 53 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 54 | // Returns true if this node matches |word|. If |exact| is true, the string |
| 55 | // must exactly match. Otherwise, this uses a starts with comparison. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 56 | virtual bool Matches(const base::string16& word, bool exact) const = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | |
[email protected] | 2532060 | 2012-10-18 22:05:56 | [diff] [blame] | 58 | // Returns true if this node matches at least one of the words in |words|. An |
| 59 | // entry is added to |match_positions| for all matching words giving the |
| 60 | // matching regions. |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 61 | virtual bool HasMatchIn(const QueryWordVector& words, |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 62 | Snippet::MatchPositions* match_positions) const = 0; |
[email protected] | 7de9959 | 2008-12-09 19:16:02 | [diff] [blame] | 63 | |
[email protected] | 5d592f0 | 2013-06-22 16:59:22 | [diff] [blame] | 64 | // Returns true if this node matches at least one of the words in |words|. |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 65 | virtual bool HasMatchIn(const QueryWordVector& words) const = 0; |
[email protected] | 5d592f0 | 2013-06-22 16:59:22 | [diff] [blame] | 66 | |
[email protected] | 7de9959 | 2008-12-09 19:16:02 | [diff] [blame] | 67 | // Appends the words that make up this node in |words|. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 68 | virtual void AppendWords(std::vector<base::string16>* words) const = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | }; |
| 70 | |
avi | 8e000a7 | 2016-11-02 18:06:20 | [diff] [blame] | 71 | using QueryNodeVector = std::vector<std::unique_ptr<query_parser::QueryNode>>; |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 72 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 73 | // This class is used to parse queries entered into the history search into more |
| 74 | // normalized queries that can be passed to the SQLite backend. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | class QueryParser { |
| 76 | public: |
| 77 | QueryParser(); |
| 78 | |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 79 | // For CJK ideographs and Korean Hangul, even a single character |
| 80 | // can be useful in prefix matching, but that may give us too many |
| 81 | // false positives. Moreover, the current ICU word breaker gives us |
| 82 | // back every single Chinese character as a word so that there's no |
| 83 | // point doing anything for them and we only adjust the minimum length |
| 84 | // to 2 for Korean Hangul while using 3 for others. This is a temporary |
| 85 | // hack until we have a segmentation support. |
kkimlabs | f1a7a373 | 2014-11-04 10:30:46 | [diff] [blame] | 86 | static bool IsWordLongEnoughForPrefixSearch( |
| 87 | const base::string16& word, |
| 88 | MatchingAlgorithm matching_algorithm); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 89 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | // Parse a query into a SQLite query. The resulting query is placed in |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 91 | // |sqlite_query| and the number of words is returned. |
kkimlabs | f1a7a373 | 2014-11-04 10:30:46 | [diff] [blame] | 92 | int ParseQuery(const base::string16& query, |
| 93 | MatchingAlgorithm matching_algorithm, |
| 94 | base::string16* sqlite_query); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | |
[email protected] | 8c793c8 | 2011-05-19 00:41:33 | [diff] [blame] | 96 | // Parses |query|, returning the words that make up it. Any words in quotes |
| 97 | // are put in |words| without the quotes. For example, the query text |
| 98 | // "foo bar" results in two entries being added to words, one for foo and one |
| 99 | // for bar. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 100 | void ParseQueryWords(const base::string16& query, |
kkimlabs | f1a7a373 | 2014-11-04 10:30:46 | [diff] [blame] | 101 | MatchingAlgorithm matching_algorithm, |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 102 | std::vector<base::string16>* words); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | |
[email protected] | 8c793c8 | 2011-05-19 00:41:33 | [diff] [blame] | 104 | // Parses |query|, returning the nodes that constitute the valid words in the |
| 105 | // query. This is intended for later usage with DoesQueryMatch. Ownership of |
| 106 | // the nodes passes to the caller. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 107 | void ParseQueryNodes(const base::string16& query, |
kkimlabs | f1a7a373 | 2014-11-04 10:30:46 | [diff] [blame] | 108 | MatchingAlgorithm matching_algorithm, |
avi | 8e000a7 | 2016-11-02 18:06:20 | [diff] [blame] | 109 | QueryNodeVector* nodes); |
[email protected] | 7de9959 | 2008-12-09 19:16:02 | [diff] [blame] | 110 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | // Returns true if the string text matches the query nodes created by a call |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 112 | // to ParseQuery. If the query does match, each of the matching positions in |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 113 | // the text is added to |match_positions|. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 114 | bool DoesQueryMatch(const base::string16& text, |
avi | 8e000a7 | 2016-11-02 18:06:20 | [diff] [blame] | 115 | const QueryNodeVector& nodes, |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 116 | Snippet::MatchPositions* match_positions); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | |
[email protected] | 5d592f0 | 2013-06-22 16:59:22 | [diff] [blame] | 118 | // Returns true if all of the |words| match the query |nodes| created by a |
| 119 | // call to ParseQuery. |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 120 | bool DoesQueryMatch(const QueryWordVector& words, |
avi | 8e000a7 | 2016-11-02 18:06:20 | [diff] [blame] | 121 | const QueryNodeVector& nodes); |
[email protected] | 5d592f0 | 2013-06-22 16:59:22 | [diff] [blame] | 122 | |
| 123 | // Extracts the words from |text|, placing each word into |words|. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 124 | void ExtractQueryWords(const base::string16& text, |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 125 | QueryWordVector* words); |
| 126 | |
| 127 | // Sorts the match positions in |matches| by their first index, then |
| 128 | // coalesces any match positions that intersect each other. |
| 129 | static void SortAndCoalesceMatchPositions(Snippet::MatchPositions* matches); |
[email protected] | 5d592f0 | 2013-06-22 16:59:22 | [diff] [blame] | 130 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | private: |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 132 | // Does the work of parsing |query|; creates nodes in |root| as appropriate. |
| 133 | // This is invoked from both of the ParseQuery methods. |
kkimlabs | f1a7a373 | 2014-11-04 10:30:46 | [diff] [blame] | 134 | bool ParseQueryImpl(const base::string16& query, |
| 135 | MatchingAlgorithm matching_algorithm, |
| 136 | QueryNodeList* root); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 138 | DISALLOW_COPY_AND_ASSIGN(QueryParser); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | }; |
| 140 | |
[email protected] | acf9f27 | 2014-04-15 23:04:00 | [diff] [blame] | 141 | } // namespace query_parser |
| 142 | |
| 143 | #endif // COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ |