-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentAlignment.pl
More file actions
490 lines (411 loc) · 13.8 KB
/
DocumentAlignment.pl
File metadata and controls
490 lines (411 loc) · 13.8 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# Document Alignment step of the Parallel Data Extraction Workflow.
#
# Author: Radu ION, Research Institute for AI, Romanian Academy (RACAI)
#
# ver 0.1, 30-Aug-11, Radu ION: created.
# ver 0.2, 30-Aug-11, Radu ION: fixed ComMetric bug with temporary files.
# ver 0.3, 07-Jun-12, Mârcis Pinnis: ported updates from ParallelDataMining.pl ver 2.1.
use strict;
use warnings;
sub getFullLanguageName( $ );
sub absolutePathDocuments( $ );
sub readPropertyFile( $ );
sub normalizeLang( $ );
sub readCmdLineArguments( @ );
#ComMetric does alignments from Non-English to English only (so does the Feature Classifier).
sub swapDocumentAlignments( $ );
if ( scalar( @ARGV ) == 0 ) {
die( "Usage: DocumentAlignment.pl
--source <language> --target <language>
--param CONFIG=path\\to\\config.prop
--param DOCALIGN=<dictmetric|commetric|emacc|featclass>
--input <path to source documents file>
--input <path to target documents file>
--output <path to the document alignment file>\n" );
}
my( $pdataminingconf ) = readCmdLineArguments( @ARGV );
my( $docaligntool ) = $pdataminingconf->{"DOCALIGN"};
#Some document alignment tools know to align from Non-English to English only.
#Thus if alignment is requested from English to Non-English, we must swap the obtained alignments.
my( %SWAP_DOCUMENT_ALIGNMENTS ) = (
"commetric" => 0,
"emacc" => 0,
"dictmetric" => 0,
"featclass" => 0
);
#java, perl must be accesible from the %PATH%.
my( %DOCUMENT_ALIGNMENT_TOOLS ) = (
#CTS ComMetric
"commetric" => {
"COMMAND" => "java -jar commetric\\ComMetric.jar",
"ARGS" => [
"--source " . do {
my( $lang ) = $pdataminingconf->{"SRCL"};
die( "DocumentAlignment::main: 'commetric' document alignment tool must have one English side !\n" )
if ( $pdataminingconf->{"SRCL"} ne "en" && $pdataminingconf->{"TRGL"} ne "en" && $docaligntool eq "commetric" );
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$lang = $pdataminingconf->{"TRGL"};
$SWAP_DOCUMENT_ALIGNMENTS{"commetric"} = 1;
}
uc( getFullLanguageName( $lang ) );
},
"--target " . do {
my( $lang ) = $pdataminingconf->{"TRGL"};
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$lang = $pdataminingconf->{"SRCL"};
$SWAP_DOCUMENT_ALIGNMENTS{"commetric"} = 1;
}
uc( getFullLanguageName( $lang ) );
},
"--WN \"" . $pdataminingconf->{"CONFIG"}->{"ComMetric_WN"} . "\"",
"--threshold " . $pdataminingconf->{"CONFIG"}->{"ComMetric_Threshold"},
"--translationAPI " . $pdataminingconf->{"CONFIG"}->{"ComMetric_TranslationAPI"},
"--input " . do {
my( $input1 ) = $pdataminingconf->{"INPUTFILE1"};
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$input1 = $pdataminingconf->{"INPUTFILE2"};
}
$input1;
},
"--input " . do {
my( $input2 ) = $pdataminingconf->{"INPUTFILE2"};
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$input2 = $pdataminingconf->{"INPUTFILE1"};
}
$input2;
},
"--output " . $pdataminingconf->{"OUTPUTFILE"},
"--tempDir " . do {
my( $tmpdir ) = $pdataminingconf->{"CONFIG"}->{"ComMetric_TempDir"};
#Clean up
qx/del \/F \/Q \/S ${tmpdir}\\/;
$tmpdir;
}
]
},
#CTS DictMetric
"dictmetric" => {
"COMMAND" => "java -jar dictmetric\\DictMetric.jar",
"ARGS" => [
"--source " . do {
my( $lang ) = $pdataminingconf->{"SRCL"};
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$lang = $pdataminingconf->{"TRGL"};
$SWAP_DOCUMENT_ALIGNMENTS{"dictmetric"} = 1;
}
lc( getFullLanguageName( $lang ) );
},
"--target " . do {
my( $lang ) = $pdataminingconf->{"TRGL"};
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$lang = $pdataminingconf->{"SRCL"};
$SWAP_DOCUMENT_ALIGNMENTS{"dictmetric"} = 1;
}
lc( getFullLanguageName( $lang ) );
},
"--WN \"" . $pdataminingconf->{"CONFIG"}->{"DictMetric_WN"} . "\"",
"--threshold " . $pdataminingconf->{"CONFIG"}->{"DictMetric_Threshold"},
"--input " . do {
my( $input1 ) = $pdataminingconf->{"INPUTFILE1"};
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$input1 = $pdataminingconf->{"INPUTFILE2"};
}
$input1;
},
"--input " . do {
my( $input2 ) = $pdataminingconf->{"INPUTFILE2"};
if ( $pdataminingconf->{"SRCL"} eq "en" ) {
$input2 = $pdataminingconf->{"INPUTFILE1"};
}
$input2;
},
"--output " . $pdataminingconf->{"OUTPUTFILE"},
"--tempDir " . do {
my ( $tmpdir ) = $pdataminingconf->{"CONFIG"}->{"DictMetric_TempDir"};
#Clean up
qx/del \/F \/Q \/S ${tmpdir}\\/;
$tmpdir;
},
"--option " . $pdataminingconf->{"CONFIG"}->{"DictMetric_Option"}
]
},
#RACAI EMACC
"emacc" => {
"COMMAND" => "perl emacc\\emacc2.pl",
"ARGS" => [
"--source " . $pdataminingconf->{"SRCL"},
"--target " . $pdataminingconf->{"TRGL"},
"--param EMLOOPS=" . $pdataminingconf->{"CONFIG"}->{"EMACC_EMLOOPS"},
"--param MAXTARGETALIGNMENTS=" . $pdataminingconf->{"CONFIG"}->{"EMACC_MAXTARGETALIGNMENTS"},
"--param INIDISTRIB=" . $pdataminingconf->{"CONFIG"}->{"EMACC_INIDISTRIB"},
"--param TEQPUPDATETHR=" . $pdataminingconf->{"CONFIG"}->{"EMACC_TEQPUPDATETHR"},
"--param LEXALSCORE=" . $pdataminingconf->{"CONFIG"}->{"EMACC_LEXALSCORE"},
"--param PROBTYPE=" . $pdataminingconf->{"CONFIG"}->{"EMACC_PROBTYPE"},
"--param DICTINVERSE=0",
"--param CLUSTERFILE=" . $pdataminingconf->{"CONFIG"}->{"EMACC_CLUSTERFILE"},
"--input " . $pdataminingconf->{"INPUTFILE1"},
"--input " . $pdataminingconf->{"INPUTFILE2"},
"--output " . $pdataminingconf->{"OUTPUTFILE"}
]
},
#USFD Feature-based Document Pair Classifier
#Uses Google Translate which does not work.
#Not in use until a fix is available.
"featclass" => {
"COMMAND" => "echo ParallelDataMining::main: 'featclass' needs an upgrade. Will not run it.",
"ARGS" => []
},
#"featclass" => {
# "COMMAND" => "featclass.bat",
# "ARGS" => [
# "--source " . do {
# my( $windirmodelscmd ) = "dir /B " . $pdataminingconf->{"CONFIG"}->{"FeatClass_Models"};
# my( @windirmodels ) = qx/$windirmodelscmd/;
# my( $foundlangpair ) = 0;
#
# MODELS:
# foreach my $m ( @windirmodels ) {
# $m =~ s/^\s+//;
# $m =~ s/\s+$//;
#
# my( @toks ) = split( /(?:\W|_)+/, $m );
#
# for ( my $i = 0; $i < scalar( @toks ); $i++ ) {
# if ( $toks[$i] =~ /^(en|ro|sl|lt|lv|hr|et|el|de)$/i && $toks[$i + 1] =~ /^(en|ro|sl|lt|lv|hr|et|el|de)$/i ) {
# if ( lc( $toks[$i] ) eq $pdataminingconf->{"SRCL"} && lc( $toks[$i + 1] ) eq $pdataminingconf->{"TRGL"} ) {
# $foundlangpair = 1;
# last MODELS;
# }
# }
# }
# }
#
# die( "ParallelDataMining::main: no '" . $pdataminingconf->{"SRCL"} . "-" . $pdataminingconf->{"TRGL"} . "' models in 'featclass' models directory !\n" )
# if ( ! $foundlangpair && $selecteddocaligntool eq "featclass" );
#
# getFullLanguageName( $pdataminingconf->{"SRCL"} )
# },
# "--target " . getFullLanguageName( $pdataminingconf->{"TRGL"} ),
# "--param threshold=" . $pdataminingconf->{"CONFIG"}->{"FeatClass_Threshold"},
# "--input " . do {
# die( "ParallelDataMining::main: 'featclass' requires lists of documents with absolute paths (first --input) !\n" )
# if ( ! absolutePathDocuments( $pdataminingconf->{"INPUTFILE1"} ) && $selecteddocaligntool eq "featclass" );
#
# my( $crtdir ) = qx/echo %CD%/;
#
# $crtdir =~ s/^\s+//;
# $crtdir =~ s/\s+$//;
#
# $crtdir . "\\" . $pdataminingconf->{"INPUTFILE1"} if ( $pdataminingconf->{"INPUTFILE1"} !~ /^[A-Z]:\\/ );
# },
# "--input " . do {
# die( "ParallelDataMining::main: 'featclass' requires lists of documents with absolute paths (second --input) !\n" )
# if ( ! absolutePathDocuments( $pdataminingconf->{"INPUTFILE2"} ) && $selecteddocaligntool eq "featclass" );
#
# my( $crtdir ) = qx/echo %CD%/;
#
# $crtdir =~ s/^\s+//;
# $crtdir =~ s/\s+$//;
#
# $crtdir . "\\" . $pdataminingconf->{"INPUTFILE2"} if ( $pdataminingconf->{"INPUTFILE2"} !~ /^[A-Z]:\\/ );
# },
# "--output " . $pdataminingconf->{"OUTPUTFILE"}
# ]
#}
);
#1. DOCUMENT ALIGNMENT STEP.
warn( "DocumentAlignment::main: running '$docaligntool' document aligner ...\n\n" );
warn( "############## Tool Output ##################################\n\n" );
my( $cmdline ) = "";
$cmdline .= $DOCUMENT_ALIGNMENT_TOOLS{$docaligntool}->{"COMMAND"} . " ";
$cmdline .= join( " ", @{ $DOCUMENT_ALIGNMENT_TOOLS{$docaligntool}->{"ARGS"} } );
warn( $cmdline . "\n\n" );
system( $cmdline );
swapDocumentAlignments( $pdataminingconf->{"OUTPUTFILE"} ) if ( $SWAP_DOCUMENT_ALIGNMENTS{$docaligntool} );
warn( "\n############## End Tool Output ##############################\n\n" );
warn( "DocumentAlignment::main: end '$docaligntool' document aligner.\n\n" );
#END MAIN.
sub readCmdLineArguments( @ ) {
my( @args ) = @_;
my( %clconf ) = ();
my( %allowedparams ) = (
"CONFIG" => 1,
"DOCALIGN" => 1
);
my( $input ) = 0;
while ( scalar( @args ) > 0 ) {
my( $opt ) = shift( @args );
SWOPT: {
$opt eq "--source" and do {
$clconf{"SRCL"} = normalizeLang( shift( @args ) );
last;
};
$opt eq "--target" and do {
$clconf{"TRGL"} = normalizeLang( shift( @args ) );
last;
};
$opt eq "--param" and do {
my( $param, $value ) = split( /\s*=\s*/, shift( @args ) );
$param = uc( $param );
die( "DocumentAlignment::readCmdLineArguments: unknown parameter '$param' !\n" )
if ( ! exists( $allowedparams{$param} ) );
$clconf{$param} = $value;
last;
};
$opt eq "--input" and do {
$input++;
$clconf{"INPUTFILE$input"} = shift( @args );
last;
};
$opt eq "--output" and do {
$clconf{"OUTPUTFILE"} = shift( @args );
last;
};
die( "DocumentAlignment::readCmdLineArguments: unknown option '$opt' !\n" );
}
}
die( "DocumentAlignment::readCmdLineArguments: required '--source' switch not present !\n" )
if ( ! exists( $clconf{"SRCL"} ) );
die( "DocumentAlignment::readCmdLineArguments: required '--target' switch not present !\n" )
if ( ! exists( $clconf{"TRGL"} ) );
die( "DocumentAlignment::readCmdLineArguments: required '--param DOCALIGN' switch not present !\n" )
if ( ! exists( $clconf{"DOCALIGN"} ) );
$clconf{"DOCALIGN"} = lc( $clconf{"DOCALIGN"} );
die( "DocumentAlignment::readCmdLineArguments: DOCALIGN '" . $clconf{"DOCALIGN"} . "' is not a known value !\n" )
if ( $clconf{"DOCALIGN"} !~ /^(commetric|emacc|featclass|dictmetric)$/ );
die( "DocumentAlignment::readCmdLineArguments: a required '--input' switch is not present !\n" )
if ( ! exists( $clconf{"INPUTFILE1"} ) || ! exists( $clconf{"INPUTFILE2"} ) );
die( "DocumentAlignment::readCmdLineArguments: required '--output' switch not present !\n" )
if ( ! exists( $clconf{"OUTPUTFILE"} ) );
#Read in the property file.
if ( exists( $clconf{"CONFIG"} ) ) {
$clconf{"CONFIG"} = readPropertyFile( $clconf{"CONFIG"} );
}
else {
$clconf{"CONFIG"} = readPropertyFile( "DocumentAlignment.prop" );
}
return \%clconf;
}
sub readPropertyFile( $ ) {
my( %propHash ) = ();
open( INFILE, "<", $_[0] ) or die( "DocumentAlignment::readPropertyFile: cannot open file '$_[0]' !\n" );
binmode( INFILE, ":utf8" );
while ( my $line = <INFILE> ) {
$line =~ s/^\x{FEFF}//; # cuts BOM
$line =~ s/^\s+//;
$line =~ s/\s+$//;
if ( $line ne "" && $line !~ /^\s*#/ ) {
my( $key, $value ) = split( /=/, $line, 2 );
$key =~ s/^\s+//;
$key =~ s/\s+$//;
$value =~ s/^\s+//;
$value =~ s/\s+$//;
if ( defined( $key ) && defined( $value ) ) {
$propHash{$key} = $value;
}
}
}
close INFILE;
return \%propHash;
}
sub normalizeLang( $ ) {
my( $lang ) = lc( $_[0] );
my( %accuratlanguages ) = (
#1
"romanian" => "ro",
"rum" => "ro",
"ron" => "ro",
"ro" => "ro",
#2
"english" => "en",
"eng" => "en",
"en" => "en",
#3
"estonian" => "et",
"est" => "et",
"et" => "et",
#4
"german" => "de",
"ger" => "de",
"deu" => "de",
"de" => "de",
#5
"greek" => "el",
"gre" => "el",
"ell" => "el",
"el" => "el",
#6
"croatian" => "hr",
"hrv" => "hr",
"hr" => "hr",
#7
"latvian" => "lv",
"lav" => "lv",
"lv" => "lv",
#8
"lithuanian" => "lt",
"lit" => "lt",
"lt" => "lt",
#9
"slovenian" => "sl",
"slovene" => "sl",
"slv" => "sl",
"sl" => "sl"
);
return $accuratlanguages{$lang} if ( exists( $accuratlanguages{$lang} ) );
die( "DocumentAlignment::normalizeLang: unknown language '$lang' !\n" );
}
sub getFullLanguageName( $ ) {
my( $lang ) = lc( $_[0] );
my( %accuratlanguages ) = (
#1
"ro" => "Romanian",
#2
"en" => "English",
#3
"et" => "Estonian",
#4
"de" => "German",
#5
"el" => "Greek",
#6
"hr" => "Croatian",
#7
"lv" => "Latvian",
#8
"lt" => "Lithuanian",
#9
"sl" => "Slovene"
);
return $accuratlanguages{$lang} if ( exists( $accuratlanguages{$lang} ) );
die( "DocumentAlignment::getFullLanguageName: unknown language '$lang' !\n" );
}
sub swapDocumentAlignments( $ ) {
my( @swappedalignments ) = ();
open( DOCAL, "<", $_[0] ) or die( "DocumentAlignment::swapDocumentAlignments: cannot open file '$_[0]' for reading !\n" );
binmode( DOCAL, ":utf8" );
while ( my $line = <DOCAL> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my( $srcdoc, $trgdoc, $score ) = split( /\t+/, $line );
push( @swappedalignments, $trgdoc . "\t" . $srcdoc . "\t" . $score . "\n" );
}
close( DOCAL );
open( DOCAL, ">", $_[0] ) or die( "DocumentAlignment::swapDocumentAlignments: cannot open file '$_[0]' for writing !\n" );
binmode( DOCAL, ":utf8" );
foreach my $al ( @swappedalignments ) {
print( DOCAL $al );
}
close( DOCAL );
}
sub absolutePathDocuments( $ ) {
open( DOCL, "<", $_[0] ) or die( "DocumentAlignment::absolutePathDocuments: cannot open file '$_[0]' !\n" );
binmode( DOCL, ":utf8" );
while ( my $line = <DOCL> ) {
$line =~ s/^\s+//;
$line =~ s/\s+$//;
return 0 if ( $line !~ /^[A-Z]:\\/ );
}
close( DOCL );
return 1;
}