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 typedef std::vector<query_parser::QueryWord> QueryWordVector; |
| 28 |
27 // QueryNode is used by QueryParser to represent the elements that constitute a | 29 // QueryNode is used by QueryParser to represent the elements that constitute a |
28 // query. While QueryNode is exposed by way of ParseQuery, it really isn't meant | 30 // query. While QueryNode is exposed by way of ParseQuery, it really isn't meant |
29 // for external usage. | 31 // for external usage. |
30 class QueryNode { | 32 class QueryNode { |
31 public: | 33 public: |
32 virtual ~QueryNode() {} | 34 virtual ~QueryNode() {} |
33 | 35 |
34 // Serialize ourselves out to a string that can be passed to SQLite. Returns | 36 // Serialize ourselves out to a string that can be passed to SQLite. Returns |
35 // the number of words in this node. | 37 // the number of words in this node. |
36 virtual int AppendToSQLiteQuery(base::string16* query) const = 0; | 38 virtual int AppendToSQLiteQuery(base::string16* query) const = 0; |
37 | 39 |
38 // Return true if this is a QueryNodeWord, false if it's a QueryNodeList. | 40 // Return true if this is a QueryNodeWord, false if it's a QueryNodeList. |
39 virtual bool IsWord() const = 0; | 41 virtual bool IsWord() const = 0; |
40 | 42 |
41 // Returns true if this node matches |word|. If |exact| is true, the string | 43 // Returns true if this node matches |word|. If |exact| is true, the string |
42 // must exactly match. Otherwise, this uses a starts with comparison. | 44 // must exactly match. Otherwise, this uses a starts with comparison. |
43 virtual bool Matches(const base::string16& word, bool exact) const = 0; | 45 virtual bool Matches(const base::string16& word, bool exact) const = 0; |
44 | 46 |
45 // Returns true if this node matches at least one of the words in |words|. An | 47 // Returns true if this node matches at least one of the words in |words|. An |
46 // entry is added to |match_positions| for all matching words giving the | 48 // entry is added to |match_positions| for all matching words giving the |
47 // matching regions. | 49 // matching regions. |
48 virtual bool HasMatchIn(const std::vector<QueryWord>& words, | 50 virtual bool HasMatchIn(const QueryWordVector& words, |
49 Snippet::MatchPositions* match_positions) const = 0; | 51 Snippet::MatchPositions* match_positions) const = 0; |
50 | 52 |
51 // Returns true if this node matches at least one of the words in |words|. | 53 // Returns true if this node matches at least one of the words in |words|. |
52 virtual bool HasMatchIn(const std::vector<QueryWord>& words) const = 0; | 54 virtual bool HasMatchIn(const QueryWordVector& words) const = 0; |
53 | 55 |
54 // Appends the words that make up this node in |words|. | 56 // Appends the words that make up this node in |words|. |
55 virtual void AppendWords(std::vector<base::string16>* words) const = 0; | 57 virtual void AppendWords(std::vector<base::string16>* words) const = 0; |
56 }; | 58 }; |
57 | 59 |
| 60 typedef std::vector<query_parser::QueryNode*> QueryNodeStarVector; |
| 61 |
58 // This class is used to parse queries entered into the history search into more | 62 // This class is used to parse queries entered into the history search into more |
59 // normalized queries that can be passed to the SQLite backend. | 63 // normalized queries that can be passed to the SQLite backend. |
60 class QueryParser { | 64 class QueryParser { |
61 public: | 65 public: |
62 QueryParser(); | 66 QueryParser(); |
63 | 67 |
64 // For CJK ideographs and Korean Hangul, even a single character | 68 // For CJK ideographs and Korean Hangul, even a single character |
65 // can be useful in prefix matching, but that may give us too many | 69 // can be useful in prefix matching, but that may give us too many |
66 // false positives. Moreover, the current ICU word breaker gives us | 70 // false positives. Moreover, the current ICU word breaker gives us |
67 // back every single Chinese character as a word so that there's no | 71 // back every single Chinese character as a word so that there's no |
(...skipping 10 matching lines...) Expand all Loading... |
78 // are put in |words| without the quotes. For example, the query text | 82 // are put in |words| without the quotes. For example, the query text |
79 // "foo bar" results in two entries being added to words, one for foo and one | 83 // "foo bar" results in two entries being added to words, one for foo and one |
80 // for bar. | 84 // for bar. |
81 void ParseQueryWords(const base::string16& query, | 85 void ParseQueryWords(const base::string16& query, |
82 std::vector<base::string16>* words); | 86 std::vector<base::string16>* words); |
83 | 87 |
84 // Parses |query|, returning the nodes that constitute the valid words in the | 88 // Parses |query|, returning the nodes that constitute the valid words in the |
85 // query. This is intended for later usage with DoesQueryMatch. Ownership of | 89 // query. This is intended for later usage with DoesQueryMatch. Ownership of |
86 // the nodes passes to the caller. | 90 // the nodes passes to the caller. |
87 void ParseQueryNodes(const base::string16& query, | 91 void ParseQueryNodes(const base::string16& query, |
88 std::vector<QueryNode*>* nodes); | 92 QueryNodeStarVector* nodes); |
89 | 93 |
90 // Returns true if the string text matches the query nodes created by a call | 94 // Returns true if the string text matches the query nodes created by a call |
91 // to ParseQuery. If the query does match, each of the matching positions in | 95 // to ParseQuery. If the query does match, each of the matching positions in |
92 // the text is added to |match_positions|. | 96 // the text is added to |match_positions|. |
93 bool DoesQueryMatch(const base::string16& text, | 97 bool DoesQueryMatch(const base::string16& text, |
94 const std::vector<QueryNode*>& nodes, | 98 const QueryNodeStarVector& nodes, |
95 Snippet::MatchPositions* match_positions); | 99 Snippet::MatchPositions* match_positions); |
96 | 100 |
97 // Returns true if all of the |words| match the query |nodes| created by a | 101 // Returns true if all of the |words| match the query |nodes| created by a |
98 // call to ParseQuery. | 102 // call to ParseQuery. |
99 bool DoesQueryMatch(const std::vector<QueryWord>& words, | 103 bool DoesQueryMatch(const QueryWordVector& words, |
100 const std::vector<QueryNode*>& nodes); | 104 const QueryNodeStarVector& nodes); |
101 | 105 |
102 // Extracts the words from |text|, placing each word into |words|. | 106 // Extracts the words from |text|, placing each word into |words|. |
103 void ExtractQueryWords(const base::string16& text, | 107 void ExtractQueryWords(const base::string16& text, |
104 std::vector<QueryWord>* words); | 108 QueryWordVector* words); |
| 109 |
| 110 // Sorts the match positions in |matches| by their first index, then |
| 111 // coalesces any match positions that intersect each other. |
| 112 static void SortAndCoalesceMatchPositions(Snippet::MatchPositions* matches); |
105 | 113 |
106 private: | 114 private: |
107 // Does the work of parsing |query|; creates nodes in |root| as appropriate. | 115 // Does the work of parsing |query|; creates nodes in |root| as appropriate. |
108 // This is invoked from both of the ParseQuery methods. | 116 // This is invoked from both of the ParseQuery methods. |
109 bool ParseQueryImpl(const base::string16& query, QueryNodeList* root); | 117 bool ParseQueryImpl(const base::string16& query, QueryNodeList* root); |
110 | 118 |
111 DISALLOW_COPY_AND_ASSIGN(QueryParser); | 119 DISALLOW_COPY_AND_ASSIGN(QueryParser); |
112 }; | 120 }; |
113 | 121 |
114 } // namespace query_parser | 122 } // namespace query_parser |
115 | 123 |
116 #endif // COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ | 124 #endif // COMPONENTS_QUERY_PARSER_QUERY_PARSER_H_ |
OLD | NEW |