Skip to content

Commit 562a389

Browse files
committed
add conlleval.pl
1 parent 238a0bc commit 562a389

File tree

2 files changed

+323
-16
lines changed

2 files changed

+323
-16
lines changed

code/conlleval.pl

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
#!/usr/bin/perl -w
2+
# conlleval: evaluate result of processing CoNLL-2000 shared task
3+
# usage: conlleval [-l] [-r] [-d delimiterTag] [-o oTag] < file
4+
# README: http://www.clips.uantwerpen.be/conll2000/chunking/output.html
5+
# options: l: generate LaTeX output for tables like in
6+
# http://cnts.uia.ac.be/conll2003/ner/example.tex
7+
# r: accept raw result tags (without B- and I- prefix;
8+
# assumes one word per chunk)
9+
# d: alternative delimiter tag (default is single space)
10+
# o: alternative outside tag (default is O)
11+
# note: the file should contain lines with items separated
12+
# by $delimiter characters (default space). The final
13+
# two items should contain the correct tag and the
14+
# guessed tag in that order. Sentences should be
15+
# separated from each other by empty lines or lines
16+
# with $boundary fields (default -X-).
17+
# url: http://www.clips.uantwerpen.be/conll2000/chunking/
18+
# started: 1998-09-25
19+
# version: 2004-01-26
20+
# author: Erik Tjong Kim Sang <[email protected]>
21+
#
22+
# with modifications by Grégoire Mesnil for Deep Learning Tutorials
23+
# https://github.com/lisa-lab/DeepLearningTutorials
24+
25+
use strict;
26+
27+
my $false = 0;
28+
my $true = 42;
29+
30+
my $boundary = "-X-"; # sentence boundary
31+
my $correct; # current corpus chunk tag (I,O,B)
32+
my $correctChunk = 0; # number of correctly identified chunks
33+
my $correctTags = 0; # number of correct chunk tags
34+
my $correctType; # type of current corpus chunk tag (NP,VP,etc.)
35+
my $delimiter = " "; # field delimiter
36+
my $FB1 = 0.0; # FB1 score (Van Rijsbergen 1979)
37+
my $firstItem; # first feature (for sentence boundary checks)
38+
my $foundCorrect = 0; # number of chunks in corpus
39+
my $foundGuessed = 0; # number of identified chunks
40+
my $guessed; # current guessed chunk tag
41+
my $guessedType; # type of current guessed chunk tag
42+
my $i; # miscellaneous counter
43+
my $inCorrect = $false; # currently processed chunk is correct until now
44+
my $lastCorrect = "O"; # previous chunk tag in corpus
45+
my $latex = 0; # generate LaTeX formatted output
46+
my $lastCorrectType = ""; # type of previously identified chunk tag
47+
my $lastGuessed = "O"; # previously identified chunk tag
48+
my $lastGuessedType = ""; # type of previous chunk tag in corpus
49+
my $lastType; # temporary storage for detecting duplicates
50+
my $line; # line
51+
my $nbrOfFeatures = -1; # number of features per line
52+
my $precision = 0.0; # precision score
53+
my $oTag = "O"; # outside tag, default O
54+
my $raw = 0; # raw input: add B to every token
55+
my $recall = 0.0; # recall score
56+
my $tokenCounter = 0; # token counter (ignores sentence breaks)
57+
58+
my %correctChunk = (); # number of correctly identified chunks per type
59+
my %foundCorrect = (); # number of chunks in corpus per type
60+
my %foundGuessed = (); # number of identified chunks per type
61+
62+
my @features; # features on line
63+
my @sortedTypes; # sorted list of chunk type names
64+
65+
# sanity check
66+
while (@ARGV and $ARGV[0] =~ /^-/) {
67+
if ($ARGV[0] eq "-l") { $latex = 1; shift(@ARGV); }
68+
elsif ($ARGV[0] eq "-r") { $raw = 1; shift(@ARGV); }
69+
elsif ($ARGV[0] eq "-d") {
70+
shift(@ARGV);
71+
if (not defined $ARGV[0]) {
72+
die "conlleval: -d requires delimiter character";
73+
}
74+
$delimiter = shift(@ARGV);
75+
} elsif ($ARGV[0] eq "-o") {
76+
shift(@ARGV);
77+
if (not defined $ARGV[0]) {
78+
die "conlleval: -o requires delimiter character";
79+
}
80+
$oTag = shift(@ARGV);
81+
} else { die "conlleval: unknown argument $ARGV[0]\n"; }
82+
}
83+
if (@ARGV) { die "conlleval: unexpected command line argument\n"; }
84+
# process input
85+
while (<STDIN>) {
86+
chomp($line = $_);
87+
@features = split(/$delimiter/,$line);
88+
if ($nbrOfFeatures < 0) { $nbrOfFeatures = $#features; }
89+
elsif ($nbrOfFeatures != $#features and @features != 0) {
90+
printf STDERR "unexpected number of features: %d (%d)\n",
91+
$#features+1,$nbrOfFeatures+1;
92+
exit(1);
93+
}
94+
if (@features == 0 or
95+
$features[0] eq $boundary) { @features = ($boundary,"O","O"); }
96+
if (@features < 2) {
97+
die "conlleval: unexpected number of features in line $line\n";
98+
}
99+
if ($raw) {
100+
if ($features[$#features] eq $oTag) { $features[$#features] = "O"; }
101+
if ($features[$#features-1] eq $oTag) { $features[$#features-1] = "O"; }
102+
if ($features[$#features] ne "O") {
103+
$features[$#features] = "B-$features[$#features]";
104+
}
105+
if ($features[$#features-1] ne "O") {
106+
$features[$#features-1] = "B-$features[$#features-1]";
107+
}
108+
}
109+
# 20040126 ET code which allows hyphens in the types
110+
if ($features[$#features] =~ /^([^-]*)-(.*)$/) {
111+
$guessed = $1;
112+
$guessedType = $2;
113+
} else {
114+
$guessed = $features[$#features];
115+
$guessedType = "";
116+
}
117+
pop(@features);
118+
if ($features[$#features] =~ /^([^-]*)-(.*)$/) {
119+
$correct = $1;
120+
$correctType = $2;
121+
} else {
122+
$correct = $features[$#features];
123+
$correctType = "";
124+
}
125+
pop(@features);
126+
# ($guessed,$guessedType) = split(/-/,pop(@features));
127+
# ($correct,$correctType) = split(/-/,pop(@features));
128+
$guessedType = $guessedType ? $guessedType : "";
129+
$correctType = $correctType ? $correctType : "";
130+
$firstItem = shift(@features);
131+
132+
# 1999-06-26 sentence breaks should always be counted as out of chunk
133+
if ( $firstItem eq $boundary ) { $guessed = "O"; }
134+
135+
if ($inCorrect) {
136+
if ( &endOfChunk($lastCorrect,$correct,$lastCorrectType,$correctType) and
137+
&endOfChunk($lastGuessed,$guessed,$lastGuessedType,$guessedType) and
138+
$lastGuessedType eq $lastCorrectType) {
139+
$inCorrect=$false;
140+
$correctChunk++;
141+
$correctChunk{$lastCorrectType} = $correctChunk{$lastCorrectType} ?
142+
$correctChunk{$lastCorrectType}+1 : 1;
143+
} elsif (
144+
&endOfChunk($lastCorrect,$correct,$lastCorrectType,$correctType) !=
145+
&endOfChunk($lastGuessed,$guessed,$lastGuessedType,$guessedType) or
146+
$guessedType ne $correctType ) {
147+
$inCorrect=$false;
148+
}
149+
}
150+
151+
if ( &startOfChunk($lastCorrect,$correct,$lastCorrectType,$correctType) and
152+
&startOfChunk($lastGuessed,$guessed,$lastGuessedType,$guessedType) and
153+
$guessedType eq $correctType) { $inCorrect = $true; }
154+
155+
if ( &startOfChunk($lastCorrect,$correct,$lastCorrectType,$correctType) ) {
156+
$foundCorrect++;
157+
$foundCorrect{$correctType} = $foundCorrect{$correctType} ?
158+
$foundCorrect{$correctType}+1 : 1;
159+
}
160+
if ( &startOfChunk($lastGuessed,$guessed,$lastGuessedType,$guessedType) ) {
161+
$foundGuessed++;
162+
$foundGuessed{$guessedType} = $foundGuessed{$guessedType} ?
163+
$foundGuessed{$guessedType}+1 : 1;
164+
}
165+
if ( $firstItem ne $boundary ) {
166+
if ( $correct eq $guessed and $guessedType eq $correctType ) {
167+
$correctTags++;
168+
}
169+
$tokenCounter++;
170+
}
171+
172+
$lastGuessed = $guessed;
173+
$lastCorrect = $correct;
174+
$lastGuessedType = $guessedType;
175+
$lastCorrectType = $correctType;
176+
}
177+
if ($inCorrect) {
178+
$correctChunk++;
179+
$correctChunk{$lastCorrectType} = $correctChunk{$lastCorrectType} ?
180+
$correctChunk{$lastCorrectType}+1 : 1;
181+
}
182+
183+
if (not $latex) {
184+
# compute overall precision, recall and FB1 (default values are 0.0)
185+
$precision = 100*$correctChunk/$foundGuessed if ($foundGuessed > 0);
186+
$recall = 100*$correctChunk/$foundCorrect if ($foundCorrect > 0);
187+
$FB1 = 2*$precision*$recall/($precision+$recall)
188+
if ($precision+$recall > 0);
189+
190+
# print overall performance
191+
printf "processed $tokenCounter tokens with $foundCorrect phrases; ";
192+
printf "found: $foundGuessed phrases; correct: $correctChunk.\n";
193+
if ($tokenCounter>0) {
194+
printf "accuracy: %6.2f%%; ",100*$correctTags/$tokenCounter;
195+
print "$correctChunk $foundCorrect $foundGuessed ";
196+
printf "precision: %6.2f%%; ",$precision;
197+
printf "recall: %6.2f%%; ",$recall;
198+
printf "FB1: %6.2f\n",$FB1;
199+
}
200+
}
201+
202+
# sort chunk type names
203+
undef($lastType);
204+
@sortedTypes = ();
205+
foreach $i (sort (keys %foundCorrect,keys %foundGuessed)) {
206+
if (not($lastType) or $lastType ne $i) {
207+
push(@sortedTypes,($i));
208+
}
209+
$lastType = $i;
210+
}
211+
# print performance per chunk type
212+
if (not $latex) {
213+
for $i (@sortedTypes) {
214+
$correctChunk{$i} = $correctChunk{$i} ? $correctChunk{$i} : 0;
215+
if (not($foundGuessed{$i})) { $foundGuessed{$i} = 0; $precision = 0.0; }
216+
else { $precision = 100*$correctChunk{$i}/$foundGuessed{$i}; }
217+
if (not($foundCorrect{$i})) { $recall = 0.0; }
218+
else { $recall = 100*$correctChunk{$i}/$foundCorrect{$i}; }
219+
if ($precision+$recall == 0.0) { $FB1 = 0.0; }
220+
else { $FB1 = 2*$precision*$recall/($precision+$recall); }
221+
printf "%17s: ",$i;
222+
printf "% 4d % 4d % 4d ", $correctChunk{$i}, $foundCorrect{$i}, $foundGuessed{$i};
223+
printf "precision: %6.2f%%; ",$precision;
224+
printf "recall: %6.2f%%; ",$recall;
225+
printf "FB1: %6.2f %d\n",$FB1,$foundGuessed{$i};
226+
}
227+
} else {
228+
print " & Precision & Recall & F\$_{\\beta=1} \\\\\\hline";
229+
for $i (@sortedTypes) {
230+
$correctChunk{$i} = $correctChunk{$i} ? $correctChunk{$i} : 0;
231+
if (not($foundGuessed{$i})) { $precision = 0.0; }
232+
else { $precision = 100*$correctChunk{$i}/$foundGuessed{$i}; }
233+
if (not($foundCorrect{$i})) { $recall = 0.0; }
234+
else { $recall = 100*$correctChunk{$i}/$foundCorrect{$i}; }
235+
if ($precision+$recall == 0.0) { $FB1 = 0.0; }
236+
else { $FB1 = 2*$precision*$recall/($precision+$recall); }
237+
printf "\n%-7s & %6.2f\\%% & %6.2f\\%% & %6.2f \\\\",
238+
$i,$precision,$recall,$FB1;
239+
}
240+
print "\\hline\n";
241+
$precision = 0.0;
242+
$recall = 0;
243+
$FB1 = 0.0;
244+
$precision = 100*$correctChunk/$foundGuessed if ($foundGuessed > 0);
245+
$recall = 100*$correctChunk/$foundCorrect if ($foundCorrect > 0);
246+
$FB1 = 2*$precision*$recall/($precision+$recall)
247+
if ($precision+$recall > 0);
248+
printf "Overall & %6.2f\\%% & %6.2f\\%% & %6.2f \\\\\\hline\n",
249+
$precision,$recall,$FB1;
250+
}
251+
252+
exit 0;
253+
254+
# endOfChunk: checks if a chunk ended between the previous and current word
255+
# arguments: previous and current chunk tags, previous and current types
256+
# note: this code is capable of handling other chunk representations
257+
# than the default CoNLL-2000 ones, see EACL'99 paper of Tjong
258+
# Kim Sang and Veenstra http://xxx.lanl.gov/abs/cs.CL/9907006
259+
260+
sub endOfChunk {
261+
my $prevTag = shift(@_);
262+
my $tag = shift(@_);
263+
my $prevType = shift(@_);
264+
my $type = shift(@_);
265+
my $chunkEnd = $false;
266+
267+
if ( $prevTag eq "B" and $tag eq "B" ) { $chunkEnd = $true; }
268+
if ( $prevTag eq "B" and $tag eq "O" ) { $chunkEnd = $true; }
269+
if ( $prevTag eq "I" and $tag eq "B" ) { $chunkEnd = $true; }
270+
if ( $prevTag eq "I" and $tag eq "O" ) { $chunkEnd = $true; }
271+
272+
if ( $prevTag eq "E" and $tag eq "E" ) { $chunkEnd = $true; }
273+
if ( $prevTag eq "E" and $tag eq "I" ) { $chunkEnd = $true; }
274+
if ( $prevTag eq "E" and $tag eq "O" ) { $chunkEnd = $true; }
275+
if ( $prevTag eq "I" and $tag eq "O" ) { $chunkEnd = $true; }
276+
277+
if ($prevTag ne "O" and $prevTag ne "." and $prevType ne $type) {
278+
$chunkEnd = $true;
279+
}
280+
281+
# corrected 1998-12-22: these chunks are assumed to have length 1
282+
if ( $prevTag eq "]" ) { $chunkEnd = $true; }
283+
if ( $prevTag eq "[" ) { $chunkEnd = $true; }
284+
285+
return($chunkEnd);
286+
}
287+
288+
# startOfChunk: checks if a chunk started between the previous and current word
289+
# arguments: previous and current chunk tags, previous and current types
290+
# note: this code is capable of handling other chunk representations
291+
# than the default CoNLL-2000 ones, see EACL'99 paper of Tjong
292+
# Kim Sang and Veenstra http://xxx.lanl.gov/abs/cs.CL/9907006
293+
294+
sub startOfChunk {
295+
my $prevTag = shift(@_);
296+
my $tag = shift(@_);
297+
my $prevType = shift(@_);
298+
my $type = shift(@_);
299+
my $chunkStart = $false;
300+
301+
if ( $prevTag eq "B" and $tag eq "B" ) { $chunkStart = $true; }
302+
if ( $prevTag eq "I" and $tag eq "B" ) { $chunkStart = $true; }
303+
if ( $prevTag eq "O" and $tag eq "B" ) { $chunkStart = $true; }
304+
if ( $prevTag eq "O" and $tag eq "I" ) { $chunkStart = $true; }
305+
306+
if ( $prevTag eq "E" and $tag eq "E" ) { $chunkStart = $true; }
307+
if ( $prevTag eq "E" and $tag eq "I" ) { $chunkStart = $true; }
308+
if ( $prevTag eq "O" and $tag eq "E" ) { $chunkStart = $true; }
309+
if ( $prevTag eq "O" and $tag eq "I" ) { $chunkStart = $true; }
310+
311+
if ($tag ne "O" and $tag ne "." and $prevType ne $type) {
312+
$chunkStart = $true;
313+
}
314+
315+
# corrected 1998-12-22: these chunks are assumed to have length 1
316+
if ( $tag eq "[" ) { $chunkStart = $true; }
317+
if ( $tag eq "]" ) { $chunkStart = $true; }
318+
319+
return($chunkStart);
320+
}

code/rnnslu.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,10 @@ def conlleval(p, g, w, filename, script_path):
107107

108108
return get_perf(filename, script_path)
109109

110-
111-
def download(origin, destination):
112-
'''
113-
download the corresponding atis file
114-
from http://www-etud.iro.umontreal.ca/~mesnilgr/atis/
115-
'''
116-
print('Downloading data from %s' % origin)
117-
urllib.urlretrieve(origin, destination)
118-
119-
120110
def get_perf(filename, folder):
121111
''' run conlleval.pl perl script to obtain
122112
precision/recall and F1 score '''
123113
_conlleval = os.path.join(folder, 'conlleval.pl')
124-
if not os.path.isfile(_conlleval):
125-
url = 'http://www-etud.iro.umontreal.ca/~mesnilgr/atis/conlleval.pl'
126-
download(url, _conlleval)
127-
os.chmod(_conlleval, stat.S_IRWXU) # give the execute permissions
128114

129115
proc = subprocess.Popen(["perl",
130116
_conlleval],
@@ -288,6 +274,7 @@ def main(param=None):
288274
folder = os.path.join(os.path.dirname(__file__), folder_name)
289275
if not os.path.exists(folder):
290276
os.mkdir(folder)
277+
script_path = os.path.dirname(__file__)
291278

292279
# load the dataset
293280
train_set, valid_set, test_set, dic = atisfold(param['fold'])
@@ -351,12 +338,12 @@ def main(param=None):
351338
groundtruth_test,
352339
words_test,
353340
folder + '/current.test.txt',
354-
folder)
341+
script_path)
355342
res_valid = conlleval(predictions_valid,
356343
groundtruth_valid,
357344
words_valid,
358345
folder + '/current.valid.txt',
359-
folder)
346+
script_path)
360347

361348
if res_valid['f1'] > best_f1:
362349

0 commit comments

Comments
 (0)