better search

is use this code

==========

opendir DIR,$dir or die "can't open $dir $!";
while( $file=readdir DIR ){
next unless -T "$dir/$file";
open FILE,"<$dir/$file" or warn "can't open <$dir/$file $!";
local $/=undef;
$text=<FILE>;
while( $text =~ /\s*([^.!?]*\Q$word\E[^.!?]*[.!?])/g ){
#print br;
$day = substr($file, 0, 2);
$month = substr($file, 2, 2);
$year = substr($file, 4, 4);

print "<tr><td><center>$day-$month-$year</td>";

print "<td><center>";
#print "$file: $1\n";
print escapeHTML "$1\n";
print "</td>";

========

excuse the untidyness :(
but it searches a dir full of text files and
gives back the sentences in those files that
match the query.
but say i search for "test"
and one file has "this is a test."
(note the full stop)
it wont find it!
is there a way to ensurethat words at the end
of a sentence (which have a ?!,or.) will be found
can someone tell me how to change the code?
thanks
boofullsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cadabraCommented:
I suggest a slightly different strategy, and have changed the code ,
not removing the untidiness :)




==========


$dir = ".";
$word = "test";
opendir DIR,$dir or die "can't open $dir $!";
while( $file=readdir DIR ){
next unless -T "$dir/$file";
open FILE,"<$dir/$file" or warn "can't open <$dir/$file $!";
local $/=undef;
$text=<FILE>;


@sentences = split(/[.!?]/,$text);
foreach $sentence (grep(/\b$word\b/, @sentences))
{
$day = substr($file, 0, 2);
$month = substr($file, 2, 2);
$year = substr($file, 4, 4);

print "<tr><td><center>$day-$month-$year</td>";

print "<td><center>";
print "$sentence\n";
#print escapeHTML "$sentence\n";
print "</td>";
}
}

==========

NOTE: You may want to deal with remove whitespace from the beginning and end of each $sentence before printing it.

Over to you,
Cadabra.

0
cadabraCommented:
If you are interested in providing search capabilities for your site, look at:

http://hotwired.lycos.com/webmonkey/97/16/index2a.html?tw=programming

Also, note that you can install a commercial search engine in your site for free if your site is "small": for example, you can install altavista search engine on your site for free if you search less than 3000 files.

I think there may be similar deals from other vendors like hotbot, atomz, infoseek etc.

Good luck,
cadabra.
0
boofullsAuthor Commented:
@sentences = split(/[.!?]/,$text);
                         foreach $sentence (grep(/\b$word\b/, @sentences))
                         {

doesnt seem to work
it gives me no matching line back!
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

olthoffCommented:
Your regular expression is
$text =~ /\s*([^.!?]*\Q$word\E[^.!?]*[.!?])/g

wouldn't you want an m at the end?
$text =~ /\s*([^.!?]*\Q$word\E[^.!?]*[.!?])/gm

0
ozoCommented:
What is in $text and $word when it doesn't seem to work?

/m makes no difference when you're not matching ^ or $
0
cadabraCommented:
I don't know why you did'nt get any matching lines back, but test this
code snippet, which sets the variables $word and $text by itself, breaks down the sentences, and prints the searched ones:


$text="hello, test. testing hello. test\nhi there.";
$text=$text . "This is a test.";
$word="test";
@sentences = split(/[.!?]/,$text);
foreach $sentence (grep(/\b$word\b/, @sentences))
{
  print "\n[$sentence]\n";
}



Cadabra.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
boofullsAuthor Commented:
ozo
$text="this is a test."
$word="test"

its doesnt match as theres a "." after test in $text
0
boofullsAuthor Commented:
oh sorry
i adjusted the $word before i searched....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.