-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemaccconf.pm
419 lines (356 loc) · 13.7 KB
/
emaccconf.pm
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# EMACC configuration file. Change this before running!
#
# (C) ICIA 2011, Radu ION.
#
# ver 1.0, 30.09.2011, Radu ION: portable for Windows and Linux.
# ver 1.1, 10.10.2011, Radu ION: added EMACCMODE.
package emaccconf;
use strict;
use warnings;
use File::Spec;
use File::Path;
use Sys::Hostname;
sub checkDir( $$ );
sub checkFile( $$ );
sub checkClusterFile( $$ );
sub checkInitMode( $$ );
sub checkPLexType( $$ );
sub checkEmaccMode( $$ );
sub checkProbType( $$ );
sub checkInt( $$ );
sub checkProb( $$ );
sub checkReal( $$ );
sub checkBool( $$ );
sub checkLang( $$ );
sub new;
sub addValue( $$$$ );
sub genClusterFile();
sub portableCopyFileToDir( $$ );
sub portableRemoveFile( $ );
sub portableRemoveFileFromDir( $$ );
sub portableRemoveAllFilesFromDir( $ );
sub portableForkAndDetach( $ );
##################
#CONFIG FILE######
##################
#Only change values between 'BEGIN CONF' and 'END CONF'!
sub new {
my( $classname ) = shift();
my( $conf ) = shift();
my( $this ) = {};
############################## BEGIN CONF ##############################################################################
#Source language
addValue( $this, $conf, "SRCL", "en" );
#Target language
addValue( $this, $conf, "TRGL", "ro" );
#How many EM loops should EMACC execute
addValue( $this, $conf, "EMLOOPS", 5 );
#EMACC mode: 'emacc-full' (the EM algorithm) or 'emacc-simple' (a simple greedy alignment in which all pairs above a certain prob are output).
#Currently, if too many possible document pairs, the models are too big.
addValue( $this, $conf, "EMACCMODE", 'emacc-full' );
#How many document alignments we permit.
#If MAXTARGETALIGNMENTS = 1, then we have only 1:1 alignments, if = 2, then we permit 1:2 alignments...
addValue( $this, $conf, "MAXTARGETALIGNMENTS", 3 );
#What is the translation equivalents probability above which we should re-estimate their probabilities in the EM process?
addValue( $this, $conf, "TEQPUPDATETHR", 0.4 );
#What percent from the alignments to output? (usually 1)
#But for 'emacc-simple' this is usually lower (e.g. 0.5)
addValue( $this, $conf, "OUTPUTPERCENT", 1 );
#What percent from the top document alignments found to be used for translation equivalents re-estimation (see the paper from BUCC 2011)
addValue( $this, $conf, "UPDATEPERCENT", $this->{"OUTPUTPERCENT"} );
#How small is the smallest value? (best left unchanged)
addValue( $this, $conf, "DEFSMALLP", 1e-30 );
#Initial document alignment distribution:
#- uniform: D1
#- document similarity precomputed: D2
#Best results: D2!
addValue( $this, $conf, "INIDISTRIB", "D2" );
#This variable holds the name of the corpus we are processing. It will appear in the output model names.
addValue( $this, $conf, "CORPUSNAME", "emacc2-run" );
#The name of the output file (the file that will store the document alignments)
#It is advised to keep that name that contains the values of important parameters.
addValue( $this, $conf, "DALOUTFILE", $this->{"SRCL"} . "-" . $this->{"TRGL"} . "-" . $this->{"CORPUSNAME"} . "-INID_" . $this->{"INIDISTRIB"} . "-TEQUPD_" . $this->{"TEQPUPDATETHR"} . "-OUTP_" . $this->{"OUTPUTPERCENT"} . ".al" );
#What is the minimum probability of a translation equivalents pair in order to be used in the alignment process?
#GIZA++ scores: 0..1
addValue( $this, $conf, "LEXALSCORE", 0.4 );
#What measure to use when computing the probability of the initial alignment of two documents:
#'fair': the measure is given by the total number of translations over all possible pairs;
#'biased': D2 measure (see the BUCC 2011 paper from ACCURAT publications page).
addValue( $this, $conf, "PLEXTYPE", 'biased' );
#If to try and stem words (1) or not (0).
#Lemmas are tried with suffix matching ... time consuming.
addValue( $this, $conf, "LEMMAS", 1 );
#The 'clsuter.info' file which holds the number of available machines/cpus
addValue( $this, $conf, "CLUSTERFILE", 'generate' );
#The NFS mount point (the NFS directory which contains the documents do be aligned and the lingustic resources)
#It can be a local directory if no clustering is involved.
#This directory must exist (on each cluster node) and must be writable and readable by 'rion' user.
addValue( $this, $conf, "NFSMOUNTPOINT", "." );
#This directory must exist (on each cluster node) and must be writable and readable by 'rion' user.
#An attempt is made to create it if does not exist.
my( $tmpdir ) = File::Spec->catdir( File::Spec->tmpdir(), "tmpalign" );
mkpath( $tmpdir );
addValue( $this, $conf, "LOCALMOUNTPOINT", $tmpdir );
#The pre-computed D(1|2) distribution file name (the '.pc' file)
addValue( $this, $conf, "PRECOMPMODELFILE", $this->{"SRCL"} . "-" . $this->{"TRGL"} . "-" . $this->{"CORPUSNAME"} . "-" . $this->{"PLEXTYPE"} . "-LEM_" . $this->{"LEMMAS"} . "-TEQ_" . $this->{"LEXALSCORE"} . ".pc" );
#The pre-computed translation equivalents pairs per document pair (the '.f' file)
addValue( $this, $conf, "DPAIRTEQMODELFILE", $this->{"SRCL"} . "-" . $this->{"TRGL"} . "-" . $this->{"CORPUSNAME"} . "-LEM_" . $this->{"LEMMAS"} . "-TEQ_" . $this->{"LEXALSCORE"} . ".f" );
#When using GIZA++ probabilities
#'giza' - retain the original probability;
#'comp' - recompute a different probabiliry.
addValue( $this, $conf, "PROBTYPE", 'giza' );
#Stop words files:
addValue( $this, $conf, "STOPWSRCL", File::Spec->catfile( $this->{"NFSMOUNTPOINT"}, "res", "stopwords_" . $this->{"SRCL"} . ".txt" ) );
addValue( $this, $conf, "STOPWTRGL", File::Spec->catfile( $this->{"NFSMOUNTPOINT"}, "res", "stopwords_" . $this->{"TRGL"} . ".txt" ) );
#Inflectional endings files:
addValue( $this, $conf, "INFLSRCL", File::Spec->catfile( $this->{"NFSMOUNTPOINT"}, "res", "endings_" . $this->{"SRCL"} . ".txt" ) );
addValue( $this, $conf, "INFLTRGL", File::Spec->catfile( $this->{"NFSMOUNTPOINT"}, "res", "endings_" . $this->{"TRGL"} . ".txt" ) );
#We have en_<lang> dictionaries.
#If you want <lang>_en alignments, put 1 here (and be sure to set SRCL and TRGL properly!). Else 0.
addValue( $this, $conf, "DICTINVERSE", 0 ); checkBool( "DICTINVERSE", $this );
my( $DICTFILE );
my( $DICTINVERSE ) = $this->{"DICTINVERSE"};
if ( $DICTINVERSE ) {
$DICTFILE = File::Spec->catfile( $this->{"NFSMOUNTPOINT"}, "dict", $this->{"TRGL"} . "_" . $this->{"SRCL"} );
}
else {
$DICTFILE = File::Spec->catfile( $this->{"NFSMOUNTPOINT"}, "dict", $this->{"SRCL"} . "_" . $this->{"TRGL"} );
}
addValue( $this, $conf, "DICTFILE", $DICTFILE );
############################## END CONF ################################################################################
checkLang( "SRCL", $this );
checkLang( "TRGL", $this );
checkReal( "DEFSMALLP", $this );
checkProb( "TEQPUPDATETHR", $this );
checkProb( "OUTPUTPERCENT", $this );
checkProb( "LEXALSCORE", $this );
checkProb( "UPDATEPERCENT", $this );
checkInt( "EMLOOPS", $this );
checkInt( "MAXTARGETALIGNMENTS", $this );
checkInitMode( "INIDISTRIB", $this );
checkPLexType( "PLEXTYPE", $this );
checkEmaccMode( "EMACCMODE", $this );
checkProbType( "PROBTYPE", $this );
checkBool( "LEMMAS", $this );
checkClusterFile( "CLUSTERFILE", $this );
checkDir( "NFSMOUNTPOINT", $this );
checkFile( "STOPWSRCL", $this );
checkFile( "STOPWTRGL", $this );
checkFile( "INFLSRCL", $this );
checkFile( "INFLTRGL", $this );
checkFile( "DICTFILE", $this );
checkDir( "NFSMOUNTPOINT", $this );
checkDir( "LOCALMOUNTPOINT", $this );
bless( $this, $classname );
return $this;
}
#The rest of these functions are not to be called through the object interface.
sub addValue( $$$$ ) {
my( $this, $conf, $varname, $vardefaultvalue ) = @_;
if ( exists( $conf->{$varname} ) && $conf->{$varname} ne "" ) {
$this->{$varname} = $conf->{$varname};
}
else {
$this->{$varname} = $vardefaultvalue;
}
}
sub checkDir( $$ ) {
my( $varname, $this ) = @_;
my( $dir ) = $this->{$varname};
my( $testfile ) = File::Spec->catfile( $dir, "test2476blah2144" );
open( TF, ">", $testfile ) or die( "emaccconf::checkDir: '$varname' has issues: '$! (" . int( $! ) . ")'.\n" );
close( TF );
unlink( $testfile ) or warn( "emaccconf::checkDir: could not remove '$testfile' because '$!'.\n" );
}
sub checkFile( $$ ) {
my( $varname, $this ) = @_;
my( $file ) = $this->{$varname};
if ( ! -f ( $file ) ) {
die( "emaccconf::checkFile: '$varname' does not exist !\n" );
}
}
sub checkClusterFile( $$ ) {
my( $varname, $this ) = @_;
if ( $this->{$varname} eq "generate" ) {
genClusterFile();
$this->{$varname} = "cluster-autogen.info";
}
checkFile( $varname, $this );
}
sub checkInitMode( $$ ) {
my( $varname, $this ) = @_;
my( $smode ) = $this->{$varname};
if ( $smode ne "D1" && $smode ne "D2" ) {
die( "emaccconf::checkInitMode: invalid value for '$varname' (either 'D1' or 'D2') !\n" );
}
}
sub checkPLexType( $$ ) {
my( $varname, $this ) = @_;
my( $smode ) = $this->{$varname};
if ( $smode ne "fair" && $smode ne "biased" ) {
die( "emaccconf::checkPLexType: invalid value for '$varname' (either 'fair' or 'biased') !\n" );
}
}
sub checkEmaccMode( $$ ) {
my( $varname, $this ) = @_;
my( $smode ) = $this->{$varname};
if ( $smode ne "emacc-full" && $smode ne "emacc-simple" ) {
die( "emaccconf::checkEmaccMode: invalid value for '$varname' (either 'emacc-full' or 'emacc-one') !\n" );
}
}
sub checkProbType( $$ ) {
my( $varname, $this ) = @_;
my( $smode ) = $this->{$varname};
if ( $smode ne "giza" && $smode ne "comp" ) {
die( "emaccconf::checkProbType: invalid value for '$varname' (either 'giza' or 'comp') !\n" );
}
}
sub checkInt( $$ ) {
my( $varname, $this ) = @_;
my( $int ) = $this->{$varname};
if ( $int !~ /^[0-9]+$/ ) {
die( "emaccconf::checkInt: invalid value for '$varname' !\n" );
}
}
sub checkProb( $$ ) {
my( $varname, $this ) = @_;
my( $prob ) = $this->{$varname};
if ( $prob !~ /^[0-9]+(?:\.[0-9]+)?(?:[eE]-?[0-9]+)?$/ ) {
die( "emaccconf::checkProb: invalid value for '$varname' (real number) !\n" );
}
if ( $prob < 0 || $prob > 1 ) {
die( "emaccconf::checkProb: invalid value for '$varname' ([0..1]) !\n" );
}
}
sub checkReal( $$ ) {
my( $varname, $this ) = @_;
my( $real ) = $this->{$varname};
if ( $real !~ /^[0-9]+(?:\.[0-9]+)?(?:[eE]-?[0-9]+)?$/ ) {
die( "emaccconf::checkReal: invalid value for '$varname' !\n" );
}
}
sub checkBool( $$ ) {
my( $varname, $this ) = @_;
my( $bool ) = $this->{$varname};
if ( $bool !~ /^[01]$/ ) {
die( "emaccconf::checkBool: invalid value for '$varname' (either '0' or '1') !\n" );
}
}
sub checkLang( $$ ) {
my( $varname, $this ) = @_;
my( $lang ) = $this->{$varname};
if ( $lang !~ /^(?:en|ro|de|lt|lv|sl|el|hr|et)$/ ) {
die( "emaccconf::checkLang: invalid value for '$varname' !\n" );
}
}
sub genClusterFile() {
my( $thishostname ) = hostname();
open( CLF, ">", "cluster-autogen.info" ) or die( "pdataextractconf::genClusterFile: cannot open file 'cluster-autogen.info' !\n" );
print( CLF "#This is a comment.\n" );
print( CLF "#This autogenerated file will NOT work if a cluster run is desired!\n" );
print( CLF "#Line format (tab separated fields):\n" );
print( CLF "#- hostname of the machine in cluster (run 'hostname' command)\n" );
print( CLF "#- IP of the machine\n" );
print( CLF "#- ID (string) of one CPU core\n\n" );
#Linux systems...
if ( -f ( "/proc/cpuinfo" ) ) {
open( CPU, "<", "/proc/cpuinfo" ) or die( "pdataextractconf::genClusterFile: cannot open file '/proc/cpuinfo' !\n" );
while ( my $line = <CPU> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if ( $line !~ /:/ );
my( $variable, $value ) = split( /\s*:\s*/, $line );
$variable =~ s/^\s+//;
$variable =~ s/\s+$//;
$value =~ s/^\s+//;
$value =~ s/\s+$//;
if ( $variable eq "processor" ) {
print( CLF $thishostname . "\t" . "127.0.0.1" . "\t" . "cpu$value" . "\n" );
}
}
close( CPU );
}
#Windows systems...
else {
#Don't know. 1 core :D
print( CLF $thishostname . "\t" . "127.0.0.1" . "\t" . "cpu0" . "\n" );
}
close( CLF );
}
sub portableCopyFileToDir( $$ ) {
my( $file, $dir ) = @_;
#Windows run
if ( $^O =~ /^MSWin(?:32|64)$/i ) {
warn( "`copy \/Y ${file} ${dir}\\'\n" );
qx/copy \/Y ${file} ${dir}\\/;
}
#Linux run
elsif ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
qx/cp -fv ${file} ${dir}\/ 1>&2/;
}
else {
die( "emaccconf::portableCopyFileToDir: unsupported operating system '$^O' !\n" );
}
}
sub portableRemoveFile( $ ) {
my( $file ) = $_[0];
#Windows run
if ( $^O =~ /^MSWin(?:32|64)$/i ) {
warn( "`del \/F \/Q ${file}'\n" );
qx/del \/F \/Q ${file}/;
}
#Linux run
elsif ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
qx/rm -fv ${file} 1>&2/;
}
else {
die( "emaccconf::portableRemoveFile: unsupported operating system '$^O' !\n" );
}
}
sub portableRemoveFileFromDir( $$ ) {
my( $dir, $file ) = @_;
#Windows run
if ( $^O =~ /^MSWin(?:32|64)$/i ) {
warn( "`del \/F \/Q ${dir}\\${file}'\n" );
qx/del \/F \/Q ${dir}\\${file}/;
}
#Linux run
elsif ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
qx/rm -fv ${dir}\/${file} 1>&2/;
}
else {
die( "emaccconf::portableRemoveFileFromDir: unsupported operating system '$^O' !\n" );
}
}
sub portableRemoveAllFilesFromDir( $ ) {
my( $dir ) = $_[0];
#Windows run
if ( $^O =~ /^MSWin(?:32|64)$/i ) {
warn( "`/del \/F \/Q ${dir}\\'\n" );
qx/del \/F \/Q ${dir}\\/;
}
#Linux run
elsif ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
qx/rm -fv ${dir}\/* 1>&2/;
}
else {
die( "emaccconf::portableRemoveAllFilesFromDir: unsupported operating system '$^O' !\n" );
}
}
sub portableForkAndDetach( $ ) {
my( $cmd ) = $_[0];
#Windows run
if ( $^O =~ /^MSWin(?:32|64)$/i ) {
warn( "`start /B ${cmd}'\n" );
system( "start /B ${cmd}" );
}
#Linux run
elsif ( $^O =~ /^Linux$/i || $^O =~ /^Cygwin$/i || $^O =~ /^MSys$/i ) {
warn( "`${cmd} &'\n" );
system( "${cmd} &" );
}
else {
die( "emaccconf::portableForkAndDetach: unsupported operating system '$^O' !\n" );
}
}
1;