| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # Copyright (c) 2000 Josiah Bryan USA | ||
| 4 | # | ||
| 5 | # See AUTHOR section in pod text below for usage and distribution rights. | ||
| 6 | # | ||
| 7 | |||
| 8 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | $Lingua::EN::Dict::VERSION = "0.20"; | |
| 10 | 1 | $Lingua::EN::Dict::ID = | |
| 11 | '$Id: Lingua::EN::Dict.pm, v'.$Lingua::EN::Dict::VERSION.' 2000/06/10 24:07:16 josiah Exp $'; | ||
| 12 | } | ||
| 13 | |||
| 14 | package Lingua::EN::Dict; | ||
| 15 | |||
| 16 | @ISA = qw(Exporter); | ||
| 17 | @EXPORT = qw(daemon); | ||
| 18 | |||
| 19 | 0 | ▶ | use XML::Parser; # Parses the XML dictionary file |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | use Data::Dumper; # Used to store extra data in XML and for TCP | ||
| 21 | use IO::Handle; # Just needed for the autoflush() function | ||
| 22 | use Socket; # Ummmm...gee, what is this for? :) | ||
| 23 | use strict; # Make sure I behave | ||
| 24 | |||
| 25 | BEGIN { | ||
| 26 | eval('use Coy'); # Try to make it pretty | ||
| 27 | eval('use Carp') if $@; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub EOL{"\015\012"} | ||
| 31 | sub flush { my $a = shift; autoflush $a, 1 }; | ||
| 32 | sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } | ||
| 33 | sub daemon { | ||
| 34 | my $port = shift || 7778; | ||
| 35 | my $file = shift || 'words.xml'; | ||
| 36 | my $inad = shift || INADDR_ANY; | ||
| 37 | my $dict = Lingua::EN::Dict->new(file=>$file,warn=>1); | ||
| 38 | socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "socket: $!"; | ||
| 39 | setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!"; | ||
| 40 | bind(SERVER, sockaddr_in($port, $inad)) || die "bind: $!"; | ||
| 41 | listen(SERVER,SOMAXCONN) || die "listen: $!"; | ||
| 42 | logmsg "server started on port $port"; | ||
| 43 | my $paddr; | ||
| 44 | for (; $paddr = accept(CLIENT,SERVER); close Client) { | ||
| 45 | my($port,$iaddr) = sockaddr_in($paddr); | ||
| 46 | my $name = gethostbyaddr($iaddr,AF_INET); | ||
| 47 | logmsg "connection from $name [",inet_ntoa($iaddr),"] | ||
| 48 | at port $port"; | ||
| 49 | RXLOOP: | ||
| 50 | $_=<CLIENT>; chomp; chomp; | ||
| 51 | logmsg "received command $_ | ||
| 52 | at port $port"; | ||
| 53 | /^DICT-PING/ && do { | ||
| 54 | print CLIENT "DICT-PING: Ping? Pong! : You are: $name [",inet_ntoa($iaddr),"] at port $port", EOL; | ||
| 55 | flush *CLIENT; | ||
| 56 | goto RXLOOP; | ||
| 57 | }; | ||
| 58 | /^DICT-QUERY/ && do { | ||
| 59 | /^(DICT-QUERY:)([\w]+)/; | ||
| 60 | my $dump=Dumper($dict->{$2}); $dump=~s/\$VAR1 = //g; | ||
| 61 | print CLIENT $dump,EOL; | ||
| 62 | flush *CLIENT; | ||
| 63 | goto RXLOOP; | ||
| 64 | }; | ||
| 65 | /^DICT-KEYS/ && do { | ||
| 66 | my @keys = keys %{$dict}; | ||
| 67 | print CLIENT '[\''.join('\',\'',@keys).'\']', EOL; | ||
| 68 | flush *CLIENT; | ||
| 69 | goto RXLOOP; | ||
| 70 | }; | ||
| 71 | /^DICT-TENSE/ && do { | ||
| 72 | /^(DICT-TENSE:)([\w]+)/; | ||
| 73 | print CLIENT $dict->tense($2), EOL; | ||
| 74 | flush *CLIENT; | ||
| 75 | goto RXLOOP; | ||
| 76 | }; | ||
| 77 | /^DICT-TYPES/ && do { | ||
| 78 | /^(DICT-TYPES:)([\w]+)/; | ||
| 79 | my $type = $2;$type =~ s/\r//g;$type =~ s/\n//g; | ||
| 80 | print CLIENT '[\''.join('\',\'',@{$dict->types($type)}).'\']',EOL; | ||
| 81 | flush *CLIENT; | ||
| 82 | goto RXLOOP; | ||
| 83 | }; | ||
| 84 | /^DICT-SET/ && do { | ||
| 85 | /^(DICT-SET:)([\w]+)/; | ||
| 86 | my $word = $2;$word =~ s/\r//g;$word =~ s/\n//g; | ||
| 87 | s/^(DICT-SET:$word)//g; | ||
| 88 | $dict->{$word} = eval; | ||
| 89 | $dict->{$word}->{modified_by} = { name => $name, ip => inet_ntoa($iaddr) } | ||
| 90 | if(inet_ntoa($iaddr) ne '127.0.0.1'); | ||
| 91 | }; | ||
| 92 | /^DICT-WRITEOUT/ && do { | ||
| 93 | $dict->save; | ||
| 94 | }; | ||
| 95 | /^DICT-CLOSE/ && do { | ||
| 96 | next | ||
| 97 | }; | ||
| 98 | !/^DICT/ && do { | ||
| 99 | print CLIENT "Erroneous command: $_",EOL; | ||
| 100 | flush *CLIENT; | ||
| 101 | next | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | my $word_hash={}; | ||
| 107 | |||
| 108 | sub new { | ||
| 109 | my $type = shift; | ||
| 110 | my %args = @_; | ||
| 111 | my $file = $args{file} || 'words.xml'; | ||
| 112 | my $self = {}; | ||
| 113 | $word_hash=$self; | ||
| 114 | $self->{_}->{'.file'}=$file; | ||
| 115 | $self->{_}->{'.changed'} = 0; | ||
| 116 | $self = bless $self,$type; | ||
| 117 | my ($remote,$port, $iaddr, $paddr, $proto, $line, $flag, $sync); | ||
| 118 | $flag = 1; | ||
| 119 | $sync = defined($args{$sync})?$args{$sync}:0; | ||
| 120 | $remote = $args{server} || 'localhost'; | ||
| 121 | $port = $args{port} || 7778; | ||
| 122 | if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } | ||
| 123 | die "No port" unless $port; | ||
| 124 | $iaddr = inet_aton($remote) || ($flag = 0); | ||
| 125 | $paddr = sockaddr_in($port, $iaddr); | ||
| 126 | $proto = getprotobyname('tcp'); | ||
| 127 | socket(SERVER, PF_INET, SOCK_STREAM, $proto) || ($flag = 0); | ||
| 128 | my $server = *SERVER; | ||
| 129 | undef $self->{_}->{'.server'}; | ||
| 130 | undef $self->{_}->{'.sync'}; | ||
| 131 | if($args{file} && !$args{server}) { | ||
| 132 | $flag = 0; | ||
| 133 | undef $server; | ||
| 134 | } | ||
| 135 | if($flag) { | ||
| 136 | if(connect($server, $paddr)) { | ||
| 137 | print $server "DICT-PING",EOL; flush $server; | ||
| 138 | my $rx = <SERVER>; | ||
| 139 | if($rx =~ /^DICT-PING/) { | ||
| 140 | $self->{_}->{'.server'} = $server; | ||
| 141 | $self->sync() if $sync; | ||
| 142 | } else { | ||
| 143 | close($server); | ||
| 144 | warn "did not receive expected response from server [$remote] at port [$port]" if(!$args{warn_off}); | ||
| 145 | undef $server; | ||
| 146 | } | ||
| 147 | } else { | ||
| 148 | warn "cannot connect to server [$remote] at port [$port]" if(!$args{warn_off}); | ||
| 149 | undef $server; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | if(!$server) { | ||
| 153 | if(-f $file && -s $file) { | ||
| 154 | $self->{_}->{'.sync'} = 1; | ||
| 155 | XML::Parser->new(Handlers=>{Start => \&tag_start, | ||
| 156 | End => \&tag_end, | ||
| 157 | Char => \&tag_char}) | ||
| 158 | -> parsefile($file); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | $self; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub DESTROY { | ||
| 165 | my $server = shift->{_}->{'.server'}; | ||
| 166 | print ($server "DICT-CLOSE", EOL), flush $server if $server; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub save { | ||
| 170 | my ($word,$info); | ||
| 171 | my $word_hash = shift; | ||
| 172 | my $file = $_[0] || $word_hash->{_}->{'.file'}; | ||
| 173 | return undef if(!$word_hash->{_}->{'.changed'} || !$self->{_}->{'.synced_down'}); | ||
| 174 | if($word_hash->{_}->{'.server'} && !$_[0]) { | ||
| 175 | $word_hash->sync('up'); | ||
| 176 | return $word_hash; | ||
| 177 | } | ||
| 178 | my @keys = sort keys %{$word_hash}; | ||
| 179 | open FILE, ">$file"; | ||
| 180 | print FILE "<?xml version='1.0'?>\n<dictionary>\n"; | ||
| 181 | foreach my $word (@keys) { | ||
| 182 | next if $word eq '_'; | ||
| 183 | $info=$word_hash->{$word}; | ||
| 184 | print FILE "\t<record>\n\t\t<word>$word</word>\n\t\t<type>$info->{type}</type>\n"; | ||
| 185 | if($info->{type} eq 'modal') { | ||
| 186 | print FILE "\t\t<modal_type>$info->{modal_type}</modal_type>\n"; | ||
| 187 | } | ||
| 188 | if($info->{type} eq 'verb') { | ||
| 189 | print FILE "\t\t<forms>\n\t\t\t<third>$info->{third}</third>\n\t\t\t". | ||
| 190 | "<past>$info->{past}</past>\n\t\t\t". | ||
| 191 | "<part>$info->{part}</part>\n\t\t\t". | ||
| 192 | "<gerund>$info->{gerund}</gerund>\n". | ||
| 193 | "\t\t</forms>\n"; | ||
| 194 | } | ||
| 195 | if($info->{links}) { | ||
| 196 | foreach my $link (@{$info->{links}}) { | ||
| 197 | print FILE "\t\t<link relation='$link->{relation}'>$link->{word}</link>\n"; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | if($info->{defn}) { | ||
| 201 | print FILE "\t\t<defn>$info->{defn}</defn>\n"; | ||
| 202 | } | ||
| 203 | my %clone = %{$info}; | ||
| 204 | delete $clone{links}; delete $clone{defn}; | ||
| 205 | delete $clone{third}; delete $clone{past}; | ||
| 206 | delete $clone{modal}; delete $clone{gerund}; | ||
| 207 | delete $clone{part}; delete $clone{modal_type}; | ||
| 208 | delete $clone{type}; | ||
| 209 | if(keys %clone) { | ||
| 210 | my $dump = "\n\t".Dumper(\%clone); | ||
| 211 | $dump=~s/\n/\n\t\t/g;$dump=~s/\$VAR1 = //g;$dump=~s/\;\n//g; | ||
| 212 | print FILE "\t\t<extra>$dump\n\t\t</extra>\n"; | ||
| 213 | } | ||
| 214 | print FILE "\t</record>\n"; | ||
| 215 | } | ||
| 216 | print FILE "</dictionary>"; | ||
| 217 | close FILE; | ||
| 218 | $word_hash->{_}->{'.changed'} = 0; | ||
| 219 | return $word_hash; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub tag_start { | ||
| 223 | my ($x,$el) = (shift,shift); | ||
| 224 | my ($a,$b) = (shift,shift); | ||
| 225 | $word_hash->{_}->{'.string.tmp'}->{$el} = ''; | ||
| 226 | $word_hash->{_}->{'.current.el'} = $el; | ||
| 227 | $word_hash->{_}->{'.ab'}->{$el} = {$a=>$b}; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub tag_end { | ||
| 231 | my ($x,$el) = (shift,shift); | ||
| 232 | if($el eq 'word') { | ||
| 233 | $word_hash->{ | ||
| 234 | $word_hash->{_}->{'.string.tmp'}->{$el} | ||
| 235 | } = {}; | ||
| 236 | $word_hash->{_}->{'.current.word'} = $word_hash->{_}->{'.string.tmp'}->{$el}; | ||
| 237 | $word_hash->{_}->{'.string.tmp'}->{$el} = ''; | ||
| 238 | } | ||
| 239 | if($el eq 'type') { | ||
| 240 | $word_hash->{ | ||
| 241 | $word_hash->{_}->{'.current.word'} | ||
| 242 | }->{type} = | ||
| 243 | $word_hash->{_}->{'.string.tmp'}->{$el}; | ||
| 244 | } | ||
| 245 | if($el eq 'modal_type' && $word_hash->{ | ||
| 246 | $word_hash->{_}->{'.current.word'} | ||
| 247 | }->{type} eq 'modal') { | ||
| 248 | $word_hash->{$word_hash->{_}->{'.current.word'}}->{modal} = | ||
| 249 | $word_hash->{_}->{'.string.tmp'}->{$el}; | ||
| 250 | } | ||
| 251 | if(($el eq 'third' || $el eq 'past' || $el eq 'part' || $el eq 'gerund') | ||
| 252 | && $word_hash->{ | ||
| 253 | $word_hash->{_}->{'.current.word'} | ||
| 254 | }->{type} eq 'verb') { | ||
| 255 | $word_hash->{$word_hash->{_}->{'.current.word'}}->{$el} = | ||
| 256 | $word_hash->{_}->{'.string.tmp'}->{$el}; | ||
| 257 | } | ||
| 258 | if($el eq 'link') { | ||
| 259 | push @{$word_hash->{$word_hash->{_}->{'.current.word'}}->{'links'}}, { | ||
| 260 | word => $word_hash->{_}->{'.string.tmp'}->{$el}, | ||
| 261 | relation => $word_hash->{_}->{'.ab'}->{$el}->{relation} | ||
| 262 | }; | ||
| 263 | } | ||
| 264 | if($el eq 'extra') { | ||
| 265 | my $tmp = eval($word_hash->{_}->{'.string.tmp'}->{$el}); | ||
| 266 | print STDERR $@ if $@; | ||
| 267 | my @keys = keys %{$tmp}; | ||
| 268 | foreach my $key (@keys) { | ||
| 269 | $word_hash->{$word_hash->{_}->{'.current.word'}}->{$key} = $tmp->{$key}; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | if($el eq 'defn') { | ||
| 273 | $word_hash->{$word_hash->{_}->{'.current.word'}}->{defn} = $word_hash->{_}->{'.string.tmp'}->{$el}; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | sub tag_char { | ||
| 278 | shift; $word_hash->{_}->{'.string.tmp'}->{$word_hash->{_}->{'.current.el'}}.=shift; | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | sub types { | ||
| 283 | my $word_hash = shift; | ||
| 284 | my $type = shift; | ||
| 285 | my $server = $word_hash->{_}->{'.server'}; | ||
| 286 | if($server) { | ||
| 287 | print $server "DICT-TYPES:$type", EOL; flush $server; | ||
| 288 | my $str=<SERVER>; | ||
| 289 | my $return = eval($str); | ||
| 290 | warn "Error in database server query: $@\n" if $@; | ||
| 291 | return $return; | ||
| 292 | } | ||
| 293 | my (@words,$word,$info); | ||
| 294 | while(($word,$info) = each %{$word_hash}) { | ||
| 295 | push(@words, $word) if $info->{type} eq $type; | ||
| 296 | } | ||
| 297 | return \@words; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub is { | ||
| 301 | my $word_hash = shift; | ||
| 302 | my $word = shift; | ||
| 303 | my $type = shift; | ||
| 304 | my $server = $self->{_}->{'.server'}; | ||
| 305 | my $eval = 'return 1 if $word_hash->type($word) eq $type'; | ||
| 306 | if($server) { | ||
| 307 | if(exists $self->{$word}) { | ||
| 308 | return eval($eval); | ||
| 309 | } else { | ||
| 310 | $self->retrieve($word); | ||
| 311 | return eval($eval); | ||
| 312 | } | ||
| 313 | } | ||
| 314 | eval($eval); | ||
| 315 | return undef; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub add { | ||
| 319 | my $word_hash = shift; | ||
| 320 | my $aword = shift; | ||
| 321 | my $tense = shift; | ||
| 322 | $word_hash->{$aword}->{type} = $tense; | ||
| 323 | if($tense eq 'verb') { | ||
| 324 | my $word= $aword; | ||
| 325 | my $end = substr($word,-1,1); | ||
| 326 | my $es='s'; | ||
| 327 | SWITCH: for($end) { | ||
| 328 | /e/ && do { $word = substr($word,0,-1) }; | ||
| 329 | /y/ && do { $word = substr($word,0,-1).'i' }; | ||
| 330 | /[eghsxy]/ && do { $es = 'es'; }; | ||
| 331 | 1; | ||
| 332 | } | ||
| 333 | $word_hash->{$aword}->{third} = $word.$es; | ||
| 334 | $word_hash->{$aword}->{past} = $word.'ed'; | ||
| 335 | $word_hash->{$aword}->{part} = $word.'ed'; | ||
| 336 | if(substr($aword,-2,2) ne 'ie') { | ||
| 337 | $word_hash->{$aword}->{gerund} = ($end eq 'y')?$aword.'ing':$word.'ing'; | ||
| 338 | } else { | ||
| 339 | $word_hash->{$aword}->{gerund} = substr($aword,0,-2).'ying'; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | push @{$word_hash->{_}->{'.modified'}}, $aword; | ||
| 343 | $word_hash->{_}->{'.changed'} = 1; | ||
| 344 | $word_hash->{_}->{'.server'} = 1; | ||
| 345 | return $word_hash; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub link { | ||
| 349 | my $word_hash = shift; | ||
| 350 | my $word = shift; | ||
| 351 | my $lword = shift; | ||
| 352 | my $rel = shift || 'synonym'; | ||
| 353 | $word_hash->type($word); | ||
| 354 | my @tmp = grep { $_->{word} eq $lword && $_ ->{relation} eq $rel } | ||
| 355 | @{$word_hash->{$word}->{links}}; | ||
| 356 | if(!@tmp){ | ||
| 357 | push @{$word_hash->{$word}->{links}}, { | ||
| 358 | word => $word, | ||
| 359 | relation => $rel, | ||
| 360 | }; | ||
| 361 | push @{$word_hash->{_}->{'.modified'}}, $word; | ||
| 362 | $word_hash->{_}->{'.changed'} = 1; | ||
| 363 | $word_hash->{_}->{'.server'} = 1; | ||
| 364 | return $word_hash; | ||
| 365 | } | ||
| 366 | return undef; | ||
| 367 | } | ||
| 368 | |||
| 369 | sub is_verb { shift->tense (shift) } | ||
| 370 | sub is_past { shift->tense (shift) eq 'past' } | ||
| 371 | sub is_part { shift->tense (shift) eq 'part' } | ||
| 372 | sub is_third { shift->tense (shift) eq 'third' } | ||
| 373 | sub is_gerund { shift->tense (shift) eq 'gerund' } | ||
| 374 | sub is_noun { shift->type (shift) eq 'noun' } | ||
| 375 | sub is_adv { shift->type (shift) eq 'adverb' } | ||
| 376 | sub is_pnoun { shift->type (shift) eq 'pronoun' } | ||
| 377 | sub is_art { shift->type (shift) eq 'article' } | ||
| 378 | sub is_adj { shift->type (shift) eq 'adjective' } | ||
| 379 | sub is_conj { shift->type (shift) eq 'cojunction' } | ||
| 380 | sub is_prep { shift->type (shift) eq 'preposition' } | ||
| 381 | sub defn { $_[0]->type ($_[1]); $_[0]->{$_[1]}->{defn} } | ||
| 382 | sub type { | ||
| 383 | my $self = shift; | ||
| 384 | my $word = shift; | ||
| 385 | my $server = $self->{_}->{'.server'}; | ||
| 386 | my $eval = '($self->{$word}->{type})?$self->{$word}->{type}:$self->tense($word)'; | ||
| 387 | if($server) { | ||
| 388 | if(exists $self->{$word}) { | ||
| 389 | return eval($eval); | ||
| 390 | } else { | ||
| 391 | $self->retrieve($word); | ||
| 392 | return eval($eval); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | eval($eval); | ||
| 396 | } | ||
| 397 | |||
| 398 | sub retrieve { | ||
| 399 | my $self = shift; | ||
| 400 | my $word = shift; | ||
| 401 | my $server = $self->{_}->{'.server'}; | ||
| 402 | return undef if!$server; | ||
| 403 | print $server "DICT-QUERY:$word", EOL; flush $server; | ||
| 404 | my $str;while(chomp($_=<$server>)){s/\r//g;;last if!$_;$str.=$_} | ||
| 405 | $self->{$word} = eval($str); | ||
| 406 | warn "Error in database server query: $@\n" if $@; | ||
| 407 | return $self->{$word}; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub sync { | ||
| 411 | my $self = shift; | ||
| 412 | my $server = $self->{_}->{'.server'}; | ||
| 413 | my $direction = shift || 'down'; | ||
| 414 | return undef if $direction ne 'up' && $direction ne 'down'; | ||
| 415 | my $verbose = shift || 0; | ||
| 416 | if($direction eq 'down') { | ||
| 417 | print $server "DICT-KEYS",EOL; flush $server; | ||
| 418 | my $eval = <$server>; | ||
| 419 | my $keys = eval($eval); | ||
| 420 | my $count = 0; | ||
| 421 | foreach my $key (@{$keys}) { | ||
| 422 | next if!$key; | ||
| 423 | $self->type($key); | ||
| 424 | ++$count; | ||
| 425 | print sprintf("%.0f",($#keys/$count)),"% ($count of $#keys words)\r" if($verbose); | ||
| 426 | } | ||
| 427 | $self->{_}->{'.sync'} = 1; | ||
| 428 | $self->{_}->{'.synced_down'} = 1; | ||
| 429 | } else { | ||
| 430 | my $count = 0; | ||
| 431 | my @keys = @{$self->{_}->{'.modified'}}; | ||
| 432 | foreach my $key (@keys) { | ||
| 433 | print $server "DICT-SET:$key\n"; | ||
| 434 | my $dump=Dumper($self->{$2}); $dump=~s/\$VAR1 = //g; | ||
| 435 | print $server $dump,EOL; | ||
| 436 | ++$count; | ||
| 437 | print sprintf("%.0f",($#keys/$count)),"% ($count of $#keys words)\r" if($verbose); | ||
| 438 | } | ||
| 439 | print $server 'DICT-WRITEOUT',EOL; | ||
| 440 | $self->{_}->{'.modified'} = {}; | ||
| 441 | } | ||
| 442 | $self; | ||
| 443 | } | ||
| 444 | |||
| 445 | sub tense { | ||
| 446 | my $word_hash = shift; | ||
| 447 | my $word = shift; | ||
| 448 | my $tense = shift; | ||
| 449 | my $server = $word_hash->{_}->{'.server'}; | ||
| 450 | if($server) { | ||
| 451 | print $server "DICT-TENSE:$word", EOL; flush $server; | ||
| 452 | my $str;while(chomp($_=<$server>)){s/\r//g;;last if!$_;$str.=$_} | ||
| 453 | my $return = eval($str); | ||
| 454 | warn "Error in database server query: $@\n" if $@; | ||
| 455 | return $return; | ||
| 456 | } | ||
| 457 | if($tense) { | ||
| 458 | return 1 if $word_hash->tense($word) eq $tense; | ||
| 459 | return undef; | ||
| 460 | } | ||
| 461 | my ($w,$info); | ||
| 462 | while(($w,$info) = each %{$word_hash}) { | ||
| 463 | if($info->{type} eq 'verb) { | ||
| 464 | return 'past' if $info->{past} eq $word; | ||
| 465 | return 'part' if $info->{part} eq $word; | ||
| 466 | return 'third' if $info->{third} eq $word; | ||
| 467 | return 'gerund' if $info->{gerund} eq $word; | ||
| 468 | return 'infinitive' if $w eq $word; | ||
| 469 | } | ||
| 470 | } | ||
| 471 | return undef; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub syns { | ||
| 475 | my $word_hash = shift; | ||
| 476 | my $word = shift; | ||
| 477 | my @words; | ||
| 478 | $word_hash->type($word); | ||
| 479 | foreach my $link (@{$word_hash->{$word}->{links}}) { | ||
| 480 | push @words, $link->{word} if $link->{relation} eq 'synonym'; | ||
| 481 | } | ||
| 482 | return \@words; | ||
| 483 | } | ||
| 484 | |||
| 485 | sub opps { | ||
| 486 | my $word_hash = shift; | ||
| 487 | my $word = shift; | ||
| 488 | my @words; | ||
| 489 | $word_hash->type($word); | ||
| 490 | foreach my $link (@{$word_hash->{$word}->{links}}) { | ||
| 491 | push @words, $link->{word} if $link->{relation} eq 'opposite'; | ||
| 492 | } | ||
| 493 | return \@words; | ||
| 494 | } | ||
| 495 | 1; | ||
| 496 | |||
| 497 | =begin | ||
| 498 | |||
| 499 | =head1 NAME | ||
| 500 | |||
| 501 | Lingua::EN::Dict - BETA Version of XML english dictionary storage. | ||
| 502 | |||
| 503 | =head1 SYNOPSIS | ||
| 504 | |||
| 505 | use Lingua::EN::Dict; | ||
| 506 | |||
| 507 | my $dict = Lingua::EN::Dict->new('words.xml'); | ||
| 508 | my $part_of_speech = $dict->type('abash'); | ||
| 509 | my $verb_tense = $dict->tense('zoomed'); | ||
| 510 | my $flag1 = $dict->is_verb('utilizes'); | ||
| 511 | my $flag2 = $dict->is_verb('utilized'); | ||
| 512 | my $flag3 = $dict->is_verb('utilizing'); | ||
| 513 | my @synonyms = $dict->syns('dictate'); | ||
| 514 | my @antonyms = $dict->opps('valid'); | ||
| 515 | my $defenition = $dict->defn('vindicate'); | ||
| 516 | |||
| 517 | undef $dict; | ||
| 518 | $dict = Lingua::EN::Dict->new( | ||
| 519 | server => e.tdcj.com | ||
| 520 | port => 7778, | ||
| 521 | } | ||
| 522 | # defaults to local file 'words.xml' if it | ||
| 523 | # cannot reach server. | ||
| 524 | # everything in first paragraph works here too | ||
| 525 | |||
| 526 | undef $dict; | ||
| 527 | $dict = Lingua::EN::Dict->new( | ||
| 528 | server => localhost | ||
| 529 | port => 7778, | ||
| 530 | } | ||
| 531 | # everything in first paragraph works here too | ||
| 532 | |||
| 533 | undef $dict; | ||
| 534 | $dict = Lingua::EN::Dict->new; | ||
| 535 | # same as above consructor, defaults to local file | ||
| 536 | # 'words.xml' if it cannot reach server. | ||
| 537 | # everything in first paragraph works here too | ||
| 538 | |||
| 539 | =head1 DESCRIPTION | ||
| 540 | |||
| 541 | Note: BETA VERSION. | ||
| 542 | |||
| 543 | See main reason for release of this module, three paragraphs down. | ||
| 544 | |||
| 545 | =item Description | ||
| 546 | |||
| 547 | This is a small module I came up with to use as a storage format for | ||
| 548 | my humble attempt at a natural language parser (or a subset of natural | ||
| 549 | language - english that is). This is a seperate module that stores | ||
| 550 | the words in an xml-format file. With the distribution file, you | ||
| 551 | should have received an XML file called 'words.xml' that contains almost | ||
| 552 | 3000 words consiting of several hundred verbs (not counting the seperate | ||
| 553 | forms of each of the verbs), as well as several hundred nouns, and | ||
| 554 | adjectives, articles, and modals. | ||
| 555 | |||
| 556 | This module was created for the storage and retrieval of words from | ||
| 557 | the XML file. It parses the XML file with XML::Parser and stores the | ||
| 558 | words in a blessed hash refrence which is returned from the new constructor. | ||
| 559 | This means that after you have loaded the dictionary, assuming you used | ||
| 560 | the default XML file, you can access the properties of the word 'abash' with | ||
| 561 | this: | ||
| 562 | my $info = $dict->{'abash'}; | ||
| 563 | $info will now contain a hash refrence to a structure of information about | ||
| 564 | the word 'abash'. $info will always have at least one key, 'type'. 'type' indicates | ||
| 565 | the part of speech that the word is. What keys $info contains depends on the type | ||
| 566 | of word 'abash' is. If it is a verb, $info will contain the keys 'third', 'past', | ||
| 567 | 'part', and 'gerund', and possible, 'defn'. If it is a modal, it will have a key | ||
| 568 | named 'modal_type'. Since this is a beta version, I wont go into too much more | ||
| 569 | detail here. Experiment and enjoy. Look at the default 'words.xml' file | ||
| 570 | for an idea of the structure. Each tag inside a <record></record> pair is | ||
| 571 | stored under that tags name as a key in $info, with a few exceptions, of course. | ||
| 572 | |||
| 573 | |||
| 574 | =item Reason for Release | ||
| 575 | |||
| 576 | The main reason for the beta relerase of this module is this: I would like | ||
| 577 | any and all feedback on the TCP server setup that I have added to this module. | ||
| 578 | |||
| 579 | I often got fed-up with having to wait 20 - 40 seconds for the new() constructor | ||
| 580 | to load and parse the entire 590k of words just to run a simple 2 line test script. | ||
| 581 | And since I like to tweek and run, tweek and run (the life of a Perl programmer, eh? :-), | ||
| 582 | it was really annoying to have to wait 30 seconds for each test to run, when the | ||
| 583 | actual test script took less than 50ms to run. Sooooo... I added a simple TCP transfer | ||
| 584 | setup for the dictionary. | ||
| 585 | |||
| 586 | To invovke a server process for the dictionary, simply use this one-liner: | ||
| 587 | |||
| 588 | % perl -MLingua::EN::Dict -e daemon | ||
| 589 | |||
| 590 | daemon() is a function automatically exported by this module for just this purpose. It | ||
| 591 | binds a TCP server to port 7778, accepting input from any IP address and loads the file | ||
| 592 | 'words.xml' into a dictionary object for serving. | ||
| 593 | |||
| 594 | To create a client for this server, simply use: | ||
| 595 | |||
| 596 | my $dict = new Lingua::EN::Dict; | ||
| 597 | |||
| 598 | This automatically tries to connect to the server on port 7778 of 'localhost'. If it cannot | ||
| 599 | connect to the server, it emits a warning and proceeds to try to load the default file 'words.xml'. | ||
| 600 | |||
| 601 | There are other options for the new constructor, as well as other options to the daemon() function. | ||
| 602 | For both, see below. | ||
| 603 | |||
| 604 | The reason I released this beta version was to get input from those of you who | ||
| 605 | might have some idea of how to make sure I don't leave any security holes in the TCP server | ||
| 606 | portion. Because, for example, it is possible: | ||
| 607 | |||
| 608 | my $dict = new Lingua::EN::Dict( | ||
| 609 | server => remote.server.system.com, | ||
| 610 | port => 7778 | ||
| 611 | ); | ||
| 612 | |||
| 613 | The new constructor allows you to specify the server name and port | ||
| 614 | to connect to. This would allow a central dictionary server to be setup | ||
| 615 | on some server (I have a server I am setting up for that purpose) and that | ||
| 616 | would allow other users of this module to access a much larger database | ||
| 617 | which can be updated by many people, instead of each user having to | ||
| 618 | maintain his own copy of the words database. (Yes, I know I'll need to | ||
| 619 | add forking to the daemon(), but this is just development, I'll keep it simple for | ||
| 620 | now. Future release I'll add forking.) | ||
| 621 | |||
| 622 | Since it does allow remote users the ability to TCP into the daemon, I know | ||
| 623 | that security checks need to be added to plug any potential holes. What I don't | ||
| 624 | know is exactly what holes exist and how to plug them. | ||
| 625 | |||
| 626 | HELP, I<PLEASE>! Anyone who does know anything about TCP or security of such, I<please> | ||
| 627 | take a look at the daemon() function code and let me know anything that I need to | ||
| 628 | do to make it secure. | ||
| 629 | |||
| 630 | |||
| 631 | =head1 CONSTRUCTOR | ||
| 632 | |||
| 633 | my $dict = Lingua::EN::Dict->new; | ||
| 634 | |||
| 635 | This is the default, most basic way to create a dictionary object. It will automatically | ||
| 636 | attempt to connect to the server on localhost:7778. If it doesn't find a server that | ||
| 637 | gives the expected response on that port, it will emit a warning and try to load | ||
| 638 | the file in 'words.xml' or whatever file was passed in the 'file' tag (below) and pare that. | ||
| 639 | If it cannot load that file, or if the file doesn't exist, there is no warnings. The | ||
| 640 | constructor returns a blessed refrence to a new dictionary object. | ||
| 641 | |||
| 642 | You can turn the warning about the inability to connect to the server if you want to by simply | ||
| 643 | defining the option tag 'warn_off'. | ||
| 644 | |||
| 645 | my $dict = Lingua::EN::Dict->new( warn_off => 1 ); | ||
| 646 | |||
| 647 | This will let it just silently try the server without emitting a warning if it cannot find | ||
| 648 | the server. | ||
| 649 | |||
| 650 | You can also tell it to explictly load from local disk without trying to connect to the | ||
| 651 | server by explictly passing a filename to the constructor as in this example: | ||
| 652 | |||
| 653 | my $dict = Lingua::EN::Dict->new( file => 'words.xml' ); | ||
| 654 | |||
| 655 | You may also tell it explictly what server to try to connect to with the 'server' and 'port' | ||
| 656 | tags. | ||
| 657 | |||
| 658 | my $dict = Lingua::EN::Dict->new( | ||
| 659 | server => remote.server.com, | ||
| 660 | port => 7778 | ||
| 661 | ); | ||
| 662 | |||
| 663 | You can also tell it explictly what file to load if it cannot reach the server with | ||
| 664 | the file tag. | ||
| 665 | |||
| 666 | my $dict = Lingua::EN::Dict->new( | ||
| 667 | server => remote.server.com, | ||
| 668 | port => 7778, | ||
| 669 | file => 'mydictionary.xml', | ||
| 670 | ); | ||
| 671 | |||
| 672 | |||
| 673 | Quick example of how to download an entire database and save it locally: | ||
| 674 | |||
| 675 | % perl -MLingua::EN::Dict -e 'Linga::EN::Dict->new(server=>"remote.server", | ||
| 676 | port=>7778)->sync("down",1)->save("somefile.xml") or die "Error in sync/save."' | ||
| 677 | |||
| 678 | This tries to login to the specified server and download the entire database from | ||
| 679 | that server (with the sync() command), printing out its progress as it goes. Then | ||
| 680 | it writes the entire database in XML format locally to the specified file, as | ||
| 681 | well as checking for errors. | ||
| 682 | |||
| 683 | |||
| 684 | =head1 METHODS | ||
| 685 | |||
| 686 | =item $dict->save( [ $file ] ); | ||
| 687 | |||
| 688 | This writes out the dictionary in XML format to the file loaded from or $file if specified. | ||
| 689 | The only writes out IF (a) the dictionary has been modified by add() or link, or (b) it | ||
| 690 | has had sync("down",...) called on it. If it does not write out, it will return undef, | ||
| 691 | otherwise it returns $dict. | ||
| 692 | |||
| 693 | If $file is NOT specified, AND youn are connected to a dictionary server, save() will | ||
| 694 | loop through the internal array refrence at $dict->{_}->{'.modified'}, where each | ||
| 695 | key contains the name of a word that was modified with link() or add(). save() loops | ||
| 696 | thru this array and sends the info for each word modified to the server and then | ||
| 697 | sends a DICT-WRITEOUT command to the server to write the database at the server end to | ||
| 698 | disk. | ||
| 699 | |||
| 700 | |||
| 701 | =item $dict->types($type); | ||
| 702 | |||
| 703 | This returns an array refrence to all the words that have the same type as $type. Note, | ||
| 704 | the elements of the array are a scalar value containing the name of the word, NOT a hash | ||
| 705 | refrence to the info. | ||
| 706 | |||
| 707 | =item $dict->type($word); | ||
| 708 | |||
| 709 | This returns the 'type' entry for word $word. It is almost the same as: | ||
| 710 | |||
| 711 | $type = $dict->{$word}; | ||
| 712 | |||
| 713 | Why dont you want to use that code? Well, type() automatically checks to see | ||
| 714 | if you are using a server connection, and if the word has been retrieved from | ||
| 715 | the server yet. If the word hasn't been retrieved, it automatically retrieves | ||
| 716 | the word and caches it locally, THEN it returns the type. If you were to call | ||
| 717 | the above code example on a word that hadn't been retrieved yet, you would | ||
| 718 | simply get undef. | ||
| 719 | |||
| 720 | See also retrieve(). | ||
| 721 | |||
| 722 | =item $dict->is($word,$type); | ||
| 723 | |||
| 724 | Comparse the type of $word with $type, returning 1 if $word is type $type, undef | ||
| 725 | otherwise. This also automatically retrieves and caches words from the server | ||
| 726 | in a server usage situation. See above explanation for type(). | ||
| 727 | |||
| 728 | |||
| 729 | =item $dict->add($word,$type); | ||
| 730 | |||
| 731 | This will add $word to the dictionary with type $type. If $type is 'verb', add() | ||
| 732 | will attempt to conjugate the verb into infinitive, past, present, part, and gerund parts. | ||
| 733 | add() will return $dict upon completion and add $word to the internal 'modified' list. | ||
| 734 | |||
| 735 | =item $dict->link($from, $to [,$rel]); | ||
| 736 | |||
| 737 | link() adds a relation link which appears as a <link> tag in the XML file. link() adds | ||
| 738 | a <link> entry in word $from to the word $to. $rel is an optional variable specifying the | ||
| 739 | 'relation' attribute of the link. If $rel is not specified, $rel defaults to 'synonym'. | ||
| 740 | |||
| 741 | |||
| 742 | =item $dict->retrieve($word); | ||
| 743 | |||
| 744 | This attempts to retrieve the word information for $word from the dictionary server and | ||
| 745 | cache it in $dict, if connected. If it is not connected, it will return undef, otherwise | ||
| 746 | it will return a hash refrence to the info for the word. | ||
| 747 | |||
| 748 | =item $dict->sync( [ $dir, $verbose ] ); | ||
| 749 | |||
| 750 | If sync() is called without any arguments, $dir defaults to "down". The valid values | ||
| 751 | for $dir are "down" or "up". If $verbose is defined (non-zero) it will print percentage | ||
| 752 | done followed by "\r". | ||
| 753 | |||
| 754 | If $dir eq "down", sync() will query the server for a list of all the words in the database | ||
| 755 | and then it will call retrieve() on each of the words in the list. | ||
| 756 | |||
| 757 | If $dir eq "up", sync() will loop thru all the words in the "modified" list stored internally of | ||
| 758 | words that were modified by add() or link(), and it will upload those words to the server, | ||
| 759 | followed by a DICT-WRITEOUT command to the server. | ||
| 760 | |||
| 761 | sync() is used internally by $dict->save(). | ||
| 762 | |||
| 763 | If $verbose is defined, it prints a string in the form of: | ||
| 764 | 50% (50 of 100 word) | ||
| 765 | With appropriate values substituted for the numbers, of course. | ||
| 766 | |||
| 767 | |||
| 768 | =item $dict->tense($word); | ||
| 769 | |||
| 770 | This searches the verbs in the dictionary, comparing $word to the known tenses of that | ||
| 771 | verb (Infinitive, Past, Past Participle, Third Person, Gerund). If $word matches any of the tenses, | ||
| 772 | tense() will return the name of which tense it matched ("infinitive", "past", "part", | ||
| 773 | or "gerund"). If $verb doesn't match any of the verbs in the dictionary, tense() returns | ||
| 774 | undef. | ||
| 775 | |||
| 776 | This automatically detects if you are using the dictionary connected to a dictionary server. | ||
| 777 | If you are, then it sends the tense() request to be run at the server. The server will | ||
| 778 | scan the dictionary file and send the results back. All this is done transparently, so | ||
| 779 | tense() will run the same wether you are connected to a server or not. | ||
| 780 | |||
| 781 | |||
| 782 | =item $dict->syns($word); | ||
| 783 | |||
| 784 | =item $dict->opps($word); | ||
| 785 | |||
| 786 | Both of these functions return arrays, not array refs. syns() finds all the synonyms for | ||
| 787 | the $word. opps() finds all the antonyms for $word. | ||
| 788 | |||
| 789 | You can add more synonyms or antonyms with the link() method. Example: | ||
| 790 | |||
| 791 | $dict->link('cat', 'dog', 'opposite'); | ||
| 792 | $dict->link('scream', 'yell', 'synonym'); | ||
| 793 | |||
| 794 | See the link() method for more information on syntax. | ||
| 795 | |||
| 796 | |||
| 797 | =item $dict->defn($word); | ||
| 798 | |||
| 799 | Returns a scalar containing the definition entry for word $word. Note: Words may not | ||
| 800 | have a definition entry. | ||
| 801 | |||
| 802 | |||
| 803 | =item $dict->is_verb($word); | ||
| 804 | |||
| 805 | =item $dict->is_past($word); | ||
| 806 | |||
| 807 | =item $dict->is_part($word); | ||
| 808 | |||
| 809 | =item $dict->is_third($word); | ||
| 810 | |||
| 811 | =item $dict->is_gerund($word); | ||
| 812 | |||
| 813 | These five functions test verbs for the specified part. Ex. is_gerund($word) tests if | ||
| 814 | $word is in Gerund form. Uses tense() internally. Returns undef on failure, otherwise | ||
| 815 | returns a defined value for truth. | ||
| 816 | |||
| 817 | =item $dict->is_noun($word); | ||
| 818 | |||
| 819 | =item $dict->is_adv($word); | ||
| 820 | |||
| 821 | =item $dict->is_pnoun($word); | ||
| 822 | |||
| 823 | =item $dict->is_art($word); | ||
| 824 | |||
| 825 | =item $dict->is_adj($word); | ||
| 826 | |||
| 827 | =item $dict->is_conj($word); | ||
| 828 | |||
| 829 | =item $dict->is_prep($word); | ||
| 830 | |||
| 831 | These test word $word for the type indicated by the function name. The abreviations are | ||
| 832 | as follows: | ||
| 833 | |||
| 834 | noun => noun | ||
| 835 | adv => adverb | ||
| 836 | pnoun => pronoun | ||
| 837 | art => article | ||
| 838 | adj => adjective | ||
| 839 | conj => conjunction | ||
| 840 | prep => preposition | ||
| 841 | |||
| 842 | Returns undef for false, otherwise returns a defined value for true. | ||
| 843 | |||
| 844 | |||
| 845 | =head1 daemon( [ $file,$port,$addr ] ); | ||
| 846 | |||
| 847 | This is an automatically exported function. When called with no arguments, | ||
| 848 | it binds to port 7778 and loads file 'words.xml' in a dictionary object to be served. | ||
| 849 | It, by default, accepts requests from any IP address. To bind to a specific IP, | ||
| 850 | use: | ||
| 851 | daemon(undef,undef,'206.70.2.13'); | ||
| 852 | |||
| 853 | Example usage: | ||
| 854 | |||
| 855 | % perl -MLingua::EN::Dict -e daemon | ||
| 856 | |||
| 857 | This starts a server process on port 7778, letting requests from any IP. | ||
| 858 | To specify a different file to load, use: | ||
| 859 | |||
| 860 | % perl -MLingua::EN::Dict -e daemon("myfile.xml"); | ||
| 861 | |||
| 862 | Or, for multiple network adapters, you can use one network adapter for the server (at | ||
| 863 | least, thats how my development box is: I use one network card for my dictionary server, | ||
| 864 | one for my main inet connection, and one for my LAN). Anyways, to specify port and IP, | ||
| 865 | as well as file, do: | ||
| 866 | |||
| 867 | % perl -MLingua::EN::Dict -e daemon("myfile.xml",3000,'206.70.2.13'); | ||
| 868 | |||
| 869 | B<This function is what I need feedback on.> I<Please> take a look at the source to | ||
| 870 | see if there are any security holes that need patched, or anyother security-related | ||
| 871 | problems. Thankyou ahead of time! | ||
| 872 | |||
| 873 | |||
| 874 | =head1 EXPORT | ||
| 875 | |||
| 876 | daemon(); | ||
| 877 | |||
| 878 | =head1 AUTHOR | ||
| 879 | |||
| 880 | Josiah Bryan F<E<lt>jdb@wcoil.comE<gt>> | ||
| 881 | |||
| 882 | Copyright (c) 2000 Josiah Bryan. All rights reserved. This program is free software; | ||
| 883 | you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 884 | |||
| 885 | The C<Lingua::EN::Dict> and related modules are free software. THEY COME WITHOUT WARRANTY OF ANY KIND. | ||
| 886 | |||
| 887 | =head1 DOWNLOAD | ||
| 888 | |||
| 889 | You can always download the latest copy of Lingua::EN::Dict | ||
| 890 | from http://www.josiah.countystart.com/modules/get.pl?dict:pod | ||
| 891 | |||
| 892 | |||
| 893 | =head1 SEE ALSO | ||
| 894 | |||
| 895 | Nothing to see also here... move along now. | ||
| 896 | |||
| 897 | =cut | ||