Rudolf has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks, the program I'm working on attempts to accomplish two things: 1: break a sentence (user inputed) into its elements (words or whatever seperated by a space character). 2: test those elements against a file of nouns to see if they match. if the first element in the sentence is a match, the program works the way I want it to (ex: Cars bla bla.) Cars will match, however if the element is not the first one (ex: I like Cars.), Cars will not match with Cars in the txt file. any help or tips/hints would be much appreciated thank you!
#!/usr/bin/perl my $sen = <STDIN>; chomp $sen; if($sen =~ s/(\.|\?|\!)$/ /g){ #get punctuation and replace with white +space $punctuation = $&; } while($sen =~ m/ /g){ # test for spaces in sentence my $pos = pos $sen; my $element = substr($sen,0,$pos,""); # get chunk of sentence chop $element; #remove end whitespace push(@senElements,$element); #push chunk into array } open(NOUNS,'<',"nouns.txt") or die "Can't open noun database: $!\n"; # # # attempt to recognize sentence elements as a noun via file nouns. +txt # # # foreach $element (@senElements){ while(<NOUNS>){ chomp(my $line = $_); $line =~ s/ |\n//g; #remove any space chars and newlines from file + line if($element =~ m/^($line)$/i){ print "\n!MATCH! ~ $element is a noun\n"; } } } close(NOUNS);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: testing parts of a string against a word database
by Eliya (Vicar) on Dec 01, 2011 at 00:16 UTC | |
by Rudolf (Pilgrim) on Dec 01, 2011 at 00:21 UTC | |
|
Re: testing parts of a string against a word database
by TomDLux (Vicar) on Dec 01, 2011 at 00:33 UTC | |
by Rudolf (Pilgrim) on Dec 01, 2011 at 01:45 UTC | |
|
Re: testing parts of a string against a word database
by hbm (Hermit) on Dec 01, 2011 at 00:57 UTC | |
|
Re: testing parts of a string against a word database
by TJPride (Pilgrim) on Dec 01, 2011 at 01:45 UTC |