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

Phrase.h « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2b3cda833d438aa385e658c911c5ad9201d6789 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// $Id: Phrase.h 2463 2009-08-05 21:57:21Z hieuhoang1972 $
// vim:tabstop=2

/***********************************************************************
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 <iostream>
#include <vector>
#include <list>
#include <string>
#include "Word.h"
#include "WordsBitmap.h"
#include "TypeDef.h"
#include "Util.h"
#include "mempool.h"

namespace Moses
{

class Phrase
{
	friend std::ostream& operator<<(std::ostream&, const Phrase&);
private:

	FactorDirection				m_direction;  /** Reusing Direction enum to really mean which language
																					Input = Source, Output = Target. 
																					Not really used, but nice to know for debugging purposes
																			*/
	std::vector<Word>			m_words;
	size_t m_arity;

public:
	/** No longer does anything as not using mem pool for Phrase class anymore */
	static void InitializeMemPool();
	static void FinalizeMemPool();

	/** copy constructor */
	Phrase(const Phrase &copy);
	Phrase& operator=(const Phrase&);

	/** create empty phrase 
	* \param direction = language (Input = Source, Output = Target)
	*/
	Phrase(FactorDirection direction, size_t reserveSize = ARRAY_SIZE_INCR);
	/** create phrase from vectors of words	*/
	Phrase(FactorDirection direction, const std::vector< const Word* > &mergeWords);

	/** destructor */
	virtual ~Phrase();

	void Parse(std::vector< std::vector<std::string> > &output
														, const std::string &phraseString
														, const std::string& factorDelimiter);

	static std::vector< std::vector<std::string> > Parse(
															const std::string &phraseString
															, const std::string& factorDelimiter);

	/** parse a string from phrase table or sentence input and create a 2D vector of strings
	*	\param phraseString string to parse
	*	\param factorOrder factors in the parse string. This argument is not fully used, only as a check to make ensure
	*											number of factors is what was promised
	*	\param factorDelimiter what char to use to separate factor strings from each other. Usually use '|'. Can be multi-char
	*/
	static std::vector< std::vector<std::string> > Parse(
																const std::string &phraseString
																, const std::vector<FactorType> &factorOrder
																, const std::string& factorDelimiter);

	// speeded up version of above
	static void Parse(std::vector< std::vector<std::string>* > &output
									, const std::string &phraseString
									, const std::vector<FactorType> &factorOrder
									, const std::string& factorDelimiter);

	/** Fills phrase with words from 2D string vector
		* \param factorOrder factor types of each element in 2D string vector
		* \param phraseVector 2D string vector
	*/
	void CreateFromString(const std::vector<FactorType> &factorOrder
              , const std::vector< std::vector<std::string> > &phraseVector);
	/** Fills phrase with words from format string, typically from phrase table or sentence input
		* \param factorOrder factor types of each element in 2D string vector
		* \param phraseString formatted input string to parse
		*	\param factorDelimiter delimiter, as used by Parse()
	*/

	void CreateFromString(const std::vector<FactorType> &factorOrder
              , const std::vector< std::vector<std::string>* > &phraseVector);

	void CreateFromString(const std::vector<FactorType> &factorOrder
											, const std::string &phraseString
											, const std::string &factorDelimiter);

	void CreateFromStringNewFormat(FactorDirection direction
																 , const std::vector<FactorType> &factorOrder
																 , const std::string &phraseString
																 , const std::string &factorDelimiter
																 , Word &lhs);
	
	/**	copy factors from the other phrase to this phrase. 
		IsCompatible() must be run beforehand to ensure incompatible factors aren't overwritten
	*/
	void MergeFactors(const Phrase &copy);
	//! copy a single factor (specified by factorType)
	void MergeFactors(const Phrase &copy, FactorType factorType);
	//! copy all factors specified in factorVec and none others
	void MergeFactors(const Phrase &copy, const std::vector<FactorType>& factorVec);

	/** compare 2 phrases to ensure no factors are lost if the phrases are merged
	*	must run IsCompatible() to ensure incompatible factors aren't being overwritten
	*/
	bool IsCompatible(const Phrase &inputPhrase) const;
	bool IsCompatible(const Phrase &inputPhrase, FactorType factorType) const;
	bool IsCompatible(const Phrase &inputPhrase, const std::vector<FactorType>& factorVec) const;
	
	//! really means what language. Input = Source, Output = Target
	inline FactorDirection GetDirection() const
	{
		return m_direction;
	}

	//! number of words
	inline size_t GetSize() const
	{
		return m_words.size();
	}

	//! word at a particular position
	inline const Word &GetWord(size_t pos) const
	{
		return m_words[pos];
	}
	inline Word &GetWord(size_t pos)
	{
		return m_words[pos];
	}
	void SetWord(size_t pos, const Word &word)
	{
		m_words[pos] = word;
	}
	
	//! particular factor at a particular position
	inline const Factor *GetFactor(size_t pos, FactorType factorType) const
	{
		const Word &ptr = m_words[pos];
		return ptr[factorType];
	}

	size_t GetNumTerminals() const;

	inline void SetFactor(size_t pos, FactorType factorType, const Factor *factor)
	{
		Word &ptr = m_words[pos];
		ptr[factorType] = factor;
	}

	//! whether the 2D vector is a substring of this phrase
	bool Contains(const std::vector< std::vector<std::string> > &subPhraseVector
							, const std::vector<FactorType> &inputFactor) const;

	//! create an empty word at the end of the phrase
	Word &AddWord();
	//! create copy of input word at the end of the phrase
	void AddWord(const Word &newWord)
	{
    AddWord() = newWord;
  }

	void PrependWord(const Word &newWord);

	//! create new phrase class that is a substring of this phrase
	Phrase GetSubString(const WordsRange &wordsRange) const;
	
	//! return a string rep of the phrase. Each factor is separated by the factor delimiter as specified in StaticData class
	std::string GetStringRep(const std::vector<FactorType> factorsToPrint) const; 
  
	TO_STRING();

	int Compare(const Phrase &other) const;

	/** transitive comparison between 2 phrases
	*		used to insert & find phrase in dictionary
	*/
	bool operator< (const Phrase &compare) const
	{
		return Compare(compare) < 0;
	}

	/** appends a phrase at the end of current phrase **/
	void Append(const Phrase &endPhrase);

	/** appends a phrase at the end of current phrase **/
	void Clear()
	{
		m_words.clear();
    m_words.resize(0);
    m_words.reserve(0);
	}
	void RemoveWord(size_t pos)
	{
		assert(pos < m_words.size());
		m_words.erase(m_words.begin() + pos);
	}

	size_t GetArity() const
	{ return m_arity; }

};


}