line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::Treebank; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
86955
|
use 5.008; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
118
|
|
4
|
3
|
|
|
3
|
|
17
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
112
|
|
5
|
3
|
|
|
3
|
|
67
|
use warnings; |
|
3
|
|
|
|
|
81
|
|
|
3
|
|
|
|
|
124
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
################################################################## |
8
|
3
|
|
|
3
|
|
16
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
611
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @ISA = qw ( Exporter ) ; |
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw(); |
14
|
|
|
|
|
|
|
our @EXPORT = qw(); |
15
|
|
|
|
|
|
|
our $VERSION = '0.16'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $MAX_WARN_TEXT = 100; |
18
|
|
|
|
|
|
|
our $VERBOSE = 1; |
19
|
|
|
|
|
|
|
################################################################## |
20
|
3
|
|
|
3
|
|
2591
|
use Lingua::Treebank::Const; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
3249
|
|
21
|
|
|
|
|
|
|
our $CONST_CLASS = 'Lingua::Treebank::Const'; |
22
|
|
|
|
|
|
|
################################################################## |
23
|
|
|
|
|
|
|
sub from_penn_file { |
24
|
0
|
|
|
0
|
1
|
0
|
my ($class, $file) = @_; |
25
|
|
|
|
|
|
|
|
26
|
0
|
0
|
|
|
|
0
|
open (my $fh, "<$file") or die "couldn't open $file: $!\n"; |
27
|
0
|
|
|
|
|
0
|
my @results = $class->from_penn_fh($fh); |
28
|
0
|
0
|
|
|
|
0
|
close $fh or die "couldn't close $file: $!\n"; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
0
|
return @results; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
################################################################## |
33
|
|
|
|
|
|
|
sub from_penn_fh { |
34
|
2
|
|
|
2
|
1
|
56
|
my ($class, $fh) = @_; |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
5
|
my $rawTrees; |
37
|
|
|
|
|
|
|
|
38
|
2
|
50
|
|
|
|
20
|
if (not UNIVERSAL::isa($CONST_CLASS, 'Lingua::Treebank::Const')) { |
39
|
0
|
|
|
|
|
0
|
carp "CONST_CLASS value $CONST_CLASS", |
40
|
|
|
|
|
|
|
" doesn't seem to be a subclass of Lingua::Treebank::Const\n"; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
LINE: |
44
|
2
|
|
|
|
|
15
|
while (<$fh>) { |
45
|
|
|
|
|
|
|
|
46
|
28
|
|
|
|
|
33
|
chomp; # remove newlines |
47
|
|
|
|
|
|
|
|
48
|
28
|
100
|
66
|
|
|
98
|
if ( substr( $_, 0, 3 ) eq '*x*' |
49
|
|
|
|
|
|
|
or substr( $_, 0, 10 ) eq '=' x 10 ) |
50
|
|
|
|
|
|
|
{ |
51
|
|
|
|
|
|
|
# skip header copyright comments, bar of ==== |
52
|
24
|
|
|
|
|
62
|
next LINE; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
4
|
100
|
|
|
|
40
|
next if /^\s*$/; # skip entirely blank lines |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# slurp in the rest of the merge file all at once |
58
|
2
|
|
|
|
|
10
|
local $/; |
59
|
2
|
|
|
|
|
7
|
undef $/; |
60
|
2
|
|
|
|
|
226
|
$rawTrees = $_ . (<$fh>); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
5
|
my (@utterances); |
65
|
2
|
|
|
|
|
28
|
while ($rawTrees) { |
66
|
349
|
|
|
|
|
666
|
$rawTrees =~ s/^\s+//; |
67
|
349
|
|
|
|
|
1239
|
my $token = Lingua::Treebank::Const->find_brackets($rawTrees); |
68
|
|
|
|
|
|
|
|
69
|
349
|
50
|
|
|
|
709
|
if (defined $token) { |
70
|
349
|
|
|
|
|
535
|
substr ($rawTrees, 0, length $token) = ''; |
71
|
|
|
|
|
|
|
|
72
|
349
|
|
|
|
|
2679
|
$rawTrees =~ s/^\s+//; |
73
|
|
|
|
|
|
|
|
74
|
349
|
|
|
|
|
1098
|
my $utt = $CONST_CLASS->new->from_penn_string($token); |
75
|
349
|
50
|
|
|
|
733
|
if (defined $utt) { |
76
|
349
|
|
|
|
|
631
|
push @utterances, $utt; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
else { |
79
|
0
|
|
|
|
|
0
|
carp "couldn't parse '", cite_warning($token), |
80
|
|
|
|
|
|
|
"' remaining data '", cite_warning($rawTrees), |
81
|
|
|
|
|
|
|
"' in filehandle ignored"; |
82
|
0
|
|
|
|
|
0
|
last; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
|
|
|
|
|
|
# no token extractable |
87
|
0
|
|
|
|
|
0
|
carp "unrecognized data '", cite_warning($rawTrees), |
88
|
|
|
|
|
|
|
"' remaining in filehandle ignored"; |
89
|
0
|
|
|
|
|
0
|
last; |
90
|
|
|
|
|
|
|
} |
91
|
349
|
|
|
|
|
3719
|
$rawTrees =~ s/^\s*//; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
2
|
|
|
|
|
184
|
return @utterances; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub from_cnf_file { |
99
|
0
|
|
|
0
|
1
|
|
my ($class, $file) = @_; |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
open (my $fh, "<$file") or die "couldn't open $file: $!\n"; |
102
|
0
|
|
|
|
|
|
my @root_nodes = $class->from_cnf_fh($fh); |
103
|
0
|
0
|
|
|
|
|
close $fh or die "couldn't close $file: $!\n"; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
return @root_nodes; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# BUGBUG Should share code with from_penn_fh |
109
|
|
|
|
|
|
|
sub from_cnf_fh { |
110
|
0
|
|
|
0
|
1
|
|
my ($class, $fh) = @_; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my @root_nodes; |
113
|
|
|
|
|
|
|
LINE: |
114
|
0
|
|
|
|
|
|
while (<$fh>) { |
115
|
0
|
|
|
|
|
|
chomp; |
116
|
0
|
|
|
|
|
|
s/#.*$//; # Remove comments |
117
|
0
|
0
|
|
|
|
|
next LINE if (/^\s*$/); # Skip empty lines. |
118
|
0
|
0
|
|
|
|
|
next LINE if (/^$/); # Skip sentence annotation used by |
119
|
|
|
|
|
|
|
# the Structured Language Model. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
NODE: |
122
|
0
|
|
|
|
|
|
while (length $_) { |
123
|
0
|
|
|
|
|
|
my $text = Lingua::Treebank::Const->find_brackets($_); |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
if (length $text) { |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Remove the matched constituent from the remaining |
128
|
|
|
|
|
|
|
# text. |
129
|
0
|
|
|
|
|
|
substr ($_, 0, length $text) = ''; |
130
|
0
|
|
|
|
|
|
s/^\s+//; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# The bracketed text is a CNF treebank constituent. |
133
|
0
|
|
|
|
|
|
my Lingua::Treebank::Const $node = |
134
|
|
|
|
|
|
|
Lingua::Treebank::Const->new->from_cnf_string($text); |
135
|
|
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
if (not defined $node) { |
137
|
0
|
|
|
|
|
|
warn "couldn't parse '$text', remaining data '$_; in line $.filehandle ignored"; |
138
|
0
|
|
|
|
|
|
last NODE; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
push @root_nodes, $node; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
else { |
144
|
|
|
|
|
|
|
# No token extractable. |
145
|
0
|
|
|
|
|
|
warn "unrecognized data '$_', remaining in line $. ignored\n"; |
146
|
0
|
|
|
|
|
|
last NODE; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
return @root_nodes; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
################################################################## |
158
|
|
|
|
|
|
|
sub cite_warning { |
159
|
0
|
|
|
0
|
0
|
|
my $text = shift; |
160
|
0
|
|
|
|
|
|
my $warning; |
161
|
0
|
0
|
|
|
|
|
if (length $text > $MAX_WARN_TEXT) { |
162
|
0
|
|
|
|
|
|
$warning = |
163
|
|
|
|
|
|
|
substr($text, 0, $MAX_WARN_TEXT / 2); |
164
|
0
|
|
|
|
|
|
$warning .= ' [ ... OMITTED ... ] '; |
165
|
0
|
|
|
|
|
|
$warning .= |
166
|
|
|
|
|
|
|
substr($text, -($MAX_WARN_TEXT / 2) ); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
else { |
169
|
0
|
|
|
|
|
|
$warning = $text; |
170
|
|
|
|
|
|
|
} |
171
|
0
|
|
|
|
|
|
return $warning; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
################################################################## |
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
__END__ |