summaryrefslogtreecommitdiff
path: root/dtrain/pairsampling.h
blob: 9b88a4be6f23e53624841999647cc7a6b054e9eb (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
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
#ifndef _DTRAIN_PAIRSAMPLING_H_
#define _DTRAIN_PAIRSAMPLING_H_

namespace dtrain
{


inline void
all_pairs(vector<ScoredHyp>* s, vector<pair<ScoredHyp,ScoredHyp> >& training)
{
  for (unsigned i = 0; i < s->size()-1; i++) {
    for (unsigned j = i+1; j < s->size(); j++) {
      pair<ScoredHyp,ScoredHyp> p;
      p.first = (*s)[i];
      p.second = (*s)[j];
      training.push_back(p);
    }
  }
}

inline void
rand_pairs_5050(vector<ScoredHyp>* s, vector<pair<ScoredHyp,ScoredHyp> >& training,
                  MT19937* prng)
{
  for (unsigned i = 0; i < s->size()-1; i++) {
    for (unsigned j = i+1; j < s->size(); j++) {
      if (prng->next() < .5) {
        pair<ScoredHyp,ScoredHyp> p;
        p.first = (*s)[i];
        p.second = (*s)[j];
        training.push_back(p);
      }
    }
  }
}

bool
_multpart_cmp_hyp_by_score(ScoredHyp a, ScoredHyp b)
{
  return a.score < b.score;
}
inline void
multpart108010(vector<ScoredHyp>* s, vector<pair<ScoredHyp,ScoredHyp> >& training)
{
  sort(s->begin(), s->end(), _multpart_cmp_hyp_by_score);
  pair<ScoredHyp,ScoredHyp>  p;
  unsigned sz = s->size();
  unsigned slice = 10;
  unsigned sep = sz%slice;
  if (sep == 0) sep = sz/slice;
  for (unsigned i = 0; i < sep; i++) {
    for(unsigned j = sep; j < sz; j++) {
      p.first = (*s)[i];
      p.second = (*s)[j];
      if(p.first.rank < p.second.rank) training.push_back(p);
    }
  }
  for (unsigned i = sep; i < sz-sep; i++) {
    for (unsigned j = sz-sep; j < sz; j++) {
      p.first = (*s)[i];
      p.second = (*s)[j];
      if(p.first.rank < p.second.rank) training.push_back(p);
    }
  }
}


inline bool
_PRO_accept_pair(pair<ScoredHyp,ScoredHyp> &p)
{
  if (fabs(p.first.score - p.second.score) < 0.05) return false;
  return true;
}
bool
_PRO_cmp_pair_by_diff(pair<ScoredHyp,ScoredHyp> a, pair<ScoredHyp,ScoredHyp> b)
{
  // descending order
  return (fabs(a.first.score - a.second.score)) > (fabs(b.first.score - b.second.score));
}
inline void
PROsampling(vector<ScoredHyp>* s, vector<pair<ScoredHyp,ScoredHyp> >& training) // ugly
{
  unsigned max_count = 5000, count = 0;
  bool b = false;
  for (unsigned i = 0; i < s->size()-1; i++) {
    for (unsigned j = i+1; j < s->size(); j++) {
      pair<ScoredHyp,ScoredHyp> p;
      p.first = (*s)[i];
      p.second = (*s)[j];
      if (_PRO_accept_pair(p)) {
        training.push_back(p);
        count++;
        if (count == max_count) {
          b = true;
          break;
        }
      }
    }
    if (b) break;
  }
  sort(training.begin(), training.end(), _PRO_cmp_pair_by_diff);
  if (training.size() > 50)
    training.erase(training.begin()+50, training.end()); 
  return;
}

inline void
all_pairs_discard(vector<ScoredHyp>* s, vector<pair<ScoredHyp,ScoredHyp> >& training)
{
  for (unsigned i = 0; i < s->size()-1; i++) {
    for (unsigned j = i+1; j < s->size(); j++) {
      pair<ScoredHyp,ScoredHyp> p;
      p.first = (*s)[i];
      p.second = (*s)[j];
      if(_PRO_accept_pair(p))
        training.push_back(p);
    }
  }
}


} // namespace

#endif