-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge_lists.pl
executable file
·216 lines (189 loc) · 6.23 KB
/
merge_lists.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
my %counts;
my @id2title;
my $id2title_length = 0;
# Check if directory exists
my $directory = $ARGV[0] || "";
if (!-d $directory) {
print STDERR "Directory '$directory' does not exist. You have to call merge_lists.pl with as argument a directory path with the lists.\n";
exit 1;
}
# Open pages
my $pagesFile = "$directory/pages.tsv";
print STDERR "Getting higher page id from $pagesFile...\n";
open(FILE, '<', $pagesFile) or die("Unable to open file $pagesFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($pageId) = split("\t", $line);
$id2title_length = $pageId if $id2title_length <= $pageId
}
close(FILE);
$#id2title = $id2title_length;
print STDERR "Reading $pagesFile...\n";
open(FILE, '<', $pagesFile) or die("Unable to open file $pagesFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($pageId, $pageTitle, $pageSize, $isRedirect) = split("\t", $line);
$counts{$pageTitle}{"i"} = $pageId;
unless ($isRedirect) {
$counts{$pageTitle}{"s"} = $pageSize;
}
$id2title[$pageId] = $pageTitle;
}
close(FILE);
# Open pagelinks
my $pagelinksFile = "$directory/pagelinks.tsv";
print STDERR "Reading $pagelinksFile...\n";
open(FILE, '<', $pagelinksFile) or die("Unable to open file $pagelinksFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($sourcePageId, $targetPageTitle) = split("\t", $line);
$counts{$targetPageTitle}{"l"} = exists($counts{$targetPageTitle}{"l"}) ? $counts{$targetPageTitle}{"l"}+1 : 0;
}
close(FILE);
# Open langlinks
my $langlinksFile = "$directory/langlinks.tsv";
print STDERR "Reading $langlinksFile...\n";
open(FILE, '<', $langlinksFile) or die("Unable to open file $langlinksFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($pageTitle) = split("\t", $line);
next unless ($pageTitle);
$counts{$pageTitle}{"ll"} = exists($counts{$pageTitle}{"ll"}) ? $counts{$pageTitle}{"ll"}+1 : 0;
}
close(FILE);
# Open pageviews
my $pageviewsFile = "$directory/pageviews.tsv";
print STDERR "Reading $pageviewsFile...\n";
open(FILE, '<', $pageviewsFile) or die("Unable to open file $pageviewsFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($pageTitle, $pageviewCount) = split("\t", $line);
$counts{$pageTitle}{"v"} = $pageviewCount;
}
close(FILE);
# Open redirects
my $redirectsFile = "$directory/redirects.tsv";
print STDERR "Reading $redirectsFile...\n";
open(FILE, '<', $redirectsFile) or die("Unable to open file $redirectsFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($sourcePageId, $targetPageTitle) = split("\t", $line);
my $sourcePageTitle = $id2title[$sourcePageId];
if ($sourcePageTitle && exists($counts{$sourcePageTitle})) {
if (exists($counts{$targetPageTitle})) {
if ($counts{$sourcePageTitle}{"l"}) {
if ($counts{$targetPageTitle}{"l"}) {
$counts{$targetPageTitle}{"l"} += $counts{$sourcePageTitle}{"l"};
} else {
$counts{$targetPageTitle}{"l"} = $counts{$sourcePageTitle}{"l"};
}
}
if ($counts{$sourcePageTitle}{"ll"}) {
if ($counts{$targetPageTitle}{"ll"}) {
$counts{$targetPageTitle}{"ll"} += $counts{$sourcePageTitle}{"ll"};
} else {
$counts{$targetPageTitle}{"ll"} = $counts{$sourcePageTitle}{"ll"};
}
}
if ($counts{$sourcePageTitle}{"v"}) {
if ($counts{$targetPageTitle}{"v"}) {
$counts{$targetPageTitle}{"v"} += $counts{$sourcePageTitle}{"v"};
} else {
$counts{$targetPageTitle}{"v"} = $counts{$sourcePageTitle}{"v"};
}
}
}
delete($counts{$sourcePageTitle});
$id2title[$sourcePageId] = undef;
}
}
close(FILE);
# Free a bit of memory
@id2title = ();
$id2title_length = 0;
# Open ratings (if exists)
my %projects;
my %classes;
my %evals;
sub revert_hash {
my $hash = shift;
my %result;
while (my ($key, $value) = each %$hash) {
$result{$value}=$key;
}
\%result;
}
sub shorten_ratings {
my $ratings = shift;
my $result = "";
while ($ratings =~ m/(.+?)=([^:]+):([^\t]+)(\t|)/g) {
$result .= ($result ? "\t" : "") .
($projects{$1} //= scalar(keys(%projects))) . "=" .
($classes{$2} //= scalar(keys(%classes))) . ":" .
($evals{$3} //= scalar(keys(%evals)));
}
$result
}
sub expand_ratings {
my $ratings = shift;
my $result = "";
while ($ratings =~ m/(.+?)=([^:]+):([^\t]+)(\t|)/g) {
$result .= ($result ? "\t" : "") .
$projects{$1} . "=" .
$classes{$2} . ":" .
$evals{$3};
}
$result
}
my $ratingsFile = "$directory/ratings.tsv";
if (-f $ratingsFile) {
print STDERR "Reading $ratingsFile...\n";
open(FILE, '<', $ratingsFile) or die("Unable to open file $ratingsFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($pageTitle, $project, $quality, $importance) = split("\t", $line);
my $ratingEntry = shorten_ratings($project."=".$quality.":".$importance);
if (exists($counts{$pageTitle}{"r"})) {
$counts{$pageTitle}{"r"} .= ($counts{$pageTitle}{"r"} ? "\t" : "").$ratingEntry;
} else {
$counts{$pageTitle}{"r"} = $ratingEntry;
}
}
close(FILE);
}
%projects = %{revert_hash(\%projects)};
%classes = %{revert_hash(\%classes)};
%evals = %{revert_hash(\%evals)};
# Print counts
print STDERR "Printing all counts...\n";
open(FILE, '<', $pagesFile) or die("Unable to open file $pagesFile\n");
while(<FILE>) {
my $line = $_;
chomp($line);
my ($pageId, $pageTitle, $pageSize, $isRedirect) = split("\t", $line);
next if ($isRedirect);
print
$pageTitle."\t".
$pageId."\t".
($counts{$pageTitle}{"s"} || "0")."\t".
($counts{$pageTitle}{"l"} || "0")."\t".
($counts{$pageTitle}{"ll"} || "0")."\t".
($counts{$pageTitle}{"v"} || "0").
(exists($counts{$pageTitle}{"r"}) ? "\t".expand_ratings($counts{$pageTitle}{"r"}) : "").
"\n";
}
close(FILE);
# Exit
print STDERR "Finishing...\n";
exit 0;