aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/fieldmatch/metrics.h
blob: ddec235d032e0f89c86b72734129dc7d53c164b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/searchlib/common/feature.h>
#include <vespa/vespalib/stllike/string.h>
#include <vector>
#include <memory>

namespace search::features::fieldmatch {

class Computer;

/**
 * The collection of metrics calculated by the string match metric calculator.
 *
 * @author Jon Bratseth
 * @author Simon Thoresen Hult
 */
class Metrics {
public:
    /**
     * Convenience typedefs.
     */
    using UP = std::unique_ptr<Metrics>;
    using SP = std::shared_ptr<Metrics>;

public:
    /**
     * Constructs a new metrics object.
     *
     * @param source The source of this.
     */
    Metrics(const Computer *source);

    /**
     * Implements the copy constructor.
     *
     * @param rhs The metrics to copy.
     */
    Metrics(const Metrics &rhs);

    /**
     * Implements the assignment operator.
     */
    Metrics & operator=(const Metrics & rhs);

    /**
     * Resets this object.
     */
    void reset();

    /**
     * Are these metrics representing a complete match.
     *
     * @return Whether or not this represents a complete match.
     */
    bool isComplete() const {
        return _complete;
    }

    /**
     * Sets whether or not these metrics represent a complete match.
     *
     * @param complete Whether or not this represents a complete match.
     * @return This, to allow chaining.
     */
    Metrics &setComplete(bool complete) {
        _complete = complete;
        return *this;
    }

    /**
     * Returns the segment start points.
     *
     * @return The start point list.
     */
    std::vector<uint32_t> &getSegmentStarts() {
        return _segmentStarts;
    }

    /**
     * Returns the total number of out of order token sequences within field segments.
     *
     * @return The number of tokens.
     */
    uint32_t getOutOfOrder() const {
        return _outOfOrder;
    }

    /**
     * Returns the number of field text segments which are needed to match the query as completely as possible.
     *
     * @return The number of segments.
     */
    uint32_t getSegments() const {
        return _segments;
    }

    /**
     * Returns the total number of position jumps (backward or forward) within document segments.
     *
     * @return The number of position jumps.
     */
    uint32_t getGaps() const {
        return _gaps;
    }

    /**
     * Returns the summed size of all gaps within segments.
     *
     * @return The summed size.
     */
    uint32_t getGapLength() const {
        return _gapLength;
    }

    /**
     * Returns the size of the longest matched continuous, in-order sequence in the document.
     *
     * @return The size of the sequence.
     */
    uint32_t getLongestSequence() const {
        return _longestSequence;
    }

    /**
     * Returns the number of tokens in the field preceding the start of the first matched segment.
     *
     * @return The number of tokens.
     */
    int getHead() const {
        return _head;
    }

    /**
     * Returns the number of tokens in the field following the end of the last matched segment.
     *
     * @return The number of tokens.
     */
    int getTail() const {
        return _tail;
    }

    /**
     * Returns the number of query terms which was matched in this field.
     *
     * @return The number of matched terms.
     */
    uint32_t getMatches() const {
        return _matches;
    }

    /**
     * Returns the number of in-segment token pairs.
     *
     * @return The number of token pairs.
     */
    uint32_t getPairs() const {
        return _pairs;
    }

    /**
     * Returns the normalized proximity of the matched terms, weighted by the connectedness of the query terms.  This
     * number is 0.1 if all the matched terms are and have default or lower connectedness, close to 1 if they are
     * following in sequence and have a high connectedness, and close to 0 if they are far from each other in the
     * segment or out of order.
     *
     * @return The proximity.
     */
    feature_t getAbsoluteProximity() const {
        return _pairs < 1 ? 0.1f : _proximity / _pairs;
    }

    /**
     * Returns the normalized proximity of the matched terms, not taking term connectedness into account.  This number
     * is close to 1 if all the matched terms are following each other in sequence, and close to 0 if they are far from
     * each other or out of order
     *
     * @return The proximity.
     */
    feature_t getUnweightedProximity() const {
        return _pairs < 1 ? 1.0f : _unweightedProximity / _pairs;
    }

    /**
     * Returns the sum of the distance between all segments making up a match to the query, measured as the sum of the
     * number of token positions separating the <i>start</i> of each field adjacent segment.
     *
     * @return The sum distance.
     */
    feature_t getSegmentDistance() const {
        return _segmentDistance;
    }

    /**
     * <p>Returns the normalized weight of this match relative to the whole query: The sum of the weights of all
     * <i>matched</i> terms/the sum of the weights of all <i>query</i> terms If all the query terms were matched, this
     * is 1. If no terms were matched, or these matches has weight zero, this is 0.</p>
     *
     * <p>As the sum of this number over all the terms of the query is always 1, sums over all fields of normalized rank
     * features for each field multiplied by this number for the same field will produce a normalized number.</p>
     *
     * <p>Note that this scales with the number of matched query terms in the field. If you want a component which does
     * not, divide by matches.</p>
     *
     * @return The normalized weight.
     */
    feature_t getWeight() const {
        return _weight;
    }

    /**
     * <p>Returns the normalized term significance (1-frequency) of the terms of this match relative to the whole query:
     * The sum of the significance of all <i>matched</i> terms/the sum of the significance of all <i>query</i> terms If
     * all the query terms were matched, this is 1. If no terms were matched, or if the significance of all the matched
     * terms is zero (they are present in all (possible) documents), this number is zero.</p>
     *
     * <p>As the sum of this number over all the terms of the query is always 1, sums over all fields of normalized rank
     * features for each field multiplied by this number for the same field will produce a normalized number.</p>
     *
     * <p>Note that this scales with the number of matched query terms in the field. If you want a component which does
     * not, divide by matches.</p>
     *
     * @return The normalized significance.
     */
    feature_t getSignificance() const {
        return _significance;
    }

    /**
     * <p>Returns a normalized measure of the number of occurrence of the terms of the query.  This number is 1 if there
     * are many occurences of the query terms <i>in absolute terms, or relative to the total content of the field</i>,
     * and 0 if there are none.</p>
     *
     * <p>This is suitable for occurence in fields containing regular text.</p>
     *
     * @return The normalized number of occurences.
     */
    feature_t getOccurrence() const {
        return _occurrence;
    }

    /**
     * <p>Returns a normalized measure of the number of occurrence of the terms of the query:
     *
     * <code>sum over all query terms(min(number of occurences of the term, maxOccurrences)) / (query term count *
     * 100)</code>
     *
     * <p>This number is 1 if there are many occurrences of the query terms, and 0 if there are none.  This number does
     * not take the actual length of the field into account, so it is suitable for uses of occurrence to denote
     * importance across multiple terms.</p>
     *
     * @return The normalized number of occurences.
     */
    feature_t getAbsoluteOccurrence() const {
        return _absoluteOccurrence;
    }

    /**
     * <p>Returns a normalized measure of the number of occurrence of the terms of the query, weighted by term weight.
     * This number is close to 1 if there are many occurrences of highly weighted query terms, in absolute terms, or
     * relative to the total content of the field, and 0 if there are none.</p>
     *
     * @return The normalized measure of weighted occurences.
     */
    feature_t getWeightedOccurrence() const {
        return _weightedOccurrence;
    }

    /**
     * <p>Returns a normalized measure of the number of occurrence of the terms of the query, taking weights into
     * account so that occurrences of higher weighted query terms has more impact than lower weighted terms.</p>
     *
     * <p>This number is 1 if there are many occurrences of the highly weighted terms, and 0 if there are none.  This
     * number does not take the actual length of the field into account, so it is suitable for uses of occurrence to
     * denote importance across multiple terms.</p>
     *
     * @return The normalized measure of weighted occurences.
     */
    feature_t getWeightedAbsoluteOccurrence() const {
        return _weightedAbsoluteOccurrence;
    }

    /**
     * <p>Returns a normalized measure of the number of occurrence of the terms of the query <i>in absolute terms, or
     * relative to the total content of the field</i>, weighted by term significance.
     *
     * <p>This number is 1 if there are many occurrences of the highly significant terms, and 0 if there are none.</p>
     *
     * @return The normalized measure of occurences, weighted by significance.
     */
    feature_t getSignificantOccurrence() const {
        return _significantOccurrence;
    }

    /**
     * The ratio of query tokens which was matched in the field: <code>matches/queryLength</code>.
     *
     * @return The query completeness.
     */
    feature_t getQueryCompleteness() const;

    /**
     * The ratio of query tokens which was matched in the field: <code>matches/fieldLength</code>.
     *
     * @return The field completeness.
     */
    feature_t getFieldCompleteness() const;

    /**
     * Total completeness, where field completeness is more important: <code>queryCompleteness * ( 1 -
     * fieldCompletenessImportancy + fieldCompletenessImportancy * fieldCompleteness )</code>
     *
     * @return The total completeness.
     */
    feature_t getCompleteness() const;

    /**
     * Returns how well the order of the terms agreed in segments: <code>1-outOfOrder/pairs</code>.
     *
     * @return The orderness of terms.
     */
    feature_t getOrderness() const {
        return _pairs < 1 ? 1.0f : 1 - (feature_t)_outOfOrder / _pairs;
    }

    /**
     * Returns the degree to which different terms are related (occurring in the same segment):
     * <code>1-segments/(matches-1)</code>.
     *
     * @return The relatedness of terms.
     */
    feature_t getRelatedness() const;

    /**
     * Returns <code>longestSequence/matches</code>
     *
     * @return The longest sequence ratio.
     */
    feature_t getLongestSequenceRatio() const {
        return _matches == 0 ? 0.0f : (feature_t)_longestSequence / _matches;
    }

    /**
     * Returns the closeness of the segments in the field: <code>1-segmentDistance/fieldLength</code>.
     *
     * @return The segment proximity.
     */
    feature_t getSegmentProximity() const;

    /**
     * Returns a value which is close to 1 when matched terms are close and close to zero when they are far apart in the
     * segment. Relatively more connected terms influence this value more.  This is absoluteProximity/average
     * connectedness.
     *
     * @return The matched term proximity.
     */
    feature_t getProximity() const;

    /**
     * <p>Returns the average of significance and weight.</p>
     *
     * <p>As the sum of this number over all the terms of the query is always 1, sums over all fields of normalized rank
     * features for each field multiplied by this number for the same field will produce a normalized number.</p>
     *
     * <p>Note that this scales with the number of matched query terms in the field. If you want a component which does
     * not, divide by matches.</p>
     *
     * @return The importance.
     */
    feature_t getImportance() const {
        return (getSignificance() + getWeight()) / 2;
    }

    /**
     * A normalized measure of how early the first segment occurs in this field:
     * <code>1-(head+1)/max(6,field.length)</code>.
     *
     * @return The earliness of the first segment.
     */
    feature_t getEarliness() const;

    /**
     * <p>A ready-to-use aggregate match score. Use this if you don't have time to find a better application specific
     * aggregate score of the fine grained match metrics.</p>
     *
     * <p>The current forumla is
     *
     * <code> ( proximityCompletenessImportance * (1-relatednessImportance + relatednessImportance*relatedness)
     * proximity * completeness^2 + earlinessImportance * earliness + segmentProximityImportance * segmentProximity ) /
     * (proximityCompletenessImportance + earlinessImportance + relatednessImportance)</code>
     *
     * but this is subject to change (i.e improvement) at any time.  </p>
     *
     * <p>Weight and significance are not taken into account because this is mean to capture tha quality of the match in
     * this field, while those measures relate this match to matches in other fields. This number can be multiplied with
     * those values when combining with other field match scores.</p>
     *
     * @return The match score.
     */
    feature_t getMatch() const;

    /**
     * <p>The metric use to select the best segments during execution of the string match metric algoritm.</p>
     *
     * <p>This metric, and any metric it dependends on, must be correct each time a segment is completed, not only when
     * the metrics are complete, because this metric is used to choose segments during calculation.</p>
     *
     * @return The score of the segmentation.
     */
    feature_t getSegmentationScore() const;

    /**
     * Called once for every match.
     *
     * @param i The index of the matched query term.
     */
    void onMatch(uint32_t i);


    /**
     * Called once per sequence, when the sequence starts.
     *
     * @param j Sequence starts at this position.
     */
    void onSequenceStart(uint32_t j);

    /**
     * Called once per sequence when the sequence ends.
     *
     * @param j Sequence ends at this position.
     */
    void onSequenceEnd(uint32_t j) ;

    /**
     * Called once when this value is calculated, before onComplete.
     *
     * @param occurence The new occurence value.
     */
    void setOccurrence(feature_t occurrence) {
        _occurrence = occurrence;
    }

    /**
     * Called once when this value is calculated, before onComplete.
     *
     * @param weightedOccurence The new occurence weight.
     */
    void setWeightedOccurrence(feature_t weightedOccurrence) {
        _weightedOccurrence = weightedOccurrence;
    }

    /**
     * Called once when this value is calculated, before onComplete.
     *
     * @param absoluteOccurence The new absolute occurence value.
     */
    void setAbsoluteOccurrence(feature_t absoluteOccurrence) {
        _absoluteOccurrence = absoluteOccurrence;
    }

    /**
     * Called once when this value is calculated, before onComplete.
     *
     * @param weightedAbsoluteOccurence The new absolute occurence weight.
     */
    void setWeightedAbsoluteOccurrence(feature_t weightedAbsoluteOccurrence) {
        _weightedAbsoluteOccurrence = weightedAbsoluteOccurrence;
    }

    /**
     * Called once when this value is calculated, before onComplete.
     *
     * @param significantOccurence The new significant occurence value.
     */
    void setSignificantOccurrence(feature_t significantOccurrence) {
        _significantOccurrence = significantOccurrence;
    }

    /**
     * Called once when matching is complete.
     */
    void onComplete();

    /**
     * Called when <i>any</i> pair is encountered.
     *
     * @param i         The query term matched.
     * @param j         The field term index.
     * @param previousJ The end of the previous segment, or -1 if this is the first segment.
     */
    void onPair(uint32_t i, uint32_t j, uint32_t previousJ);

    /**
     * Called when an in-sequence pair is encountered.
     *
     * @param i         The query term matched.
     * @param j         The field term index.
     * @param previousJ The end of the previous segment, or -1 if this is the first segment.
     */
    void onInSequence(uint32_t i, uint32_t j, uint32_t previousJ);

    /**
     * Called when a gap (within a sequence) is encountered.
     *
     * @param i         The query term matched.
     * @param j         The field term index.
     * @param previousJ The end of the previous segment, or -1 if this is the first segment.
     */
    void onInSegmentGap(uint32_t i, uint32_t j, uint32_t previousJ);

    /**
     * Called when a new segment is started
     *
     * @param i         The query term matched.
     * @param j         The field term index.
     * @param previousJ The end of the previous segment, or -1 if this is the first segment.
     * */
    void onNewSegment(uint32_t i, uint32_t j, uint32_t previousJ);

    /**
     * Returns a string representation of this.
     *
     * @return A string representation.
     */
    vespalib::string toString() const;

private:
    const Computer *_source;
    bool            _complete;

    // Metrics
    uint32_t  _outOfOrder;
    uint32_t  _segments;
    uint32_t  _gaps;
    uint32_t  _gapLength;
    uint32_t  _longestSequence;
    int       _head;
    int       _tail;
    uint32_t  _matches;
    feature_t _proximity;
    feature_t _unweightedProximity;
    feature_t _segmentDistance;
    uint32_t  _pairs;
    feature_t _weight;
    feature_t _significance;
    feature_t _occurrence;
    feature_t _weightedOccurrence;
    feature_t _absoluteOccurrence;
    feature_t _weightedAbsoluteOccurrence;
    feature_t _significantOccurrence;

    // Temporary variables
    uint32_t              _currentSequence;
    std::vector<uint32_t> _segmentStarts;
    uint32_t              _queryLength; // num terms searching this field
};

}