forked from ufal/hamledt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_ud12.pl
executable file
·181 lines (174 loc) · 6.58 KB
/
populate_ud12.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
#!/usr/bin/env perl
# Creates folders and Makefiles for Universal Dependencies 1.2.
# Copyright © 2015 Dan Zeman <[email protected]>
# License: GNU GPL
use utf8;
use open ':utf8';
binmode(STDIN, ':utf8');
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
my $udpath = '/net/work/people/zeman/unidep';
opendir(DIR, $udpath) or die("Cannot read the contents of $udpath: $!");
my @folders = sort(grep {-d "$udpath/$_" && m/^UD_[A-Z]/} (readdir(DIR)));
closedir(DIR);
print("Found ", scalar(@folders), " UD folders in $udpath.\n");
# We need a mapping from the English names of the languages (as they appear in folder names) to their ISO codes.
my %langcodes =
(
'Amharic' => 'am',
'Ancient_Greek' => 'grc',
'Arabic' => 'ar',
'Basque' => 'eu',
'Bulgarian' => 'bg',
'Catalan' => 'ca',
'Croatian' => 'hr',
'Czech' => 'cs',
'Danish' => 'da',
'Dutch' => 'nl',
'English' => 'en',
'Estonian' => 'et',
'Finnish' => 'fi',
'French' => 'fr',
'German' => 'de',
'Gothic' => 'got',
'Greek' => 'el',
'Hebrew' => 'he',
'Hindi' => 'hi',
'Hungarian' => 'hu',
'Indonesian' => 'id',
'Irish' => 'ga',
'Italian' => 'it',
'Japanese' => 'ja',
'Kazakh' => 'kk',
'Korean' => 'ko',
'Latin' => 'la',
'Norwegian' => 'no',
'Old_Church_Slavonic' => 'cu',
'Persian' => 'fa',
'Polish' => 'pl',
'Portuguese' => 'pt',
'Romanian' => 'ro',
'Russian' => 'ru',
'Slovak' => 'sk',
'Slovenian' => 'sl',
'Spanish' => 'es',
'Swedish' => 'sv',
'Tamil' => 'ta',
'Turkish' => 'tr'
);
foreach my $folder (@folders)
{
# The name of the folder: 'UD_' + language name + optional treebank identifier.
# Example: UD_Ancient_Greek-PROIEL
my $udname = '';
my $language = '';
my $treebank = '';
my $langcode;
if($folder =~ m/^UD_(([A-Za-z_]+)(?:-([A-Z]+))?)$/)
{
$udname = $1;
$language = $2;
$treebank = $3 if(defined($2));
if(exists($langcodes{$language}))
{
$langcode = $langcodes{$language};
# Read the README file first. We need to know whether this repository is scheduled for the upcoming release.
my $metadata = read_readme("$udpath/$folder");
if($metadata->{release})
{
# Look for the other files in the repository.
opendir(DIR, "$udpath/$folder") or die("Cannot read the contents of the folder $udpath/$folder");
my @files = readdir(DIR);
my @conllufiles = grep {-f "$udpath/$folder/$_" && m/\.conllu$/} (@files);
my $n = scalar(@conllufiles);
# Only process folders that are git repositories and contain CoNLL-U files.
if($n > 0 && -d "$udpath/$folder/.git")
{
my $lctreebank = lc($treebank);
my $key = $langcode;
$key .= '_'.$lctreebank if($treebank ne '');
my $hfolder = "$langcode-ud12$lctreebank";
print("$folder --> $hfolder\n");
if(1) # can be switched off for dry runs
{
my $hpath = "/net/work/people/zeman/hamledt/normalize/$hfolder";
system("mkdir -p $hpath");
my $makefile = <<EOF
LANGCODE=$langcode
TREEBANK=$hfolder
UDCODE=$key
UDNAME=$udname
include ../common.mak
SOURCEDIR=/net/work/people/zeman/unidep/UD_\$(UDNAME)
source:
cp \$(SOURCEDIR)/\$(UDCODE)-ud-train.conllu data/source/train.conllu
cp \$(SOURCEDIR)/\$(UDCODE)-ud-dev.conllu data/source/dev.conllu
cp \$(SOURCEDIR)/\$(UDCODE)-ud-test.conllu data/source/test.conllu
# Do not convert Universal Dependencies to the Prague style and then back to UD. Instead, read directly UD.
# Note that there will be just one tree per sentence, not three. (There are three trees per sentence for treebanks that are converted via Prague.)
ud: conllu_to_treex
EOF
;
open(MAKEFILE, ">$hpath/Makefile") or die("Cannot write to Makefile: $!");
print MAKEFILE ($makefile);
close(MAKEFILE);
system("cd $hpath ; git add Makefile ; make dirs ; make source ; make ud");
}
}
closedir(DIR);
}
}
}
}
#------------------------------------------------------------------------------
# Reads the README file of a treebank and finds the metadata lines. Example:
#=== Machine-readable metadata ================================================
#Documentation status: partial
#Data source: automatic
#Data available since: UD v1.2
#License: CC BY-NC-SA 2.5
#Genre: fiction
#Contributors: Celano, Giuseppe G. A.; Zeman, Daniel
#==============================================================================
#------------------------------------------------------------------------------
sub read_readme
{
my $folder = shift;
my $filename = (-f "$folder/README.txt") ? "$folder/README.txt" : "$folder/README.md";
open(README, $filename) or return;
binmode(README, ':utf8');
my %metadata;
my @attributes = ('Documentation status', 'Data source', 'Data available since', 'License', 'Genre', 'Contributors');
my $attributes_re = join('|', @attributes);
while(<README>)
{
s/\r?\n$//;
s/^\s+//;
s/\s+$//;
s/\s+/ /g;
if(m/^($attributes_re):\s*(.*)$/i)
{
my $attribute = $1;
my $value = $2;
$value = '' if(!defined($value));
if(exists($metadata{$attribute}))
{
print("WARNING: Repeated definition of '$attribute' in $folder/$filename\n");
}
$metadata{$attribute} = $value;
if($attribute eq 'Data available since')
{
if($metadata{$attribute} =~ m/^UD v1\.(\d)$/ && $1 <= 2)
{
$metadata{'release'} = 1;
}
}
}
elsif(m/change\s*log/i)
{
$metadata{'changelog'} = 1;
}
}
close(README);
return \%metadata;
}