-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcosine_bot_sum.pl
More file actions
237 lines (237 loc) · 6.64 KB
/
cosine_bot_sum.pl
File metadata and controls
237 lines (237 loc) · 6.64 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
#!/usr/bin/perl
# Programme Perl pour une classification cosinus sur la tache OM
# Date : 14 05 2014
# Author: Jean-Valère Cossu
# email: [email protected]
# Usage : perl cosine_om_om.pl train test ouput
use strict;
# use warnings;
# use utf8;
if ($#ARGV < 0){
print STDERR "Erreur cosine_om.pl : Argument manquant\n\n";
print STDERR "Usage : perl cosine_om.pl train test ouput\n\n";
exit 1;
}
# Ouverture des fichiers
print "Cosinus ... \n";
print "Classification pour la tache OM\n";
open (CORPUS_TRAIN, $ARGV[0])or die "Erreur cosine_om.pl : Impossible d'ouvrir train input $ARGV[0]\n";
open (CORPUS_TEST, $ARGV[1])or die "Erreur cosine_om.pl : Impossible d'ouvrir test intput $ARGV[1]\n";
my $file="> $ARGV[2]";
open (SYS, $file)or die "Erreur cosine_om.pl : Impossible d'ouvrir ouput $file\n";
# Variables
my @train=();
@train = <CORPUS_TRAIN>;
chomp(@train);
close(CORPUS_TRAIN);
my @test=();
@test = <CORPUS_TEST>;
chomp(@test);
close(CORPUS_TEST);
my %s_wis_2; # Somme des Wi classe1²
my %out_sys; # Somme des Wi classe1²
my %out_gold; # Somme des Wi classe1²
my %cos_d_classe; # poids dans la classe
my %numerateur_classe; # Somme des WIM x Wi pour chaque classe
my %denominateur_classe; # Somme des WIM x Wi pour chaque classe
my %WIM; # WIM
my %lambda; # expected weight of each term in each class - Poisson pmf parameter
my %n_dial = (); # number of dialogs by class
my %lprior = (); # estimated log probability of each class (prior)
my %class_df = (); # class document frequency
my %idf = (); # inverse document frequency (corpus)
my %gini = (); # purity: gini score
my %df = (); # corpus document frequency
my %df1 = (); # corpus document frequency
my %seen = ();
my %nbd_user = ();
my %ref_user = ();
my %hyp_user = ();
my $term; # curren term
my $lab; # current dialog label
my $n_labs; # number of current labels
my $n_dialogs=0; # Nombre de documents
my $min=0;
my $bool=0;
my $coef_gini=1;
my $coef_idf=1;
my $coef_tf_tweet=1;
my $coef_tf=1;
my $z=0; # Compteur du nombre de documents traités
my %stoplist;
&reading;
&build_models;
undef %seen;
foreach my $ligne (@test){
chomp $ligne;
my @list = split('\t', $ligne);
my $user=$list[1];
$nbd_user{$user}++;
my $text =&nettoyage($list[7]);
# if(!exists $seen{$text}){
$seen{$text}=1;
my $hypothese="";
my $total=0;
my $max_methode=0;
my $new_s_WIM_2=0;
my %tf_term_tweet=();
foreach my $label (keys (%n_dial)){
$numerateur_classe{$label}=0;
$denominateur_classe{$label}=0;
$cos_d_classe{$label}=0;
}
my @zero = split(' ',$text);
my $indice=0;
while($indice <= ($#zero)){
my $word=$zero[$indice];
$tf_term_tweet{$word}++;
$indice++;
}
foreach my $mot (@zero){
$new_s_WIM_2+=(($WIM{$mot}*($tf_term_tweet{$mot}**$coef_tf_tweet))**2);
foreach my $label (%n_dial){
if(exists $lambda{$mot}{$label}){
$numerateur_classe{$label}+=($lambda{$mot}{$label}*($WIM{$mot}*($tf_term_tweet{$mot}**$coef_tf_tweet)));
}
}
}
foreach my $label (%n_dial){
$denominateur_classe{$label}=sqrt($s_wis_2{$label}*$new_s_WIM_2);
if($denominateur_classe{$label} !=0){
$cos_d_classe{$label}=$numerateur_classe{$label}/$denominateur_classe{$label};
}
$total+=$cos_d_classe{$label};
}
foreach my $label (%n_dial){
if($total!=0){
$cos_d_classe{$label}/=$total;
$hyp_user{$user}{$label}+=$cos_d_classe{$label};
}
}
$z++;
# }
}
foreach my $user (%nbd_user){
if($nbd_user{$user}>0 && $user ne "1"){
print SYS "$user\t\tOM\t".$hyp_user{$user}{"opinion_maker"}."\tNOM\t".$hyp_user{$user}{"non_opinion_maker"}."\t".$nbd_user{$user}."\n";
my $max=0;
my $hyp=0;
my $hyp1=0;
my $total=0;
foreach my $label (%n_dial){
$total+=$hyp_user{$user}{$label};
}
if($total>0){
foreach my $label (%n_dial){
$hyp_user{$user}{$label}/=$total;
if($max>$hyp_user{$user}{$label}){
$max=$hyp_user{$user}{$label};
$hyp=$label;
}
}
}
print SYS "$user\t\tOM\t".$hyp_user{$user}{"opinion_maker"}."\tNOM\t".$hyp_user{$user}{"non_opinion_maker"}."\t".$nbd_user{$user}."\n";
}
}
print "... Complete, $z documents read\n";
# Fermeture des fichiers
close (SYS);
sub build_models {
foreach my $term (keys (%df)){
if(exists $df1{$term}){
$idf{$term}=log($n_dialogs/($df{$term}+$df1{$term}));
}
else{
$idf{$term}=log($n_dialogs/$df{$term});
}
if($idf{$term}>0){
foreach my $lab (keys %{$class_df{$term}}){
$gini{$term} += (($class_df{$term}{$lab})/($df{$term}))**2;
}
}
else{
delete $df{$term};
delete $idf{$term};
foreach my $lab (keys %{$class_df{$term}}){
delete $class_df{$term}{$lab};
}
}
foreach my $lab (keys %{$class_df{$term}}) {
$lambda{$term}{$lab}=($class_df{$term}{$lab}**$coef_tf)*($idf{$term}**$coef_idf)*($gini{$term}**$coef_gini);
$s_wis_2{$lab}+=($lambda{$term}{$lab}**2);
}
$WIM{$term}=(($idf{$term}**$coef_idf)*($gini{$term}**$coef_gini))/$n_dialogs;
}
undef %idf;
undef %gini;
undef %class_df;
undef %df;
undef %df1;
print "Models DONE\n";
}
sub reading {
foreach my $ligne (@train){
chomp $ligne;
my @list = split('\t', $ligne);
my $user=$list[1];
my $label = $list[8];
my $text =&nettoyage($list[7]);
# if(!exists $seen{$text}){
# $seen{$text}=1;
my @zero = split(' ',$text);
foreach my $word (@zero){
$class_df{$word}{$label}++;
$df{$word}++;
}
$n_dial{$label}++;
$s_wis_2{$label}=0;
$n_dialogs++;
# }
}
undef @train;
foreach my $ligne (@test){
chomp $ligne;
my @list = split('\t', $ligne);
my $text =&nettoyage($list[7]);
# if(!exists $seen{$text}){
# $seen{$text}=1;
my @zero = split(' ',$text);
foreach my $word (@zero){
$df1{$word}++;
}
$n_dialogs++;
# }
}
foreach my $lab (keys (%n_dial)){
if($lab eq ""){
delete $n_dial{$lab};
}
}
print "reading done\n";
}
sub nettoyage {
my $text = $_[0];
my $temp="";
my $marker=0;
my @zero = split(' ',$text);
foreach my $word (@zero){
if((!exists $stoplist{$word}) && !($word=~ "http") && !($word=~ "pic.twitter.com") && length($word)>2){
$word=lc($word);
$word=~s/[ \-\_,\)\\(\\"\\&\;\...\«\»\.\!\?\*+:]/ /g;
$word=~s/[']/ /g;
$word=~s/[ÊÉÈËéèëê]/e/g;
$word=~s/[ÂÄÀàâä]/a/g;
$word=~s/[ÖÔôö]/o/g;
$word=~s/[ÛÜÙùûü]/u/g;
$word=~s/[ÎÏïî]/i/g;
$word=~s/[Çç]/c/g;
$word=~s/[ ]+//g;
if((!exists $stoplist{$word}) && !(length($word)==1) && !(length($word)==2)){
$temp.="$word ";
}
}
}
$text=$temp;
$text=~s/[ ]+/ /g;
return ($text);
}