summaryrefslogtreecommitdiff
path: root/extractor/matching_comparator.cc
blob: 03db95c0958a2427f6a330fe467d0801ec877a46 (plain)
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
#include "matching_comparator.h"

#include "matching.h"
#include "vocabulary.h"

MatchingComparator::MatchingComparator(int min_gap_size, int max_rule_span) :
    min_gap_size(min_gap_size), max_rule_span(max_rule_span) {}

int MatchingComparator::Compare(const Matching& left,
                                const Matching& right,
                                int last_chunk_len,
                                bool offset) const {
  if (left.sentence_id != right.sentence_id) {
    return left.sentence_id < right.sentence_id ? -1 : 1;
  }

  if (left.positions.size() == 1 && right.positions.size() == 1) {
    // The prefix and the suffix must be separated by a non-terminal, otherwise
    // we would be doing a suffix array lookup.
    if (right.positions[0] - left.positions[0] <= min_gap_size) {
      return 1;
    }
  } else if (offset) {
    for (size_t i = 1; i < left.positions.size(); ++i) {
      if (left.positions[i] != right.positions[i - 1]) {
        return left.positions[i] < right.positions[i - 1] ? -1 : 1;
      }
    }
  } else {
    if (left.positions[0] + 1 != right.positions[0]) {
      return left.positions[0] + 1 < right.positions[0] ? -1 : 1;
    }
    for (size_t i = 1; i < left.positions.size(); ++i) {
      if (left.positions[i] != right.positions[i]) {
        return left.positions[i] < right.positions[i] ? -1 : 1;
      }
    }
  }

  if (right.positions.back() + last_chunk_len - left.positions.front() >
      max_rule_span) {
    return -1;
  }

  return 0;
}