http://www.perlmonks.org?node_id=89175


in reply to natural language sentence construction

My view about feasibility is near impossible. Well, depending on what you want to do. Parsing natural language and attempting to make it more "friendly" to your users is (a)highly unique to your particular application, and (b)requires many, *many*, special case situations. There is a CPAN module for making a word its plural, and that can be useful. . . but as for parsing an english sentence, that is near impossible with current technology--if only for the reason that language is abigous, even to native speakers at times(how often do people ask others to repeat what they said?). There was a node on this a short time ago, I believe, when someone asked if there was a module for grammar checking: called English/Grammer or something similar if I remember. As for helping you with your code, there is one area where I see it could be made clearer:
foreach my $i (0..$#$types) { if ($i && $i == $#$types) { $sentence .= ($matches > 1) ? ' and ' : ' or '; } elsif ($i) { $sentence .= ', '; }
This can be written more clearer(I hope) as:
for(@$types) { if($_ eq $types->[-1]) { $sentence .= $matches>1 ? ' and ' : ' or ' } else { $sentence .= ', ' }
Maybe it's just me, but it took a couple looks to see why you were doing what you were doing. This way, hopefully, the -1 signifies that you're checking it against the last element. But, there's more than one way to do it.