File: mmAligner.cpp

package info (click to toggle)
m2m-aligner 1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,348 kB
  • sloc: cpp: 4,709; sh: 1,144; xml: 944; makefile: 292
file content (299 lines) | stat: -rw-r--r-- 10,774 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/********************************************************************
*
* file: mmAligner.cpp
*
* Copyright (c) 2007, Sittichai Jiampojamarn
* All rights reserved.
* 
* See the file COPYING in the top directory of this distribution
* for more information.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Author: Sittichai Jiampojamarn
* Email: sj@cs.ualberta.ca
*
* Many-to-many alignment : 
* History : 
*
* Sept 9, 2009 : implemented n-best generation model to output
* n-best alignments. It's based on a variation of n-best viterbi algorithm.
*   : added an initialization feature, so we can change 
*   : EM starting point and also inject lingustic knowledge.
*
* Mar 2, 2009 : re-formatting input/output for 
*  NEWS: Shared Task on Transliteration
*
* Dec 10, 2008 : re-implemented the algorithm based on 
*	Sittichai Jiampojamarn, Grzegorz Kondrak and Tarek Sherif. 
*  "Applying Many-to-Many Alignments and Hidden Markov Models 
*	to Letter-to-Phoneme Conversion". Proceedings of the Annual 
*	Conference of the North American Chapter of the Association 
*	for Computational Linguistics (NAACL-HLT 2007), Rochester, 
*	NY, April 2007, pp.372-379
*
* Credits : Tarek Sherif originally proposed this algorithm 
*  based on the Ristad and Yianilos (1997) stochastic transducer
*  as a part of his Master thesis graduated in 2007, U. of Alberta.
*
* Citation : If you use the code for research or commercial purposes,
*  please acknowledge its use with a citation:
*
*  @InProceedings{jiampojamarn2007:main,
*  author    = {Jiampojamarn, Sittichai  and  Kondrak, Grzegorz  and  Sherif, Tarek},
*  title     = {Applying Many-to-Many Alignments and Hidden Markov Models to Letter-to-Phoneme Conversion},
*  booktitle = {Human Language Technologies 2007: The Conference of the North American Chapter of the Association for Computational Linguistics; Proceedings of the Main Conference},
*  month     = {April},
*  year      = {2007},
*  address   = {Rochester, New York},
*  publisher = {Association for Computational Linguistics},
*  pages     = {372--379},
*  url       = {http://www.aclweb.org/anthology/N/N07/N07-1047}
*  }
*
**********************************************************************/

#include <iostream>
#include <list>
#include <ctime>
#include <cstdio>

#include <tclap/CmdLine.h>

#include "param.h"
#include "mmEM.h"
#include "util.h"

using namespace std;
using namespace TCLAP;

namespace TCLAP
{
        /**
         * long doubles have value-like semantics.
         */
        template<>
        struct ArgTraits<long double> {
		typedef ValueLike ValueCategory;
        };
}

inline void printTimeStamp(time_t startT)
{
	time_t stopT, diffT;

	time(&stopT);
	diffT = difftime(stopT, startT);
	cout << endl << "Time stamp: " << ctime(&stopT);
	cout << diffT << " seconds after started\n\n";
}

int main(int argc, char** argv)
{

	try{
		// program options //
		CmdLine cmd("Command description message ", ' ' , "1.0");

		ValueArg<string> inputFilename("i", "inputFile", "Input filename" , true, "", "string", cmd);
		ValueArg<string> outputFilename("o", "outputFile", "Output filename", false, "", "string", cmd);

		ValueArg<string> alignerOut("","alignerOut", "Aligner model output filename", false, "", "string", cmd);
		ValueArg<string> alignerIn("","alignerIn", "Aligner model input filename", false, "", "string", cmd);

		ValueArg<unsigned int> maxX("", "maxX", "Maximum length of substring x (default 2)", false, 2, "int", cmd);
		ValueArg<unsigned int> maxY("", "maxY", "Maximum length of substring y (default 2)", false, 2, "int", cmd);

		SwitchArg delX("", "delX", "Allow deletion of substring x (default false)", cmd, false);
		SwitchArg delY("", "delY", "Allow deletion of substring y (default false)", cmd, false);

		SwitchArg eqMap("", "eqMap", "Allow mapping of |x| == |y| > 1 (default false)", cmd, false);

		vector<string> allowFn;
		allowFn.push_back("conXY");
		allowFn.push_back("conYX");
		allowFn.push_back("joint");
		ValuesConstraint<string> allowFnValues(allowFn);
		ValueArg<string> maxFn("", "maxFn", "Maximization function [conXY, conYX, joint] (default conYX)", false, "conYX", &allowFnValues, cmd);

		ValueArg<double> cutOff("", "cutoff", "Training threshold (default 0.01)", false, 0.01, "double", cmd);

		SwitchArg printScore("", "pScore", "Report score of each alignment (default false)", cmd, false);

		ValueArg<string> prefixProcess("","pProcess","Specify prefix output files", false, "", "string", cmd);

		ValueArg<string> nullChar("","nullChar", "Null character used (default _)", false, "_", "string", cmd);
		ValueArg<string> sepChar("","sepChar","Separated character used (default |)", false, "|", "string", cmd);
		ValueArg<string> sepInChar("","sepInChar","Separated in-character used (default :)", false, ":","string", cmd);

		vector<string> allowFormat;
		allowFormat.push_back("l2p");
		allowFormat.push_back("news");
		ValuesConstraint<string> allowFormats(allowFormat);
		ValueArg<string> inFormat("","inFormat","Input file format [l2p, news] (default news)", false, "news", &allowFormats, cmd);

		ValueArg<unsigned int> nBest("","nBest","Generate n-best alignments (default n=1)", false, 1, "int", cmd);

		ValueArg<string> initFile("","init","Initial mapping (model) filename (default null)", false, "", "string", cmd);

		ValueArg<long double> initProbCut("","initProb","Cut-off sum prior probability (default 0.5)", false, 0.5, "long double", cmd);

		SwitchArg errorInFile("","errorInFile","Keep unaligned item in the output file (default false)", cmd, false);

		SwitchArg limitPair("","limit","Limit the alignment pair to used only from the initFile only (default false)", cmd, false);

		cmd.parse(argc, argv);

		// define program parameters //
		param myParam;
		myParam.inputFilename = inputFilename.getValue();
		myParam.outputFilename = outputFilename.getValue();

		myParam.alignerIn = alignerIn.getValue();
		myParam.alignerOut = alignerOut.getValue();

		myParam.maxX = maxX.getValue();
		myParam.maxY = maxY.getValue();
		myParam.delX = delX.getValue();
		myParam.delY = delY.getValue();
		myParam.eqMap = eqMap.getValue();

		myParam.maxFn = maxFn.getValue();
		myParam.cutOff = cutOff.getValue();

		myParam.printScore = printScore.getValue();
		myParam.prefixProcess = prefixProcess.getValue();

		myParam.nullChar = nullChar.getValue();
		myParam.sepChar = sepChar.getValue();
		myParam.sepInChar = sepInChar.getValue();

		myParam.inFormat = inFormat.getValue();
		myParam.nBest = nBest.getValue();
		
		myParam.initFile = initFile.getValue();

		myParam.initProbCut = initProbCut.getValue();

		myParam.errorInFile = errorInFile.getValue();

		myParam.limitPair = limitPair.getValue();

		// print program command to stdout//

		cout << "Command line: " << endl;

		for (int i = 0; i < argc; i++)
		{
			cout << argv[i] << endl;
		}
		cout << endl;

		cout << inputFilename.getDescription() << " : " << inputFilename.getValue() << endl;
		cout << outputFilename.getDescription() << " : " << outputFilename.getValue() << endl;
		cout << alignerIn.getDescription() << " : " << alignerIn.getValue() << endl;
		cout << alignerOut.getDescription() << " : " << alignerOut.getValue() << endl;
		cout << maxX.getDescription() << " : " << maxX.getValue() << endl;
		cout << maxY.getDescription() << " : " << maxY.getValue() << endl;
		cout << delX.getDescription() << " : " << delX.getValue() << endl;
		cout << delY.getDescription() << " : " << delY.getValue() << endl;
		cout << eqMap.getDescription() << " : " << eqMap.getValue() << endl;
		cout << maxFn.getDescription() << " : " << maxFn.getValue() << endl;
		cout << cutOff.getDescription() << " : " << cutOff.getValue() << endl;
		cout << printScore.getDescription() << " : " << printScore.getValue() << endl;
		cout << prefixProcess.getDescription() << " : " << prefixProcess.getValue() << endl;
		cout << nullChar.getDescription() << " : " << nullChar.getValue() << endl;
		cout << sepChar.getDescription() << " : " << sepChar.getValue() << endl;
		cout << sepInChar.getDescription() << " : " << sepInChar.getValue() << endl;
		cout << inFormat.getDescription() << " : " << inFormat.getValue() << endl;
		cout << nBest.getDescription() << " : " << nBest.getValue() << endl;
		cout << initFile.getDescription() << " : " << initFile.getValue() << endl;
		cout << initProbCut.getDescription() << " : " << initProbCut.getValue() << endl;
		cout << errorInFile.getDescription() << " : " << errorInFile.getValue() << endl;
		cout << limitPair.getDescription() << " : " << limitPair.getValue() << endl;

		if (myParam.prefixProcess == "")
		{
			myParam.prefixProcess = "m-mAlign." + stringify(myParam.maxX) + "-" + stringify(myParam.maxY);
			if (myParam.delX)
			{
				myParam.prefixProcess += ".delX";
			}
			if (myParam.delY)
			{
				myParam.prefixProcess += ".delY";
			}

			myParam.prefixProcess += "." + stringify(myParam.nBest) + "-best";
			myParam.prefixProcess += "." + myParam.maxFn;

			if (myParam.printScore)
			{
				myParam.prefixProcess += ".pScore";
			}

			if (myParam.initFile != "")
			{
				myParam.prefixProcess += ".withInit";
			}

			if (myParam.errorInFile)
			{
				myParam.prefixProcess += ".errorInFile";
			}

			if (myParam.limitPair)
			{
				myParam.prefixProcess += ".limitPair";
			}
		}

		cout << endl;
		time(&myParam.startT);
		cout << "Started at: " << ctime(&myParam.startT) << endl << endl;
		
		mmEM myAligner;

		if (myParam.alignerIn != "")
		{
			// read aligner model from file
			myAligner.readAlignerFromFile(myParam);
		}
		else
		{
			// training aligner //
			myAligner.training(myParam);
			
			// writing aligner model to file //
			if (myParam.alignerOut == "")
			{
				myParam.alignerOut = myParam.inputFilename + "." + myParam.prefixProcess + ".align.model";
			}
			myAligner.writeAlingerToFile(myParam);

			// read model from file //
			myParam.alignerIn = myParam.alignerOut;
			myAligner.readAlignerFromFile(myParam);
		}
		printTimeStamp(myParam.startT);

		// create alignments //
		if (myParam.outputFilename == "")
		{
			myParam.outputFilename = myParam.inputFilename + "." + myParam.prefixProcess + ".align";
		}
		myAligner.createAlignments(myParam);
		printTimeStamp(myParam.startT);		
	}
	catch (TCLAP::ArgException &e)
	{
		cerr << "error: " << e.error() <<  " for arg " << e.argId() << endl;
	}
	return 0;
}