The Search Is On...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

The Search Is On...

Post by harrisonad »

hi, I am now working on a forum for posting technical problems around the company. Now i want to know what is the most common word exists in a problem description.
Can this be done in a query or I have to use PHP to do this?

My first solution though is to concatenate all strings to a single one, then perform the search from there.

Code: Select all

$strings = array();
$query = "SELECT * FROM requests";
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
	$strings[] = $data['descp'];
}
$all_strings = implode(". ",$strings);
// do the search operation here
$word = getMostCommonWord($all_strings);
I need to make that fictional function into flesh but I have no idea, or I have but not enough.

Can you help me with this?
Thanks in advance.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

SELECT descp, COUNT(*) AS count
FROM requests
GROUP BY descp
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

hi timvw, thanks for your reply but I need only the single word that is not only common to a single description (which is a sentence or phrase) but to all descriptions inside the database.
for example, if user#1 entered 'Printer not functioning' to his problem description and user#2 entered 'Installing new printer', and another user, user#3 typed 'Zip Drive cannot read disk', and all this decriptions are stored in the database, then i can retrieve them using

Code: Select all

SELECT descp FROM tablename
Now I will concatenate them into a single string, and of course remove all unwanted words such as 'not', 'new' and 'cannot'. Then I have to find out what word has the most occurence. In this case is must be the word 'Printer'.
Anyone?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

create a table containing the words you want to look at, maybe phrases.. then create a second table that builds a many to many relationship between the word table and request table.

It's fairly efficient, and requires less processing time at the point of search, but does require a bit of processing at request insertion, although not much really..
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Hi, thanks for helping me with this. I already built the function I need to get the most common word.

Code: Select all

function getCommonWord($str){
	$words = explode(" ",trim(MultiReplace(array('.','  '),'',$str)));
	$word_counts = array();
	foreach($words as $word){
		if(isset($word_counts[$word]))
			$word_counts[$word]++;
		else 	
			$word_counts[$word] = 1;		
	}
	arsort($word_counts);
	
	list($key,$value) = each($word_counts);
	return $key;
}

function MultiReplace($aray_search,$replace,$str){
	foreach($aray_search as $search){
		$str = str_replace($search,$replace,$str);
	}
	return $str;
}
What do you think?
Post Reply