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
|
// $Id$
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
#pragma once
#include <sstream>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <string>
#include "TypeDef.h"
#include "Util.h"
#include "hash.h"
class FactorCollection;
/** Represents a factor (word, POS, etc) on the E or F side
*
* A Factor object is a tuple of direction (Input or Output,
* corresponding to French or English), a type (surface form,
* POS, stem, etc), and the value of the factor.
*
* @TODO I find this design problematic- essentially, a factor should
* just be a value type and the factor type and "direction"
* should be the keys in a larger identification system that
* find instances of specific factors.
*
*/
class Factor
{
friend std::ostream& operator<<(std::ostream&, const Factor&);
// only these classes are allowed to instantiate this class
friend class FactorCollection;
protected:
static size_t s_id;
FactorDirection m_direction;
FactorType m_factorType;
const std::string *m_ptrString;
const size_t m_id;
Factor(FactorDirection direction, FactorType factorType, const std::string *factorString);
public:
inline size_t hash() const
{
size_t h=quick_hash((const char*)&m_direction, sizeof(FactorDirection), 0xc7e7f2fd);
h=quick_hash((const char*)&m_factorType, sizeof(FactorType), h);
h=quick_hash((const char*)&m_ptrString, sizeof(const std::string *), h);
return h;
}
inline FactorDirection GetFactorDirection() const
{
return m_direction;
}
inline FactorType GetFactorType() const
{
return m_factorType;
}
inline const std::string &GetString() const
{
return *m_ptrString;
}
inline size_t GetId() const
{
return m_id;
}
// do it properly. needed for insert & finding of words in dictionary
inline int Compare(const Factor &compare) const
{
if (m_ptrString < compare.m_ptrString)
return -1;
if (m_ptrString > compare.m_ptrString)
return 1;
if (m_direction < compare.m_direction)
return -1;
if (m_direction > compare.m_direction)
return 1;
if (m_factorType < compare.m_factorType)
return -1;
if (m_factorType > compare.m_factorType)
return 1;
return 0;
}
inline bool operator<(const Factor &compare) const
{
return Compare(compare) < 0;
}
inline bool operator==(const Factor &compare) const
{
return Compare(compare) == 0;
}
TO_STRING;
};
|