in reply to Re: Parsing english
in thread Parsing english
Here is my basic code (very un-commented at present) and not very elagent.
It doesnt do anything with the 'attributes' yet.. this will be looking up in the database (finding objects of class=Attribute name={value})
Please dont hold back and rip apart/suggest/improve where applicable.
$qtys{'the'}=0; $qtys{'some'}=20; $qtys{'bunch'}=30; $qtys{'pile'}=40; $qtys{'many'}=50; $qtys{'heaps'}=100; $qtys{'heap'}=100; print &parse_obj('a small white fluffy cat called sam'); print &parse_obj('a pile of sticks made of wood'); print &parse_obj('an old orange'); print &parse_obj('the broken golden cup of plenty'); print &parse_obj('a player called bob'); print &parse_obj('3 blind mice'); print &parse_obj('a piece of old triangular wooden boat called fred'); print &parse_obj('a kind of mouse'); print &parse_obj('some roses'); sub parse_obj { my $this_obj = $_[0]; if($this_obj =~ /(.+) made of (.+)/i) { $this_obj = $1; $material=$2; }else{ } $this_obj =~ /(\w+)\s?(.+)/; my $qty = $1; my $rest = $2; my $pre=''; my $class=''; my $name = ''; my $qty_type=''; my $attribs=''; my $material=''; if($qty =~ /the/i) { # handle special named one off objects 'the void' 'the sword of ligh +t' # check for specially typed objects.. if($rest =~ /(.+) of (.+)/i){ $pre=$1; $name='of '.$2; }else{ $pre=$rest; } }elsif($rest =~ /(.+) of (.+)/i){ $pre=$1; $class=$2; if($pre =~ /(.+) (.+)/){ $pre = $1; $qty_type=$2; }else{ $qty_type=$pre; $pre =''; } }else{ $pre=$rest; } $pre .= ' '.$class; if(($pre =~ /(.+) called (\w+)/)||($pre =~ /(.+) named (\w+)/)){ $pre = $1; $name = $2; $class=''; } if($pre =~ /(.+) (\w+)/){ $attribs=$1; $class=$2; }else{ if($name ne ''){ $class=$pre; $attribs=''; }else{ $class=$pre; } } if($qtys{$qty_type} ne ''){ # convert 'a pile of' into 'pile' = 30 $qty = $qtys{$qty_type}; $qty_type=''; } $qty=($qtys{$qty} eq '') ? 1 : $qtys{$qty}; return "\n-----------------\n>>$this_obj\n qty[$qty] size=[$size] typ +[$qty_type] att[$attribs] mat=[$material] class[$class] name[$name]\n +"; }
___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com
|
|---|