This class represents a search query in a vector space of tokens.
| 76 | |
| 77 | // This class represents a search query in a vector space of tokens. |
| 78 | class QueryVec |
| 79 | { |
| 80 | public: |
| 81 | class Builder |
| 82 | { |
| 83 | public: |
| 84 | template <typename Token> |
| 85 | void AddFull(Token && token) |
| 86 | { |
| 87 | m_tokens.emplace_back(std::forward<Token>(token)); |
| 88 | } |
| 89 | |
| 90 | template <typename Token> |
| 91 | void SetPrefix(Token && token) |
| 92 | { |
| 93 | m_prefix = std::forward<Token>(token); |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | friend class QueryVec; |
| 98 | |
| 99 | std::vector<strings::UniString> m_tokens; |
| 100 | std::optional<strings::UniString> m_prefix; |
| 101 | }; |
| 102 | |
| 103 | explicit QueryVec(IdfMap & idfs) : m_idfs(&idfs) {} |
| 104 | |
| 105 | QueryVec(IdfMap & idfs, Builder const & builder); |
| 106 | |
| 107 | // Computes cosine similarity between |*this| and |rhs|. |
| 108 | double Similarity(IdfMap & docIdfs, DocVec const & rhs); |
| 109 | |
| 110 | // Computes vector norm of the query. |
| 111 | double Norm(); |
| 112 | |
| 113 | bool Empty() const { return m_tfs.empty() && !m_prefix; } |
| 114 | |
| 115 | private: |
| 116 | double GetFullTokenWeight(size_t i); |
| 117 | double GetPrefixTokenWeight(); |
| 118 | |
| 119 | friend std::string DebugPrint(QueryVec const & qv) |
| 120 | { |
| 121 | std::ostringstream os; |
| 122 | os << "QueryVec " + ::DebugPrint(qv.m_tfs); |
| 123 | if (qv.m_prefix) |
| 124 | os << " " << DebugPrint(*qv.m_prefix); |
| 125 | return os.str(); |
| 126 | } |
| 127 | |
| 128 | IdfMap * m_idfs; |
| 129 | std::vector<TokenFrequencyPair> m_tfs; |
| 130 | std::optional<strings::UniString> m_prefix; |
| 131 | }; |
| 132 | } // namespace search |