| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ | 5 #ifndef COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ |
| 6 #define COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ | 6 #define COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "components/query_parser/snippet.h" | 12 #include "components/query_parser/snippet.h" |
| 13 | 13 |
| 14 namespace query_parser { | 14 namespace query_parser { |
| 15 | 15 |
| 16 class QueryNodeList; | 16 class QueryNodeList; |
| 17 | 17 |
| 18 // Used by HasMatchIn. | 18 // Used by HasMatchIn. |
| 19 struct QueryWord { | 19 struct QueryWord { |
| 20 // The work to match against. | 20 // The work to match against. |
| 21 base::string16 word; | 21 base::string16 word; |
| 22 | 22 |
| 23 // The starting position of the word in the original text. | 23 // The starting position of the word in the original text. |
| 24 size_t position; | 24 size_t position; |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 enum 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 |
| 27 typedef std::vector<query_parser::QueryWord> QueryWordVector; | 35 typedef std::vector<query_parser::QueryWord> QueryWordVector; |
| 28 | 36 |
| 29 // QueryNode is used by QueryParser to represent the elements that constitute a | 37 // QueryNode is used by QueryParser to represent the elements that constitute a |
| 30 // query. While QueryNode is exposed by way of ParseQuery, it really isn't meant | 38 // query. While QueryNode is exposed by way of ParseQuery, it really isn't meant |
| 31 // for external usage. | 39 // for external usage. |
| 32 class QueryNode { | 40 class QueryNode { |
| 33 public: | 41 public: |
| 34 virtual ~QueryNode() {} | 42 virtual ~QueryNode() {} |
| 35 | 43 |
| 36 // Serialize ourselves out to a string that can be passed to SQLite. Returns | 44 // Serialize ourselves out to a string that can be passed to SQLite. Returns |
| (...skipping 28 matching lines...) Expand all Loading... |
| 65 public: | 73 public: |
| 66 QueryParser(); | 74 QueryParser(); |
| 67 | 75 |
| 68 // For CJK ideographs and Korean Hangul, even a single character | 76 // For CJK ideographs and Korean Hangul, even a single character |
| 69 // can be useful in prefix matching, but that may give us too many | 77 // can be useful in prefix matching, but that may give us too many |
| 70 // false positives. Moreover, the current ICU word breaker gives us | 78 // false positives. Moreover, the current ICU word breaker gives us |
| 71 // back every single Chinese character as a word so that there's no | 79 // back every single Chinese character as a word so that there's no |
| 72 // point doing anything for them and we only adjust the minimum length | 80 // point doing anything for them and we only adjust the minimum length |
| 73 // to 2 for Korean Hangul while using 3 for others. This is a temporary | 81 // to 2 for Korean Hangul while using 3 for others. This is a temporary |
| 74 // hack until we have a segmentation support. | 82 // hack until we have a segmentation support. |
| 75 static bool IsWordLongEnoughForPrefixSearch(const base::string16& word); | 83 static bool IsWordLongEnoughForPrefixSearch( |
| 84 const base::string16& word, |
| 85 MatchingAlgorithm matching_algorithm); |
| 76 | 86 |
| 77 // Parse a query into a SQLite query. The resulting query is placed in | 87 // Parse a query into a SQLite query. The resulting query is placed in |
| 78 // |sqlite_query| and the number of words is returned. | 88 // |sqlite_query| and the number of words is returned. |
| 79 int ParseQuery(const base::string16& query, base::string16* sqlite_query); | 89 int ParseQuery(const base::string16& query, |
| 90 MatchingAlgorithm matching_algorithm, |
| 91 base::string16* sqlite_query); |
| 80 | 92 |
| 81 // Parses |query|, returning the words that make up it. Any words in quotes | 93 // Parses |query|, returning the words that make up it. Any words in quotes |
| 82 // are put in |words| without the quotes. For example, the query text | 94 // are put in |words| without the quotes. For example, the query text |
| 83 // "foo bar" results in two entries being added to words, one for foo and one | 95 // "foo bar" results in two entries being added to words, one for foo and one |
| 84 // for bar. | 96 // for bar. |
| 85 void ParseQueryWords(const base::string16& query, | 97 void ParseQueryWords(const base::string16& query, |
| 98 MatchingAlgorithm matching_algorithm, |
| 86 std::vector<base::string16>* words); | 99 std::vector<base::string16>* words); |
| 87 | 100 |
| 88 // Parses |query|, returning the nodes that constitute the valid words in the | 101 // Parses |query|, returning the nodes that constitute the valid words in the |
| 89 // query. This is intended for later usage with DoesQueryMatch. Ownership of | 102 // query. This is intended for later usage with DoesQueryMatch. Ownership of |
| 90 // the nodes passes to the caller. | 103 // the nodes passes to the caller. |
| 91 void ParseQueryNodes(const base::string16& query, | 104 void ParseQueryNodes(const base::string16& query, |
| 105 MatchingAlgorithm matching_algorithm, |
| 92 QueryNodeStarVector* nodes); | 106 QueryNodeStarVector* nodes); |
| 93 | 107 |
| 94 // Returns true if the string text matches the query nodes created by a call | 108 // Returns true if the string text matches the query nodes created by a call |
| 95 // to ParseQuery. If the query does match, each of the matching positions in | 109 // to ParseQuery. If the query does match, each of the matching positions in |
| 96 // the text is added to |match_positions|. | 110 // the text is added to |match_positions|. |
| 97 bool DoesQueryMatch(const base::string16& text, | 111 bool DoesQueryMatch(const base::string16& text, |
| 98 const QueryNodeStarVector& nodes, | 112 const QueryNodeStarVector& nodes, |
| 99 Snippet::MatchPositions* match_positions); | 113 Snippet::MatchPositions* match_positions); |
| 100 | 114 |
| 101 // Returns true if all of the |words| match the query |nodes| created by a | 115 // Returns true if all of the |words| match the query |nodes| created by a |
| 102 // call to ParseQuery. | 116 // call to ParseQuery. |
| 103 bool DoesQueryMatch(const QueryWordVector& words, | 117 bool DoesQueryMatch(const QueryWordVector& words, |
| 104 const QueryNodeStarVector& nodes); | 118 const QueryNodeStarVector& nodes); |
| 105 | 119 |
| 106 // Extracts the words from |text|, placing each word into |words|. | 120 // Extracts the words from |text|, placing each word into |words|. |
| 107 void ExtractQueryWords(const base::string16& text, | 121 void ExtractQueryWords(const base::string16& text, |
| 108 QueryWordVector* words); | 122 QueryWordVector* words); |
| 109 | 123 |
| 110 // Sorts the match positions in |matches| by their first index, then | 124 // Sorts the match positions in |matches| by their first index, then |
| 111 // coalesces any match positions that intersect each other. | 125 // coalesces any match positions that intersect each other. |
| 112 static void SortAndCoalesceMatchPositions(Snippet::MatchPositions* matches); | 126 static void SortAndCoalesceMatchPositions(Snippet::MatchPositions* matches); |
| 113 | 127 |
| 114 private: | 128 private: |
| 115 // Does the work of parsing |query|; creates nodes in |root| as appropriate. | 129 // Does the work of parsing |query|; creates nodes in |root| as appropriate. |
| 116 // This is invoked from both of the ParseQuery methods. | 130 // This is invoked from both of the ParseQuery methods. |
| 117 bool ParseQueryImpl(const base::string16& query, QueryNodeList* root); | 131 bool ParseQueryImpl(const base::string16& query, |
| 132 MatchingAlgorithm matching_algorithm, |
| 133 QueryNodeList* root); |
| 118 | 134 |
| 119 DISALLOW_COPY_AND_ASSIGN(QueryParser); | 135 DISALLOW_COPY_AND_ASSIGN(QueryParser); |
| 120 }; | 136 }; |
| 121 | 137 |
| 122 } // namespace query_parser | 138 } // namespace query_parser |
| 123 | 139 |
| 124 #endif // COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ | 140 #endif // COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ |
| OLD | NEW |