summaryrefslogtreecommitdiff
path: root/extools/striped_grammar.h
blob: cdf529d67a722216c971233b87564ecc514da975 (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
#ifndef _STRIPED_GRAMMAR_H_
#define _STRIPED_GRAMMAR_H_

#include <iostream>
#include <boost/functional/hash.hpp>
#include <vector>
#include <tr1/unordered_map>
#include "sparse_vector.h"
#include "wordid.h"
#include "tdict.h"

// represents statistics / information about a rule pair
struct RuleStatistics {
  SparseVector<float> counts;
  std::vector<std::pair<short,short> > aligns;
  RuleStatistics() {}
  RuleStatistics(int name, float val, const std::vector<std::pair<short,short> >& al) :
      aligns(al) {
    counts.set_value(name, val);
  }
  void ParseRuleStatistics(const char* buf, int start, int end);
  RuleStatistics& operator+=(const RuleStatistics& rhs) {
    counts += rhs.counts;
    return *this;
  }
};
std::ostream& operator<<(std::ostream& os, const RuleStatistics& s);

inline void WriteNamed(const std::vector<WordID>& v, std::ostream* os) {
  bool first = true;
  for (int i = 0; i < v.size(); ++i) {
    if (first) { first = false; } else { (*os) << ' '; }
    if (v[i] < 0) { (*os) << '[' << TD::Convert(-v[i]) << ']'; }
    else (*os) << TD::Convert(v[i]);
  }
}

inline void WriteAnonymous(const std::vector<WordID>& v, std::ostream* os) {
  bool first = true;
  for (int i = 0; i < v.size(); ++i) {
    if (first) { first = false; } else { (*os) << ' '; }
    if (v[i] <= 0) { (*os) << '[' << (1-v[i]) << ']'; }
    else (*os) << TD::Convert(v[i]);
  }
}

typedef std::tr1::unordered_map<std::vector<WordID>, RuleStatistics, boost::hash<std::vector<WordID> > > ID2RuleStatistics;

struct StripedGrammarLexer {
  typedef void (*GrammarCallback)(WordID lhs, const std::vector<WordID>& src_rhs, const ID2RuleStatistics& rules, void *extra);
  static void ReadStripedGrammar(std::istream* in, GrammarCallback func, void* extra);
};

#endif