add "Did you mean?" to search module

Begonnen von algebre, 14 Dezember 2010, 17:07:26

« vorheriges - nächstes »

0 Mitglieder und 1 Gast betrachten dieses Thema.

algebre

hi
add "Did you mean?"  google suggestion to your search module.

1- backup your modules/Search/index.php
2- open modules/Search/index.php with text editor
3- search for : if (!empty($query)) {
    echo '<br />';
    OpenTable();
    echo '<div class="title">' . _FINDMORE . '</div><br />
    <div class="content">' . _DIDNOTFIND . '<br /><br />'
     . _SEARCH . ' <b>' . $search_htmlstring . '</b> ' . _SEARCHINGIN . ':<br /><br />'
     . '<ul>'
     . $mods
     . '<li><a href="http://www.google.com/search?q=' . $search_urlstring . '" target="_blank">Google</a></li>'
     . '<li><a href="http://groups.google.com/groups?q=' . $search_urlstring . '" target="_blank">Google Groups</a></li>'
     . '</ul></div>';
    CloseTable();
}

4- replace by:

$didyoumean = '';
    echo '<br />';
    $domaine = ((_DOC_LANGUAGE == 'tr') ? 'com.tr' : _DOC_LANGUAGE);
    $ask = "http://www.google.{$domaine}/search?q=" . str_replace(' ', '+', $search_htmlstring);
    $ask = str_replace('..', '.', $ask);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $ask);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $get = curl_exec($ch);
    $get = utf8_encode($get);
    $get = explode("spell=1", $get);
    if (sizeof($get) > 1) {
        $get = explode("</a>", $get[1]);
        $get = explode("class=spell>", $get[0]);
        if (sizeof($get) > 1) {
            $res = $get[1];
            if (strlen($res) > 2) {
                $didyoumean = '<li>' ._DIDYOUMEAN. ' : <a href="modules.php?name=' . $module_name . '&amp;type='.$_REQUEST['type'].'&amp;query=' . urlencode(strip_tags($res)) . '">' . $res . '</a></li>';
            }
        }
    }
if (!empty($query)) {
   
OpenTable();

    echo '<div class="title">' . _FINDMORE . '</div><br />
    <div class="content">' . _DIDNOTFIND . '<br /><br />'
     . _SEARCH . ' <b>' . $search_htmlstring . '</b> ' . _SEARCHINGIN . ':<br /><br />'
     . '<ul>'
     . $didyoumean
     . $mods
     . '<li><a href="http://www.google.com/search?q=' . $search_urlstring . '" target="_blank">Google</a></li>'
     . '<li><a href="http://groups.google.com/groups?q=' . $search_urlstring . '" target="_blank">Google Groups</a></li>'
     . '</ul></div>';
    CloseTable();
}


5- add those constant to languages files modules/Search/language/lang-XXXX.php
english : define('_DIDYOUMEAN','Search instead for');
german & german_du : define('_DIDYOUMEAN','Stattdessen suchen nach');
french : define('_DIDYOUMEAN','Essayez avec l\'orthographe');
for turkish and danish you can find translation in google


test here or here


algebre

#1
hi
found 2 bugs:
google's suggestion is encoded in utf-8, => fixed
in some cases $ _REQUEST ['type'] (passed in url) is !isset => fixed

<?php // for syntax-highlighting
$didyoumean = '';
    echo 
'<br />';
    
$domaine = ((_DOC_LANGUAGE == 'tr') ? 'com.tr' : _DOC_LANGUAGE);
    
$domaine = ((_DOC_LANGUAGE == 'en') ? 'com' : _DOC_LANGUAGE);
    
$ask = "http://www.google.{$domaine}/search?q=" . str_replace(' ', '+', $search_htmlstring);
    
$ask = str_replace('..', '.', $ask);
    
$ch = curl_init();
    
curl_setopt($ch, CURLOPT_URL, $ask);
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    
$get = curl_exec($ch);
    
$get = utf8_encode($get);
    
$get = explode("spell=1", $get);
    if (
sizeof($get) > 1) {
        
$get = explode("</a>", $get[1]);
        
$get = explode("class=spell>", $get[0]);
        if (
sizeof($get) > 1) {
            
$res = $get[1];
            if (
strlen($res) > 2) {
                
$res = iconv("UTF-8", _CHARSET, $res);
                
$didyoumean = '<li>' ._DIDYOUMEAN. ' : <a href="modules.php?name=' . $module_name . ''.(isset($_REQUEST['type']) ? '&amp;type='.$_REQUEST['type'].'' : '').'&amp;query=' . urlencode(strip_tags($res)) . '">' . $res . '</a></li>';
            }
        }
    }
if (!empty(
$query)) {
    
OpenTable();

    echo 
'<div class="title">' . _FINDMORE . '</div><br />
    <div class="content">' 
. _DIDNOTFIND . '<br /><br />'
     
. _SEARCH . ' <b>' . $search_htmlstring . '</b> ' . _SEARCHINGIN . ':<br /><br />'
     
. '<ul>'
     
. $didyoumean 
     
. $mods
     
. '<li><a href="http://www.google.com/search?q=' . $search_urlstring . '" target="_blank">Google</a></li>'
     
. '<li><a href="http://groups.google.com/groups?q=' . $search_urlstring . '" target="_blank">Google Groups</a></li>'
     
. '</ul></div>';
    
CloseTable();
}

grafikmurkser

meine pmx-Seite : die erste und einzige, freie, deutschsprachige Community rund um fraktale Bilder apophysis-schmiede   reg.User mehr als 1000

Webfan

Hi Algebre,
thank you very much! I have implemented this into my search: http://www.pragmamx.org/Forum-topic-32049-start-msg203330.html#msg203330

Now my question is:
- Is this legal, wanted by google?
I guess, like metasearchengines do, you have to refer to where you got the results from.
But I do not know it for sure?! What do you think?
So I added a google button+link to the results from google. Is this ok?

At the link above you can find the link to my search and the source, maybe someone would like to re-code it, and make it compatible to the pragmamx core?!

mfg
:bye:

algebre

Hi
ZitatIs this legal, wanted by google?

Due to all those  tools and APIs that "Google "  made it available to everyone and for free, I think this features is free too, because it already exist in all google search pages.

this code is only an other way to see the "google results pages" ( filter)