ZGen  0.2.0
a linearization system for natural language.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
action.h
Go to the documentation of this file.
1 #ifndef __ZGEN_SHIFTREDUCE_TYPES_ACTION_H__
2 #define __ZGEN_SHIFTREDUCE_TYPES_ACTION_H__
3 
4 #include <iostream>
5 #include "engine/engine.h"
6 #include "types/instance.h"
7 #include "shiftreduce/settings.h"
8 #include <boost/tuple/tuple.hpp>
9 #include <boost/functional/hash.hpp>
10 #include <boost/archive/text_iarchive.hpp>
11 #include <boost/archive/text_oarchive.hpp>
12 #include <boost/serialization/singleton.hpp>
13 
14 namespace ZGen {
15 
16 namespace ShiftReduce {
17 
18 class Action {
19 public:
20  Action()
21  : prefix(0), label(0), word(0), index(-1) {
22  seed = 0;
23  }
24 
25  Action(int _prefix, int _label, int _word)
26  : prefix(_prefix), label(_label), word(_word), index(-1) {
27 
28  seed = 0;
29  boost::hash_combine(seed, _prefix);
30  boost::hash_combine(seed, _label);
31  boost::hash_combine(seed, _word);
32  }
33 
34  Action(int _prefix, int _label, int _word, int _index)
35  : prefix(_prefix), label(_label), word(_word), index(_index) {
36 
37  seed = 0;
38  // Only hash the first three element since index is only
39  // a auxilary element.
40  boost::hash_combine(seed, _prefix);
41  boost::hash_combine(seed, _label);
42  boost::hash_combine(seed, _word);
43  }
44 
45  inline std::size_t hash() const {
46  return seed;
47  }
48 
49  Action & operator = (const Action & a) {
50  seed = a.seed;
51  prefix = a.prefix;
52  label = a.label;
53  word = a.word;
54  index = a.index;
55 
56  return (*this);
57  }
58 
59  bool operator == (const Action & a) const {
60  return (a.seed == seed &&
61  a.prefix == prefix &&
62  a.label == label &&
63  a.word == word);
64  }
65 
66  bool operator != (const Action & a) const {
67  return !((*this) == a);
68  }
69 
71 
72  template<class Archive>
73  void serialize(Archive & ar, const unsigned int /* file_version */) {
74  ar & prefix & label & word & index & seed;
75  }
76 
77  friend std::ostream & operator << (std::ostream& os, const Action& act) {
78  namespace eg = Engine;
79  os << "(" << eg::ActionEngine::get_const_instance().decode(act.prefix)
80  << ", ";
81 
82  if (act.prefix == eg::ActionAlphabet::SH) {
83  os << eg::PostagEngine::get_const_instance().decode(act.label)
84  << ", "
85  << eg::WordEngine::get_const_instance().decode(act.word)
86  << ", [" << act.index << "]"
87  << ")";
88  } else {
89  os << eg::DeprelEngine::get_const_instance().decode(act.label)
90  << ")";
91  }
92  return os;
93  }
94 
95  friend std::size_t hash_value(const Action& a) {
96  return a.seed;
97  }
98 
99  int prefix;
100  // The action prefix (SHIFT, LEFT-ARC, RIGHT-ARC)
101 
102  int label;
103  // The relation field. It represent PoSTag (on SHIFT) and
104  // dependency relation (on LEFT-ARC and RIGHT-ARC)
105 
106  int word;
107  // The word inded.
108 
109  int index;
110  // extra field for auxilary in the SHIFT action.
111 
112  std::size_t seed;
113 };
114 
115 typedef Action action_t;
116 
117 }
118 
119 }
120 
121 #endif // end for __SR_ACTION_HPP__
Action(int _prefix, int _label, int _word)
Definition: action.h:25
int prefix
Definition: action.h:99
std::size_t seed
Definition: action.h:112
Action()
Definition: action.h:20
Definition: action.h:18
Action(int _prefix, int _label, int _word, int _index)
Definition: action.h:34
int index
Definition: action.h:109
bool operator==(const Action &a) const
Definition: action.h:59
friend class boost::serialization::access
Definition: action.h:70
std::size_t hash() const
Definition: action.h:45
friend std::ostream & operator<<(std::ostream &os, const Action &act)
Definition: action.h:77
friend std::size_t hash_value(const Action &a)
Definition: action.h:95
void serialize(Archive &ar, const unsigned int)
Definition: action.h:73
Definition: action_alphabet.h:32
bool operator!=(const Action &a) const
Definition: action.h:66
int label
Definition: action.h:102
int word
Definition: action.h:106
Action & operator=(const Action &a)
Definition: action.h:49
Action action_t
Definition: action.h:115