line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package TM::Ontology::KIF; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24425
|
use 5.008003; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
147
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = ( ); |
11
|
|
|
|
|
|
|
our @EXPORT = qw(); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
14
|
|
|
|
|
|
|
our $REVISION = '$Id: KIF.pm,v 1.1.1.1 2004/07/25 23:49:52 rho Exp $'; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
1262
|
use Data::Dumper; |
|
1
|
|
|
|
|
12024
|
|
|
1
|
|
|
|
|
249
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#-- KIF Grammar |
19
|
|
|
|
|
|
|
## |
20
|
|
|
|
|
|
|
my $grammar = q{ |
21
|
|
|
|
|
|
|
{ |
22
|
|
|
|
|
|
|
my $handlers; |
23
|
|
|
|
|
|
|
my $sentence_count = 0; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
startrule : { $handlers = $arg[0]; } kiffile |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
kiffile : result(s) |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
result : sentence { |
32
|
|
|
|
|
|
|
&{$handlers->{sentence}} ($item{sentence}); |
33
|
|
|
|
|
|
|
die "limit reached" if defined $handlers->{sentence_limit} && $sentence_count++ >= $handlers->{sentence_limit}; |
34
|
|
|
|
|
|
|
#warn Data::Dumper::Dumper ($item{sentence}); |
35
|
|
|
|
|
|
|
1; } |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sentence : '(' ( quantsent | logsent | relsent ) ')' { $return = $item[2]; } |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
quantsent : 'forall' '(' variable(s) ')' sentence { $return = [ 'forall', $item{'variable'}, $item{sentence} ]; } | |
40
|
|
|
|
|
|
|
'exists' '(' variable(s) ')' sentence { $return = [ 'exists', $item{'variable'}, $item{sentence} ]; } |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
logsent : 'not' sentence { $return = [ 'not', $item{sentence} ];} | |
43
|
|
|
|
|
|
|
'and' sentence(s) { $return = [ 'and', $item{sentence} ];} | |
44
|
|
|
|
|
|
|
'or' sentence(s) { $return = [ 'or', $item{sentence} ];} | |
45
|
|
|
|
|
|
|
'=>' sentence sentence { $return = [ '=>', $item[2], $item[3] ];} | |
46
|
|
|
|
|
|
|
'<=>' sentence sentence { $return = [ '<=>', $item[2], $item[3] ];} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
relsent : (word | variable ) term(s?) { $return = [ $item[1], $item{'term'} ];} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
term : variable | |
51
|
|
|
|
|
|
|
funterm | |
52
|
|
|
|
|
|
|
number | |
53
|
|
|
|
|
|
|
word | |
54
|
|
|
|
|
|
|
string | |
55
|
|
|
|
|
|
|
sentence | |
56
|
|
|
|
|
|
|
'<=>' | |
57
|
|
|
|
|
|
|
'=>' |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
funterm : '(' funword term(s) ')' { $return = [ $item{funword}, $item{'term'} ];} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
variable : /(\?|\@)[\w-]+/ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
word : /[a-zA-Z]+/ |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
funword : /\w+Fn/ |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
string : /"[^"]*"/ |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
number : /(\-)?\d+(\.\d+)?(e\-?\d)?/ |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=pod |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 NAME |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
TM::Ontology::KIF - Topic Map KIF Parser |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 SYNOPSIS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
use TM::Ontology::KIF; |
82
|
|
|
|
|
|
|
my $kif = new TM::Ontology::KIF (start_line_nr => 42, |
83
|
|
|
|
|
|
|
sentence_limit => 1000, |
84
|
|
|
|
|
|
|
sentence => sub { |
85
|
|
|
|
|
|
|
my $s = shift; |
86
|
|
|
|
|
|
|
print "got sentence "; |
87
|
|
|
|
|
|
|
.... |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
use IO::Handle; |
91
|
|
|
|
|
|
|
my $input = new IO::Handle; |
92
|
|
|
|
|
|
|
.... |
93
|
|
|
|
|
|
|
eval { |
94
|
|
|
|
|
|
|
$kif->parse ($input); |
95
|
|
|
|
|
|
|
}; warn $@ if $@; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This module provides KIF parsing functionality for IO::* streams. The concept |
101
|
|
|
|
|
|
|
is that the parser is reading a text stream and will invoke a subroutine which |
102
|
|
|
|
|
|
|
the calling application provided whenever a KIF sentence has been successfully |
103
|
|
|
|
|
|
|
parsed. (Similar to XML SAX processing). |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 Caveats |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=over |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item Compliance |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Currently, only a subset of the KIF syntax |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
http://logic.stanford.edu/kif/dpans.html |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
is supported, just enough to make the SUMO (IEEE) parse. Feel free to |
116
|
|
|
|
|
|
|
patch this module or bribe/contact me if you need more. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item Speed |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Currently I am using Parse::RecDescent underneath for parsing. While |
121
|
|
|
|
|
|
|
it is incredibly flexible and powerful, it is also dead slow. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=back |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 INTERFACE |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 Constructor |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
The constructor creates a new stream object. As parameters a hash can be provided |
130
|
|
|
|
|
|
|
whereby the following fields are recognized: |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=over |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item C: |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
If this is provided, then the value will be interpreted as subroutine reference. The subroutine |
137
|
|
|
|
|
|
|
will be executed every time a KIF sentence has been parsed whereby the sentence will be based as |
138
|
|
|
|
|
|
|
the only parameter. Otherwise, things will fail horribly. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item C: |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
If this is provided, then all lines will be skipped until this line number is reached. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item C |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
If this is present it limits the number of sentences which will be delivered back. |
147
|
|
|
|
|
|
|
When this limit is exceeded an exception will be raised. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=back |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub new { |
154
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
155
|
0
|
|
|
|
|
|
my %par = @_; |
156
|
0
|
|
0
|
0
|
|
|
$par{sentence} ||= sub { }; |
|
0
|
|
|
|
|
|
|
157
|
0
|
0
|
|
|
|
|
die "no subroutine reference" unless ref ($par{sentence}) eq 'CODE'; |
158
|
0
|
|
|
|
|
|
return bless { %par }, $class; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=pod |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 Methods |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=over |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item C |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This methods takes a text stream and tries to parse this according to KIF. Whenever |
170
|
|
|
|
|
|
|
particular portions of the input stream have been successfully parsed, they exist as |
171
|
|
|
|
|
|
|
an abstract trees and will be handed over to the handlers which have been setup in the |
172
|
|
|
|
|
|
|
stream constructor. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
1
|
|
|
1
|
|
1249
|
use IO::Handle; |
|
1
|
|
|
|
|
9050
|
|
|
1
|
|
|
|
|
177
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub parse { |
179
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
180
|
0
|
|
|
|
|
|
my $input = shift; |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
my $text; # we use Parse::RecDescent here, this one wants to have a string |
183
|
0
|
|
|
|
|
|
my $line_nr = 0; |
184
|
0
|
|
|
|
|
|
while (!$input->eof) { |
185
|
0
|
|
|
|
|
|
my $l = $input->getline; |
186
|
0
|
0
|
0
|
|
|
|
next if defined $self->{start_line_nr} && $line_nr++ < $self->{start_line_nr}; |
187
|
0
|
|
|
|
|
|
$l =~ s/^;.*?$//g; # remove comments here |
188
|
0
|
|
|
|
|
|
$text .= $l; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
1
|
|
|
1
|
|
2056
|
use Parse::RecDescent; |
|
1
|
|
|
|
|
61091
|
|
|
1
|
|
|
|
|
8
|
|
192
|
0
|
|
|
|
|
|
$::RD_HINT = 1; |
193
|
0
|
0
|
|
|
|
|
my $parser = new Parse::RecDescent ($grammar) or die "Problem in grammar"; |
194
|
0
|
0
|
|
|
|
|
$parser->startrule (\$text, 1, $self) or die "Error in parsing"; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=pod |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=back |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 AUTHOR |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Robert Barta, Erho@bigpond.net.auE |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Copyright (C) 2004 by Robert Barta |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
210
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.3 or, |
211
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=cut |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
1; |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
__END__ |