Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Search Algorithm

by maverick (Curate)
on Aug 10, 2000 at 20:00 UTC ( #27312=note: print w/ replies, xml ) Need Help??


in reply to Search Algorithm

If you're going to to be doing a lot of searches through a lot of files and the files that you're searching through don't change very often, then it may be worth your time to build an index.

The basic idea is this. Using a dbm, make the key a word out of one of the files, and the value a list of files that contain that word. Thus you trade time in building the index for the speed of hash lookups.

You'll end up with two programs. One that builds the index and one that does the search.

This one will build the index.

#!/usr/bin/perl use strict; # Berkeley DBMs are my fav. use DB_File; my %Index; # remove the old index and start fresh unlink("/home/maverick/tmp/index_dbm"); tie (%Index,'DB_File',"/home/maverick/tmp/index_dbm",O_RDWR|O_CREAT,06 +40,$DB_BTREE) || die "Tie Failed: $!"; foreach my $file (glob("/home/maverick/tmp/*.txt")) { open(F,$file) || die "Can't open $file: $!"; # slirp up the file and make a list of words my @words = map { split(/\W+/,$_) } <F>; # add this file to the list of matches for this word my %uniq; foreach (@words) { if (!defined($uniq{$_})) { # we've not seen this word before, so we add i +t. # I'm also assuming that ~ is safe to use as a + seperator. if (!defined($Index{$_})) { # it's the first additon of this word, + so I don't need to prepend a '~' $Index{$_} = $file; } else { $Index{$_} .= "~$file"; } $uniq{$_} = 1; } } close(F); } untie %Index;
This one searchs using it.
#!/usr/bin/perl use strict; use DB_File; my %Index; tie (%Index,'DB_File',"/home/maverick/tmp/index_dbm",O_RDWR,0640,$DB_B +TREE) || die "Tie Failed: $!"; print ">"; while(<>) { # chop off the newline $_ =~ s/[\r\n]//go; if (defined($Index{$_})) { print "$_ found in:\n"; # replace all the ~ with \n (without modifying the index) print join("\n",split(/~/,$Index{$_})),"\n"; } else { print "Not Found\n"; } print ">"; } untie(%Index);

file1.txt contains:

here's a file that contains a bunch of random keywords on many different lines that we can use for the sake of examp +le.
file2.txt contains:
here's another file that contains even more random text for the sake of example. I hope this helps solve the problem presented by tenfourty.
and if you run the index maker and then search:
darkstar:~/tmp>./mkindex.pl darkstar:~/tmp>./search.pl >tenfourty tenfourty found in: /home/maverick/tmp/file2.txt >maverick Not Found >example example found in: /home/maverick/tmp/file1.txt /home/maverick/tmp/file2.txt >text text found in: /home/maverick/tmp/file2.txt >
Hope this helps :)

/\/\averick


Comment on Re: Search Algorithm
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://27312]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-08-23 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls