-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiteTermes.pl
executable file
·192 lines (162 loc) · 6.07 KB
/
limiteTermes.pl
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
#!/usr/bin/env perl
# Declaration of pragmas
use strict;
use utf8;
use open qw/:std :utf8/;
# Call of external modules
use Encode qw(decode_utf8 encode_utf8 is_utf8);
use Getopt::Long;
# Perl modules specific for this application
use File::Copy;
use Spreadsheet::Read;
use Spreadsheet::ParseXLSX;
# Programme name
my ($programme) = $0 =~ m|^(?:.*/)?(.+)|;
my $substitute = " " x length($programme);
my $usage = "Usage: \n" .
" $programme -i raw_input_file -e Excel_file [ -o output_file ] \n" .
" $programme -h\n\n";
my $version = "0.2.2";
my $changeDate = "August 7, 2019";
# Initialising global variables
# necessary for options
my $excel = undef;
my $help = undef;
my $input = undef;
my $output = undef;
eval {
$SIG{__WARN__} = sub {usage(1);};
GetOptions(
"excel=s" => \$excel,
"help" => \$help,
"input=s" => \$input,
"output=s" => \$output,
"xcel=s" => \$excel,
);
};
$SIG{__WARN__} = sub {warn $_[0];};
if ( $help ) {
print "\nProgramme: \n";
print " “$programme”, version $version ($changeDate)\n";
print " Modify the original “doc × term” file by deleting unwanted \n";
print " terms and replacing others by preferential terms in accordance \n";
print " with the Excel file generated by “listeTerms” and checked by \n";
print " an expert or at least by the user. \n";
# print " \n";
print "\n";
print $usage;
print "\nOptions: \n";
print " -e specify the name of the Excel file \n";
print " -h display this help and exit \n";
print " -i specify the name of the raw “doc × term” input file \n";
print " -o specify the name of the clean “doc × term” output file \n";
exit 0;
}
usage(2) if not $input or not $excel;
# Global variables
my %pref = ();
my %refused = ();
my %term = ();
readExcel();
# Opening output file ...
if ( $output ) {
open(OUT, ">:utf8", $output) or die "$!,";
}
# ... or sending to the standard output
else {
open(OUT, ">&STDOUT") or die "$!,";
binmode(OUT, ":utf8");
}
# Reading input file
open(INP, "<:utf8", $input) or die "$!,";
while(<INP>) {
chomp;
s/\r//go; # just in case ...
my ($file, $term) = split(/\t/);
next if $refused{$term};
if ( $pref{$term} ) {
# print OUT "$file\t$pref{$term}\n";
$term{$file}{$pref{$term}} ++;
}
else {
# print OUT "$file\t$term\n";
$term{$file}{$term} ++;
}
}
close INP;
foreach my $file (sort keys %term) {
foreach my $term (sort keys %{$term{$file}}) {
print OUT "$file\t$term\n";
}
}
close OUT;
exit 0;
sub usage
{
print STDERR $usage;
exit shift;
}
sub readExcel
{
my $ref = ReadData ($excel, parser => 'xlsx');
my $nb_sheets = 0;
my $term_list = 0;
if ( defined $ref->[0]{"sheets"} ) {
$nb_sheets = $ref->[0]{"sheets"};
for (my $number = 1 ; $number <= $nb_sheets ; $number ++ ) {
my $sheet = $ref->[$number];
my $title = $sheet->{"label"};
# $title = decode_utf8($title);
my $maxRows = $sheet->{"maxrow"};
my $maxCols = $sheet->{"maxcol"};
if ( $title eq "Term list" ) {
$term_list ++;
my $line = 1;
my ($col1, $col4, $col5) = (undef, undef, undef);
for ( my $col = 1 ; $col <= $maxCols ; $col ++ ) {
my $cell = cr2cell($col, $line);
# my $text = decode_utf8($sheet->{$cell});
my $text = $sheet->{$cell};
if ( $text eq '?' ) {
$col1 = $col;
}
elsif ( $text eq 'Term' ) {
$col4 = $col;
}
elsif ( $text eq 'Replace by' ) {
$col5 = $col;
}
}
if ( $col1 and $col4 ) {
for ( $line = 2 ; $line <= $maxRows ; $line ++ ) {
my $cell = cr2cell($col4, $line);
# my $term = decode_utf8($sheet->{$cell});
my $term = $sheet->{$cell};
next if not $term;
$cell = cr2cell($col1, $line);
# my $status = decode_utf8($sheet->{$cell});
my $status = $sheet->{$cell};
if ( $status eq 'x' ) {
$refused{$term} ++;
next;
}
$cell = cr2cell($col5, $line);
# my $replace = decode_utf8($sheet->{$cell});
my $replace = $sheet->{$cell};
if ( $replace ) {
$pref{$term} = $replace;
}
}
}
}
}
if ( not $term_list ) {
print STDERR "Error: no term list in Excel file \"excel\"\n";
exit 3;
}
}
else {
print STDERR "Erroe: Excel file \"$excel\" is empty\n";
exit 4;
}
}