-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTermSuiteTsv2DocTerm.pl
executable file
·201 lines (175 loc) · 4.79 KB
/
TermSuiteTsv2DocTerm.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
193
194
195
196
197
198
199
200
201
#!/usr/bin/perl
# Déclaration des pragmas
use strict;
use utf8;
use open qw/:std :utf8/;
# Appel des modules externes de base
use Encode qw(decode_utf8 encode_utf8 is_utf8);
use Getopt::Long;
my ($programme) = $0 =~ m|^(?:.*/)?(.+)|;
my $substitut = " " x length($programme);
my $usage = "Usage : \n" .
" $programme -i input -o output [ -x ]\n" .
" $programme -h \n";
my $version = "0.3.1";
my $dateModif = "September 18, 2019";
# Options
my $aide = 0;
my $fichier = "";
my $liste = "";
my $repertoire = "";
my $sortie = undef;
my $exclude = undef;
eval {
$SIG{__WARN__} = sub {usage(1);};
GetOptions(
"help" => \$aide,
"input=s" => \$fichier,
"liste=s" => \$liste,
"output=s" => \$sortie,
"xclude" => \$exclude,
);
};
$SIG{__WARN__} = sub {warn $_[0];};
if ( $aide ) {
print "\nProgramme : \n “$programme”, version $version ($dateModif)\n";
print " Transforms a list of terms extracted by “TermSuite” into \n";
print " a “doc × term” file. \n";
print " \n";
print "$usage\n";
print "Options : \n";
print " -h displays that help and exits \n";
print " -i gives the name of the input TSV file \n";
# print " -l donne le nom du fichier contenant la liste des mots-clés valides, un \n";
# print " par ligne \n";
print " -o gives the name of the output file \n";
print " -x exclude shorter terms present in other terms \n";
exit 0;
}
usage(2) if not $fichier;
usage(4) if not $sortie;
# Autres variables
my $current = undef;
my $log10 = log(10);
my %att = ();
my %statut = statut();
my %nbDoc = ();
my %valide = ();
if ( $liste ) {
open(MCV, "<:utf8", $liste) or die "$!,";
while(<MCV>) {
chomp;
s/\r//go;
s/^.*\t//o;
s/^\s+//o;
s/\s+\z//o;
$valide{$_} ++;
}
close MCV;
}
open(OUT, ">:utf8", $sortie) or die "$!,";
open(INP, "<:utf8", $fichier) or die "$!,";
while(<INP>) {
chomp;
my ($file, $nb, $terme, $cat, $str) = split(/\t/);
if ( $current and $current ne $file ) {
traite();
%att = ();
}
$current = $file;
if ( $liste ) {
next if not $valide{$terme};
}
if ( $statut{$cat} ) {
next if $statut{$cat} eq 'S';
}
else {
print STDERR "Catégorie gramaticale inédite : “$cat”\n";
next if $cat =~ /[cprv]/o;
next if $cat !~ /n/o;
next if length($cat) < 2;
}
$att{$terme}{'nb'} += $nb;
$att{$terme}{'cat'}{$cat} += $nb;
$att{$terme}{'str'}{$str} = $nb;
}
close INP;
if ( $current ) {
traite();
}
exit 0;
sub usage
{
print STDERR $usage;
exit shift;
}
sub log10
{
my $valeur = shift;
return log($valeur)/$log10;
}
sub traite
{
if ( $exclude ) {
my @termes = sort {length($a) <=> length($b) or $a cmp $b} keys %att;
for ( my $i = 0 ; $i < $#termes ; $i ++ ) {
for ( my $j = $i + 1 ; $j <= $#termes ; $j ++ ) {
if ( $termes[$j] =~ /\b$termes[$i]\b/ ) {
delete $att{$termes[$i]};
last;
}
}
}
}
foreach my $terme (sort keys %att) {
print OUT "$current\t$terme\n";
}
}
sub statut
{
my %cat = (
"a" => "S",
"aaan" => "C",
"aaann" => "C",
"aan" => "C",
"aann" => "C",
"aannn" => "C",
"acan" => "S",
"acann" => "S",
"an" => "C",
"anan" => "C",
"anann" => "C",
"ann" => "C",
"annn" => "C",
"annnn" => "C",
"anpn" => "S",
"anpnn" => "S",
"n" => "S",
"naan" => "C",
"nan" => "C",
"nann" => "C",
"ncan" => "S",
"ncnn" => "S",
"ncnpn" => "S",
"nn" => "C",
"nnan" => "C",
"nnn" => "C",
"nnnn" => "C",
"nnnnn" => "C",
"nnpn" => "S",
"npan" => "S",
"npn" => "S",
"npncn" => "S",
"npncnn" => "S",
"npnn" => "S",
"npnnn" => "S",
"npnpn" => "S",
"nva" => "S",
"r" => "S",
"raan" => "S",
"ran" => "S",
"rann" => "S",
"rannn" => "S",
);
return %cat;
}