Page 1 of 1
The Search Is On...
Posted: Fri Jul 29, 2005 2:57 am
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.
Posted: Fri Jul 29, 2005 6:18 am
by timvw
Code: Select all
SELECT descp, COUNT(*) AS count
FROM requests
GROUP BY descp
Posted: Sun Jul 31, 2005 9:07 pm
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
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?
Posted: Mon Aug 01, 2005 8:26 am
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..
Posted: Tue Aug 02, 2005 7:48 pm
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?