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);