Jump to content


Photo

FULLTEXT - would like to get context


  • Please log in to reply
3 replies to this topic

#1 mgm_03

mgm_03
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts
  • LocationFort Worth, TX

Posted 05 September 2003 - 03:10 AM

I\'ve implemented FULLTEXT searching along with the score and it works great. If a user enters a keyword, the list of companies who match that keyword are returned.

Is it possible to display the context in which the keyword was found?

\"Our shop can do make prototypes from any kind of plastic\"

Assuming the keyword searched was \"prototypes\", how could I get this sentence or (fragment of it) to be returned from the query? I guess this is like a real search engine.

Any ideas? Thanks!
Guru?...in my dreams!
Sorry Everybody

----------------------

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 September 2003 - 02:02 PM

For each returned record you first have to determine which of keywords entered was found in the field, and at what position in the text.

strpos() will give you this.

Then starting at that position select several words either side and display with substr().
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 September 2003 - 05:33 PM

This\'ll give 10 words either side, or to begining and end of the sentence whichever is shorter.

[php:1:e29276e025]<?php
function context($keywords, $text) {
$result = \'\';
$textLen = strlen($text);
foreach ($keywords as $str) {
if (($p = strpos($text,$str)) !== false) {
$len = strlen($str);
$spcount = 0;
for ($i=$p-1; $i >= 0; $i--) {
if ($text{$i} == \'.\')
break;
if ($text{$i}==\' \') {
if (++$spcount > 10)
break;
}
}
$i++;
$spcount = 0;
$j1 = $p+$len;
for ($j=$j1; $j < $textLen; $j++) {
if ($text{$j} == \'.\')
break;
if ($text{$j}==\' \') {
if (++$spcount > 10)
break;
}
}
return trim(substr($text,$i, $p - $i))
. \" <font color=\'red\'>$str</font> \"
. trim(substr($text,$j1, $j - $j1));

}
}
return \'\';

}

$text = \'By Lewis Carroll. Twas brillig and the slithy toves did gyre and gimble in the wabe. \'
. \'All mimsy were the borogoves and the mome raths outgrabe.\';

$keywords = array(\'gyre\', \'jabberwock\');

echo context($keywords,$text);

?>[/php:1:e29276e025]

hth
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 mgm_03

mgm_03
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts
  • LocationFort Worth, TX

Posted 08 September 2003 - 04:37 PM

Hey man, Thanks!

I had a feeling that strpos() was the function I needed but I had not implemented any code yet.

Thanks for the code snippet, too. ...i\'ve got so much coding to do.

fwiw...i found a good tutorial that simulates Google query results:

http://zend.com/zend...al-brogdon2.php

cheers.
Guru?...in my dreams!
Sorry Everybody

----------------------




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users