#!/usr/bin/perl -w use strict; use Fcntl; use DBI; use File::Basename; use Text::English; #input Words print "Give a list of words:\n\n"; my $query = ; my @words = split /\s*(,|\s+)/, $query; #File Type: print "Give a file type (email, article):\n\n"; my $type = ; my %paths = search(\@words, $type); for my $val (keys %paths) { my $file = $paths{$val}; print "$file \n"; } sub search { my($words, $type) = @_; my($dbh, $sth1, $sth2, $rule_append, $word, $file_index); my %matches; #Database Connection: $dbh = DBI->connect( "DBI:mysql:host=localhost;database=test", "test", "test", {PrintError=>0,RaiseError=>1}); #Establish Rules for file type: CASE: { if($type = "email") { $rule_append = "where filetype = 'email' "; last CASE; } if($type = "article") { $rule_append = "where filetype = 'article' "; last CASE; } $rule_append = "where filetype = 'article' OR filetype = 'email' "; }#End of Rule foreach $word(@$words) { my $match; ( $word ) = Text::English::stem( $word ); my @poss; #round 1: Find all file pertaining to word. my $statement1 = "SELECT files FROM catalog WHERE words LIKE %".$word."%"; $sth1 = $dbh -> prepare($statement1); $sth1 -> execute(); while(my @val = $sth1 -> fetchrow_array()) { $match = $val[0]; #Get List of files. #Parse Files and Check for matches within. @poss = split(/:/, $match); foreach $file_index(@poss) { $sth2 = $dbh ->prepare("select filename, type from library where filename = $file_index and".$rule_append); $sth2 -> execute(); if(my @val2 = $sth2->fetchrow_array()) { $matches{$val2[0]} = $val2[1]; } } # All Relevant Files found. }# Cycle through all possible matches. } #End of word search loop return(%matches); #$dbh -> disconnect; }#End of search Function