-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsurvey_misc.pl
executable file
·208 lines (203 loc) · 6.91 KB
/
survey_misc.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
#!/usr/bin/env perl
# Scans all UD treebanks for language-specific features and values.
# Copyright © 2016-2018, 2020 Dan Zeman <[email protected]>
# License: GNU GPL
use utf8;
use open ':utf8';
binmode(STDIN, ':utf8');
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
use Getopt::Long;
# We need to tell Perl where to find my UD and graph modules.
BEGIN
{
use Cwd;
my $path = $0;
my $currentpath = getcwd();
$currentpath =~ s/\r?\n$//;
$libpath = $currentpath;
if($path =~ m:/:)
{
$path =~ s:/[^/]*$:/:;
chdir($path);
$libpath = getcwd();
chdir($currentpath);
}
$libpath =~ s/\r?\n$//;
#print STDERR ("libpath=$libpath\n");
}
use lib $libpath;
use udlib;
sub usage
{
print STDERR ("Usage: perl survey_features.pl --datapath /net/projects/ud --tbklist udsubset.txt\n");
print STDERR (" --datapath ... path to the folder where all UD_* treebank repositories reside\n");
print STDERR (" --tbklist .... file with list of UD_* folders to consider (e.g. treebanks we are about to release)\n");
print STDERR (" if tbklist is not present, all treebanks in datapath will be scanned\n");
}
my $datapath = '.';
my $tbklist;
GetOptions
(
'datapath=s' => \$datapath, # UD_* folders will be sought in this folder
'tbklist=s' => \$tbklist # path to file with treebank list; if defined, only treebanks on the list will be surveyed
);
my %treebanks;
if(defined($tbklist))
{
open(TBKLIST, $tbklist) or die("Cannot read treebank list from '$tbklist': $!");
while(<TBKLIST>)
{
s/^\s+//;
s/\s+$//;
my @treebanks = split(/\s+/, $_);
foreach my $t (@treebanks)
{
$t =~ s:/$::;
$treebanks{$t}++;
}
}
close(TBKLIST);
}
opendir(DIR, $datapath) or die("Cannot read the contents of '$datapath': $!");
my @folders = sort(grep {-d "$datapath/$_" && m/^UD_[A-Z]/} (readdir(DIR)));
closedir(DIR);
my $n = scalar(@folders);
print STDERR ("Found $n UD folders in '$datapath'.\n");
if(defined($tbklist))
{
my $n = scalar(keys(%treebanks));
print STDERR ("We will only scan those listed in $tbklist (the list contains $n treebanks but we have not checked yet which of them exist in the folder).\n");
}
else
{
print STDERR ("Warning: We will scan them all, whether their data is valid or not!\n");
}
if($datapath eq '.')
{
print STDERR ("Use the --datapath option to scan a different folder with UD treebanks.\n");
}
sleep(5);
# We need a mapping from the English names of the languages (as they appear in folder names) to their ISO codes.
# There is now also the new list of languages in YAML in docs-automation; this one has also language families.
my $languages_from_yaml = udlib::get_language_hash("$libpath/../docs-automation/codes_and_flags.yaml");
my %langnames;
my %langcodes;
foreach my $language (keys(%{$languages_from_yaml}))
{
# We need a mapping from language names in folder names (contain underscores instead of spaces) to language codes.
# Language names in the YAML file may contain spaces (not underscores).
my $usname = $language;
$usname =~ s/ /_/g;
$langcodes{$usname} = $languages_from_yaml->{$language}{lcode};
$langnames{$languages_from_yaml->{$language}{lcode}} = $language;
}
# Look for features in the data.
my %hash;
my %hittreebanks;
foreach my $folder (@folders)
{
# If we received the list of treebanks to be released, skip all other treebanks.
if(defined($tbklist) && !exists($treebanks{$folder}))
{
next;
}
# The name of the folder: 'UD_' + language name + optional treebank identifier.
# Example: UD_Ancient_Greek-PROIEL
my $language = '';
my $treebank = '';
my $langcode;
my $key;
if($folder =~ m/^UD_([A-Za-z_]+)(?:-([A-Za-z]+))?$/)
{
print STDERR ("$folder\n");
$language = $1;
$treebank = $2 if(defined($2));
if(exists($langcodes{$language}))
{
$langcode = $langcodes{$language};
$key = $langcode;
$key .= '_'.lc($treebank) if($treebank ne '');
my $nhits = 0;
# Look for the other files in the repository.
opendir(DIR, "$datapath/$folder") or die("Cannot read the contents of the folder '$datapath/$folder': $!");
my @files = readdir(DIR);
closedir(DIR);
my @conllufiles = grep {-f "$datapath/$folder/$_" && m/\.conllu$/} (@files);
foreach my $file (@conllufiles)
{
# Read the file and look for language-specific subtypes in the DEPREL column.
# We currently do not look for additional types in the DEPS column.
open(FILE, "$datapath/$folder/$file") or die("Cannot read '$datapath/$folder/$file': $!");
while(<FILE>)
{
if(m/^\d+\t/)
{
chomp();
my @fields = split(/\t/, $_);
my $features = $fields[9];
unless($features eq '_')
{
my @features = split(/\|/, $features);
foreach my $feature (@features)
{
my ($f, $v);
if($feature =~ m/^(.+?)=(.+)$/)
{
$f = $1;
$v = $2;
}
else
{
$f = '_';
$v = $feature;
}
$hash{$f}{$v}{$key}++;
$nhits++;
}
}
}
}
}
# Remember treebanks where we found something.
if($nhits>0)
{
$hittreebanks{$key}++;
}
}
}
}
my @features = sort(keys(%hash));
print <<EOF
---
layout: base
title: 'Miscellaneous Attributes'
udver: '2'
---
This is an automatically generated list of attributes that occur in the MISC column in the UD data.
EOF
;
foreach my $f (@features)
{
my %ffolders;
my @values = sort(keys(%{$hash{$f}}));
# Some MISC attributes (such as Translit) have very large inventories of values.
# Truncate the list. It would not be helpful to see all the values.
if(scalar(@values) > 50)
{
splice(@values, 50);
push(@values, {'...' => {'' => ''}});
}
print("\#\# $f\n\n");
print("[$f]()\n\n");
foreach my $v (@values)
{
my @folders = sort(keys(%{$hash{$f}{$v}}));
foreach my $folder (@folders)
{
print("* $f=$v\t$folder\t$hash{$f}{$v}{$folder}\n");
$ffolders{$folder}++;
}
}
print("\n");
}