/* * $Id: common.h,v 1.3 2004/01/21 13:43:57 tsuruoka Exp $ */ #ifndef __COMMON_H__ #define __COMMON_H__ #include #include #include "atmstr.h" const int max_sentence_length = 26; //const int max_sentence_length = 10; const static double INF = 999999; struct Token { AString pos; AString str; Token(const AString p, const AString s) : pos(p), str(s) {} }; struct Sentence { list lt; }; struct Rule { AString lhs; list rhs; bool operator==(const Rule & r) const { if (lhs != r.lhs) return false; return rhs == r.rhs; } }; struct RuleCount { Rule rule; int numerator; int denominator; RuleCount(const Rule & r, int n, int d) : rule(r), numerator(n), denominator(d) { assert(r.rhs.size() > 0); } double Probability() const { return (double)numerator/denominator; // return (double)(numerator + 1)/ (denominator + 2); } }; struct BinaryRule { AString lhs; AString rhs1, rhs2; double logp; bool is_temporary; // ex.) X -> NP NP // they don't appear in the rightmost BinaryRule(AString l = -1, AString r1 = -1, AString r2 = -1, double lp = 0, bool ist = false) : lhs(l), rhs1(r1), rhs2(r2), logp(lp), is_temporary(ist) {} bool IsUnary() const { return rhs2 == -1; } bool operator<(const BinaryRule & x) { if (lhs != x.lhs) return lhs < x.lhs; if (rhs1 != x.rhs1) return rhs1 < x.rhs1; if (rhs2 != x.rhs2) return rhs2 < x.rhs2; return logp < x.logp; } bool operator==(const BinaryRule & x) { if (lhs != x.lhs) return false; if (rhs1 != x.rhs1) return false; if (rhs2 != x.rhs2) return false; if (logp != x.logp) return false; return true; } }; struct CkyElement { // AString nt; //nonterminal symbol BinaryRule br; double logp; int i[2]; // from where this nonterminal is constructed int j[2]; CkyElement(const BinaryRule & br_, double lp, int i0 = -1, int j0 = -1, int i1 = -1, int j1 = -1) { br = br_; logp = lp; i[0] = i0; j[0] = j0; i[1] = i1; j[1] = j1; } CkyElement() {} bool operator<(const CkyElement & x) { return br.lhs < x.br.lhs; } }; // rule: state -> x y // (x, y) => state typedef hash_multimap< pair, BinaryRule > map_type; typedef hash_multimap< AString, BinaryRule > map_type2; typedef hash_map cell_type; struct hash > { size_t operator()(const pair & p) const { assert(p.first >= 0 && p.first < 65536); assert(p.second >= -1 && p.second < 65536); return (unsigned int)( (p.first << 16) ^ p.second); } }; struct hash > { size_t operator()(const pair & p) const { assert(int(p.first) >= 0 && int(p.first) < 65536); assert(p.second >= -1 && p.second < 65536); return (unsigned int)( (int(p.first) << 16) ^ p.second); } }; struct hash > { size_t operator()(const pair & p) const { assert(int(p.first) >= 0 && int(p.first) < 65536); assert(int(p.second) >= -1 && int(p.second) < 65536); return (unsigned int)( (int(p.first) << 16) ^ int(p.second)); } }; #endif /* * $Log: common.h,v $ * Revision 1.3 2004/01/21 13:43:57 tsuruoka * using zlib for estimation_sx * * Revision 1.2 2004/01/21 12:30:38 tsuruoka * stringbag -> AString * * Revision 1.1 2004/01/21 09:00:02 tsuruoka * source separation * */