MCPcopy Create free account
hub / github.com/comaps/comaps / Similarity

Method Similarity

libs/search/doc_vec.cpp:117–217  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115}
116
117double QueryVec::Similarity(IdfMap & docIdfs, DocVec const & rhs)
118{
119 size_t kInvalidIndex = numeric_limits<size_t>::max();
120
121 if (Empty() && rhs.Empty())
122 return 1.0;
123
124 if (Empty() || rhs.Empty())
125 return 0.0;
126
127 vector<size_t> rsMatchTo(rhs.GetNumTokens(), kInvalidIndex);
128
129 double dot = 0;
130 {
131 size_t i = 0, j = 0;
132
133 while (i < m_tfs.size() && j < rhs.GetNumTokens())
134 {
135 auto const & lt = m_tfs[i].m_token;
136 auto const & rt = rhs.GetToken(j);
137
138 if (lt < rt)
139 {
140 ++i;
141 }
142 else if (lt > rt)
143 {
144 ++j;
145 }
146 else
147 {
148 dot += GetFullTokenWeight(i) * rhs.GetWeight(docIdfs, j);
149 rsMatchTo[j] = i;
150 ++i;
151 ++j;
152 }
153 }
154 }
155
156 auto const ln = Norm();
157 auto const rn = rhs.Norm(docIdfs);
158
159 // This similarity metric assumes that prefix is not matched in the document.
160 double const similarityNoPrefix = ln > 0 && rn > 0 ? dot / sqrt(ln) / sqrt(rn) : 0;
161
162 if (!m_prefix)
163 return similarityNoPrefix;
164
165 double similarityWithPrefix = 0;
166 auto const & prefix = *m_prefix;
167
168 // Let's try to match prefix token with all tokens in the
169 // document, and compute the best cosine distance.
170 for (size_t j = 0; j < rhs.GetNumTokens(); ++j)
171 {
172 auto const & t = rhs.GetToken(j);
173 if (!strings::StartsWith(t.begin(), t.end(), prefix.begin(), prefix.end()))
174 continue;

Callers 2

GetSimilarityMethod · 0.80
FillRankingInfoFunction · 0.80

Calls 11

GetTfIdfFunction · 0.85
NormMethod · 0.80
GetIdfMethod · 0.80
StartsWithFunction · 0.50
EmptyMethod · 0.45
GetNumTokensMethod · 0.45
sizeMethod · 0.45
GetWeightMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected