-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtractWikipediaPages.pl
138 lines (127 loc) · 3.04 KB
/
ExtractWikipediaPages.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
use HTML::Entities;
my $input = shift;
my $sourceLang = shift;
my $targetLang = shift;
my $outputFolder = shift;
my $initOutputFolder = $outputFolder;
mkdir $outputFolder unless (-d $outputFolder);
sub writeContent {
my $content = shift;
#$content = decode_entities($content);
my $output = shift;
print $output . "\n";
open OUTPUT, ">:utf8", $output or die "Cannot open file $output: $!\n";
print OUTPUT $content;
close OUTPUT;
}
sub findLangLinks {
my $id = shift;
my $title = shift;
my $content = shift;
my $targetlang = shift;
my $link = "";
if ($content =~ m/\[\[$targetLang:(.*)\]\]/) {
$link = $1;
}
if ($link ne "") {
my $alignmentFile = $initOutputFolder . "alignment_$sourceLang\_$targetLang.txt";
open OUTPUT, ">>:utf8", $alignmentFile or die "Cannot open file $alignmentFile: $!\n";
print OUTPUT "$id\t$title\t$link\n";
close OUTPUT;
}
}
sub normalisePath {
my $path = shift;
if ($^O eq "MSWin32") {
$path =~ s/[\\\/]+/\\/g;
$path =~ s/[\\\/]*$/\\/;
}
else {
$path =~ s/[\\\/]+/\//g;
$path =~ s/[\\\/]*$/\//;
}
return $path;
}
my $alignmentFile = $initOutputFolder . "alignment_$sourceLang\_$targetLang.txt";
open OUTPUT, ">:utf8", $alignmentFile or die "Cannot open file $alignmentFile: $!\n";
print OUTPUT "";
close OUTPUT;
$outputFolder .= "$sourceLang/";
#$outputFolder =~ s/[\/\\]+$/\/$sourceLang\//g;
$outputFolder = normalisePath($outputFolder);
mkdir $outputFolder unless (-d $outputFolder);
my $outputFile = "";
open INPUT, "<:encoding(utf-8)", $input or die "Cannot open file $input: $!\n";
my $i = 0;
my $pageFlag = 0;
my $title = "";
my $id = 0;
my $content = "";
while (<INPUT>) {
my $sentence = decode_entities($_);
chomp($sentence);
if ($sentence =~ m/<page>/) {
$pageFlag = 1;
}
elsif ($sentence =~ m/<text xml:space=\"preserve\">(.*)<\/text>$/) {
$content .= $1 . "\n";
if ($content =~ m/\#REDIRECT/i) {
}
else {
#print "Content:\n$content\n";
#print "----------------------------------------------------------\n";
if (-e $output) {
#print "File $output exists.\n";
}
else {
&writeContent($content, $outputFile);
}
&findLangLinks($id, $title, $content, $targetLang);
}
$content = "";
$contentFlag = 0;
$title = "";
$id = "";
}
elsif ($sentence =~ m/<text xml:space=\"preserve\">(.*)$/) {
$contentFlag = 1;
$content .= $1 . "\n";
next;
}
if ($pageFlag == 1) {
if ($sentence =~ m/<title>(.*)<\/title>/) {
$title = $1;
}
elsif ($sentence =~ m/<id>(.*)<\/id>/) {
$id = $1;
$outputFile = $outputFolder . "$id\_$sourceLang.txt";
$pageFlag = 0;
}
}
if ($contentFlag == 1) {
if ($sentence =~ /^(.*)<\/text>$/) {
$content .= $1 . "\n";
if ($content =~ m/\#REDIRECT/) {
}
elsif ($title =~ m/:[^ ]/) {
}
else {
if (-e $output) {
#print "File $output exists.\n";
}
else {
&writeContent($content, $outputFile);
}
&findLangLinks($id, $title, $content, $targetLang);
}
$content = "";
$contentFlag = 0;
$title = "";
$id = "";
}
else {
$content .= $sentence . "\n";
}
}
}
close INPUT;