Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Factor.h « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8738b59fbcc90304459879fef75fc49974817d8 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// $Id: Factor.h 2526 2009-08-23 22:12:54Z hieuhoang1972 $

/***********************************************************************
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"

namespace Moses
{

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:

	//FactorDirection		m_direction;
	//FactorType				m_factorType;
	const std::string	m_ptrString;
	const size_t			m_id;

	//! protected constructor. only friend class, FactorCollection, is allowed to create Factor objects
	Factor(FactorDirection direction, FactorType factorType, const std::string &factorString, size_t id);
	//! no id set. do not used to create new factors, only used for seeing if factor exists
	Factor(FactorDirection direction, FactorType factorType, const std::string &factorString);
	
public:
	//! returns whether this factor is part of the source ('Input') or target ('Output') language
	//inline FactorDirection GetFactorDirection() const
	//{
	//	return m_direction;
	//}
	//! index, FactorType. For example, 0=surface, 1=POS. The actual mapping is user defined
	//inline FactorType GetFactorType() const
	//{
	//	return m_factorType;
	//}
	//! original string representation of the factor
	inline const std::string &GetString() const
	{
		return m_ptrString;
	}
	//! contiguous ID
	inline size_t GetId() const
	{
		return m_id;
	}

	/*
	//! Alternative comparison between factors. Not yet used
	inline unsigned int GetHash() const
	{
		unsigned int 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;
	}
	*/
	
	/** transitive comparison between 2 factors.
	*	-1 = less than
	*	+1 = more than
	*	0	= same
	*	Used by operator< & operator==, as well as other classes
	*/
	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;
	}
	//! transitive comparison used for adding objects into FactorCollection
	inline bool operator<(const Factor &compare) const
	{ 
		return Compare(compare) < 0;
	}

	// for hash set
	inline bool operator==(const Factor &compare) const
	{ 
		return Compare(compare) == 0;
	}
	


	TO_STRING();

};


}