aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsa/vectorizer.h
blob: 2fe75eb7a879db1c5e41a37543b42ec5aca0e197 (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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/08/20
 * @version $Id$
 * @file    vectorizer.h
 * @brief   Simple document vectorizer based on %FSA (%Finite %State %Automaton)
 */

#pragma once

#include <string>
#include <map>
#include <vector>

#include "fsa.h"
#include "detector.h"

namespace fsa {

// {{{ Vectorizer

/**
 * @class Vectorizer
 * @brief Simple document vectorizer based on %FSA.
 */
class Vectorizer {

public:

  // {{{ Vectorizer::VectorItem

  /**
   * @class VectorItem
   * @brief Document vector item.
   *
   * Document vector item. Contains a term/phrase and an assigned
   * weight, and provides comparison operators for sorting.
   */
  class VectorItem {
  public:
    using Hit = std::pair<unsigned int /*position*/, int /*length*/>;
    using Hits = std::vector<Hit>;
  private:
    std::string   _term;     /**< Term/phrase. */
    double        _weight;   /**< Term weight. */
    Hits          _hits;     /**< The token positions at which the term was found */
  public:
    /**
     * @brief Default constructor, creates empty item with zero weight.
     */
    VectorItem() noexcept : _term(), _weight(0.0), _hits() {}

    /**
     * @brief Copy constructor.
     *
     * @param v VectorItem to copy.
     */
    VectorItem(const VectorItem &v) : _term(v._term), _weight(v._weight), _hits(v._hits) {}

    /**
     * @brief Constructor.
     *
     * Creates a vector item from a string and a weight.
     *
     * @param t Term/phrase.
     * @param w Weight.
     */
    VectorItem(const std::string t, double w) : _term(t), _weight(w), _hits() {}

    /**
     * @brief Constructor.
     *
     * Creates a vector item from a string and a weight.
     *
     * @param t Term/phrase.
     * @param w Weight.
     */
    VectorItem(const std::string t, double w, const Hits &h) : _term(t), _weight(w), _hits(h) {}

    /**
     * @brief Destructor.
     */
    ~VectorItem() {}

    /**
     * @brief Assignment operator.
     *
     * @param v VectorItem.
     * @return Reference to (this) VectorItem.
     */
    const VectorItem& operator=(const VectorItem& v)
    {
      _term = v._term;
      _weight = v._weight;
      _hits = v._hits;
      return *this;
    }

    /**
     * @brief Less-than operator.
     *
     * The order is highest weight first, than sorted alphabetically.
     *
     * @param v Other vector item.
     * @return True is this item<other item.
     */
    bool operator<(const VectorItem & v) const
    {
      if(_weight>v._weight) return true;
      if(_weight<v._weight) return false;
      if(_term<v._term) return true;
      return false;
    }

    /**
     * @brief Greater-than operator.
     *
     * The order is highest weight first, than sorted alphabetically.
     *
     * @param v Other vector item.
     * @return True is this item>other item.
     */
    bool operator>(const VectorItem & v) const
    {
      if(_weight<v._weight) return true;
      if(_weight>v._weight) return false;
      if(_term>v._term) return true;
      return false;
    }

    /**
     * @brief Equals operator.
     *
     * Two VectorItems equal if both the terms and weight are equal.
     *
     * @param v Other vector item.
     * @return True is this item==other item.
     */
    bool operator==(const VectorItem & v) const
    {
      if(_weight==v._weight && _term==v._term) return true;
      return false;
    }

    /**
     * @brief Get the term/phrase.
     *
     * @return (Copy of) term/phrase.
     */
    std::string term() const { return _term; }

    /**
     * @brief An obsolete alias for term().
     *
     * @return (Copy of) term/phrase.
     */
    std::string getTerm() const { return _term; }

    /**
     * @brief Get the weight.
     *
     * @return Weight.
     */
    double weight() const { return _weight; }

    /**
     * @brief An obsolete alias for weight().
     *
     * @return Weight.
     */
    double getWeight() const { return _weight; }

    /**
     * @brief Get the hits.
     *
     * @return A reference to the hits vector.
     */
    const Hits &hits() const { return _hits; }

  };

  // }}}

  // {{{ Vectorizer::TfIdf

  /**
   * @class TfIdf
   * @brief Class for computing TfIdf weights.
   *
   * Class for computing TfIdf (term frequency/inverse document
   * frequency) weights.
   */
  class TfIdf {
  private:
    unsigned int _tf;   /**< Term frequency.               */
    unsigned int _idf;  /**< (Inverse) document frequency. */
  public:
    /**
     * @brief Default constructor.
     */
    TfIdf() : _tf(0), _idf(0) {}

    /**
     * @brief Copy constructor.
     *
     * @param ti TfIdf object to copy.
     */
    TfIdf(const TfIdf &ti) : _tf(ti._tf), _idf(ti._idf) {}

    /**
     * @brief Constructor.
     *
     * @param t Term frequency.
     * @param i (Inverse) document frequency.
     */
    TfIdf(unsigned int t, unsigned int i) : _tf(t), _idf(i) {}

    /**
     * @brief Destructor.
     */
    ~TfIdf() {}

    /**
     * @brief Assignment operator.
     *
     * @param ti Reference to TfIdf object.
     * @return Reference to (this) TfIdf object.
     */
    const TfIdf& operator=(const TfIdf& ti)
    {
      _tf = ti._tf;
      _idf = ti._idf;
      return *this;
    }

    /**
     * @brief Assignment operator, set only Tf.
     *
     * @param t Term frequency.
     * @return Reference to (this) TfIdf object.
     */
    const TfIdf& operator=(unsigned int t)
    {
      _tf = t;
      return *this;
    }

    /**
     * @brief Prefix increment operator.
     *
     * Prefix increment operator, increments Tf.
     *
     * @return Reference to (this) TfIdf object.
     */
    TfIdf& operator++()
    {
      ++_tf;
      return *this;
    }

    /**
     * @brief += operator.
     *
     * += operator, adds the parameter to Tf.
     *
     * @return Reference to (this) TfIdf object.
     */
    const TfIdf& operator+=(unsigned int t)
    {
      _tf+=t;
      return *this;
    }

    /**
     * @brief Get Tf value.
     *
     * @return Tf (term frequency) value.
     */
    unsigned int tf() const { return _tf; }

    /**
     * @brief An obsolete alias for tf().
     *
     * @return Tf (term frequency) value.
     */
    unsigned int getTf() const { return _tf; }

    /**
     * @brief Get Idf value.
     *
     * @return Idf ((inverse) document frequency) value.
     */
    unsigned int idf() const { return _idf; }

    /**
     * @brief An obsolete alias for idf().
     *
     * @return Idf ((inverse) document frequency) value.
     */
    unsigned int getIdf() const { return _idf; }

    /**
     * @brief Compute the weight from the Tf and Idf values.
     *
     * @param tfnorm Normalize Tf (divide by tfnorm).
     * @param idfnorm Normalize Idf (divide by idfnorm).
     * @param tfexp Tf exponent.
     * @param idfexp Idf exponent.
     * @return Weight based on Tf and Idf values.
     */
    double weight(unsigned int tfnorm=1, unsigned int idfnorm=1,
                  double tfexp=1.0, double idfexp=1.0) const;

    /**
     * @brief An obsolete alias for weight().
     *
     * @param tfnorm Normalize Tf (divide by tfnorm).
     * @param idfnorm Normalize Idf (divide by idfnorm).
     * @param tfexp Tf exponent.
     * @param idfexp Idf exponent.
     * @return Weight based on Tf and Idf values.
     */
    double getWeight(unsigned int tfnorm=1, unsigned int idfnorm=1,
                     double tfexp=1.0, double idfexp=1.0) const
    {
      return weight(tfnorm,idfnorm,tfexp,idfexp);
    }

  };

  // }}}

  /**
   * @brief Term vector type.
   */
  using TermVector = std::vector<VectorItem>;


private:

  // {{{ Vectorizer::RawVector

  /**
   * @class RawVector
   * @brief Class for building a raw document vector.
   *
   * The RawVector class is a subclass of Detector::Hits, so it can be
   * used directly with a Detector. The recognized terms and phrases
   * will be collected and counted (->term frequency). Idf counts are
   * obtained from the automaton the first time the term is
   * encountered.
   */
  class RawVector : public Detector::Hits {

  public:

    using ItemMap = std::map<std::string, std::pair<TfIdf, VectorItem::Hits> >;

    // {{{ Vectorizer::RawVector::iterator

    /**
     * @class iterator
     * @brief Iterator for the RawVector class.
     *
     * This class is actually a wrapper around an
     * std::map<std::string,TfIdf>::iterator.
     */
    class iterator {
      friend class RawVector;
    private:

      /**
       * @brief The real (std::map<>) iterator.
       */
      ItemMap::iterator _mi;

      /**
       * @brief Constructor.
       *
       * @param mi A real (std::map<>) iterator.
       */
      iterator(ItemMap::iterator mi) : _mi(mi) {}

    public:

      /**
       * @brief Default constructor.
       */
      iterator() : _mi() {}

      /**
       * @brief Copy constructor.
       *
       * @param it Reference to a Vectorizer::RawVector::iterator
       *        object.
       */
      iterator(const iterator &it) : _mi(it._mi) {}

      /**
       * @brief Constructor.
       *
       * Initialize the iterator to the beginning of a RawVector
       * object.
       *
       * @param rv Reference to a Vectorizer::RawVector object, the
       *           iterator will be initalized to rv.begin().
       */
      iterator(RawVector &rv) : _mi(rv._item_map.begin()) { }

      /**
       * @brief Assignment operator.
       *
       * @param it Reference to another iterator.
       * @return Reference to this iterator.
       */
      iterator& operator=(const iterator &it) { _mi=it._mi; return *this; }

      /**
       * @brief Not equals operator.
       *
       * @param it Reference to another iterator.
       * @return True if the two iterators point to different elements.
       */
      bool operator!=(const iterator &it) const { return _mi!=it._mi; }

      /**
       * @brief Prefix increment operator.
       *
       * @return Reference to the (incremented) iterator.
       */
      iterator& operator++() { ++_mi; return *this; }

      /**
       * @brief Dereference operator
       *
       * @return Reference to the actual pair the iterator refers to.
       */
      ItemMap::value_type& operator*() { return _mi.operator*(); }

      /**
       * @brief Dereference operator
       *
       * @return Pointer to the actual pair the iterator refers to.
       */
      ItemMap::value_type* operator->() { return _mi.operator->(); }
    };

    // }}}

  private:

    /**
     * @brief Flag for controlling whether or not the detector will
     * save hit position information.
     */
    bool _save_positions;

    /**
     * @brief The map holding the detected terms/phrases.
     */
    ItemMap _item_map;

  public:

    /**
     * @brief Default constructor.
     */
    RawVector(bool save_positions = false) : _save_positions(save_positions), _item_map() {}

    /**
     * @brief Destructor.
     */
    ~RawVector() {}

    /**
     * @brief Clear all data structures.
     */
    void clear() { _item_map.clear(); }

    /**
     * @brief Register a term or phrase.
     *
     * This method will be called by the detector for each term or
     * recognized.
     *
     * @param text Input document (tokenized).
     * @param from Index of first token of the phrase.
     * @param length Length of the phrase.
     * @param state Reference to the final state of the automaton
     *              after recognition of the phrase.
     */
    void add(const NGram &text,
             unsigned int from, int length,
             const FSA::State &state) override
    {
      ItemMap::iterator pos;
      std::string str = text.join(" ",from,length);
      pos=_item_map.find(str);
      if(pos==_item_map.end()){
        pos=_item_map.insert(
          ItemMap::value_type(
            str,
            std::pair<TfIdf,VectorItem::Hits>(
              TfIdf(1,state.nData()),
              VectorItem::Hits()
            )
          )
        ).first;
      }
      else {
        ++(pos->second.first);
      }
      if(_save_positions){
        pos->second.second.push_back(VectorItem::Hit(from,length));
      }
    }

    /**
     * @brief Get the size of the vector.
     *
     * @return Size of the vector (number of items).
     */
    unsigned int size() const { return _item_map.size(); }

    /**
     * @brief Get an iterator to the beginning of the vector.
     *
     * @return Iterator pointing to the first item of the vector.
     */
    iterator begin() { return iterator(_item_map.begin()); }

    /**
     * @brief Get an iterator to the end of the vector.
     *
     * @return Iterator pointing beyond the last item of the vector.
     */
    iterator end() { return iterator(_item_map.end()); }

  };

  // }}}

  const FSA&    _dictionary; /**< The dictionary. */
  Detector      _detector;   /**< The detector.   */
  unsigned int  _idf_docs;   /**< Total number of documents (for Idf calculations) */

  /**
   * @brief Retrieve total number of documents from the automaton.
   *
   * Retrieve total number of documents from the automaton. For the
   * Idf calculations to work properly, the total number of documents
   * needs to be stored in the automaton. This is done via a special
   * term, '#IDFDOCS', with a numerical meta info which equals the
   * total number of documents.
   */
  void initIdfCount()
  {
    _idf_docs=0;
    FSA::State s(_dictionary);
    if(s.start("#IDFDOCS"))
      _idf_docs = s.nData();

    if(!_idf_docs)
      ++_idf_docs;
  }

public:

  /**
   * @brief Constructor.
   *
   * Initialize the dictionary and the detector from an FSA.
   *
   * @param dict FSA
   */
  Vectorizer(const FSA& dict) :
    _dictionary(dict),
    _detector(_dictionary),
    _idf_docs(0)
  {
    initIdfCount();
  }

  /**
   * @brief Constructor.
   *
   * Initialize the dictionary and the detector from an FSA.
   *
   * @param dict FSA
   */
  Vectorizer(const FSA* dict) :
    _dictionary(*dict),
    _detector(_dictionary),
    _idf_docs(0)
  {
    initIdfCount();
  }

  /**
   * @brief Destructor.
   */
  ~Vectorizer() {}


  /**
   * @brief Vectorize a document.
   *
   * @param text Input document.
   * @param vector TermVector object to hold the document vector.
   * @param limit Limit the number of vector items.
   * @param keephits Include in the vector items the hit positions of terms.
   * @param tfexp Exponent for tf (term frequency).
   * @param idfexp Exponent for idf (inverse document frequency).
   */
  void vectorize(const NGram &text, TermVector &vector, unsigned int limit,
                 bool keephits, double tfexp = 1.0, double idfexp = 1.0) const;

  /**
   * @brief Vectorize a document.
   *
   * In this version of the call, hit positions are not kept.
   *
   * @param text Input document.
   * @param vector TermVector object to hold the document vector.
   * @param limit Limit the number of vector items (default=15).
   * @param tfexp Exponent for tf (term frequency).
   * @param idfexp Exponent for idf (inverse document frequency).
   */
  void vectorize(const NGram &text, TermVector &vector, unsigned int limit=15,
                 double tfexp = 1.0, double idfexp = 1.0) const;

};

// }}}

} // namespace fsa