/
SimpleSuggest.class.php
76 lines (69 loc) · 2.04 KB
/
SimpleSuggest.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* SimpleSuggest
*
* A very small suggestion tool that crawls Google search results for a
* certain query and retrieves the "Did you mean" suggestion, if available.
*
* SimpleSuggest was created after desperately searching in the
* Google API for a piece of their spellchecking algorithms. It is hackish,
* crawling using DOMDocument and grabbing a result, but it works until
* Google decides to provide an API for their crazy "Did You Mean" feature.
*
* @author Klaus Silveira <contact@klaussilveira.com>
* @package simplesuggest
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version 0.1
*/
class SimpleSuggest {
/**
* Top-level domain name of the preferred Google search
*
* @var string
*/
public $tld = 'com';
/**
* Suggests a new query, if the current one has spelling errors
*
* Checks queries against common spellings of each word and
* suggests a new query, using Google's "Did you mean" algorithm
*
* @access public
* @param string $query Query to be checked
* @return mixed Returns the suggestion or false if no suggestion
*/
public function suggest($query) {
$query = str_replace(' ', '+', $query);
$url = "http://www.google.{$this->tld}/search?q={$query}";
return $this->crawl($url);
}
/**
* Crawl Google results and get the "Did You Mean?" suggestion
*
* @access private
* @param string $url Address of the page to crawl
* @return mixed Returns the suggestion or false if no suggestion
*/
private function crawl($url) {
/**
* We need to get rid of all those warnings sent by DOMDocument
* when reading pages with invalid markup
*/
libxml_use_internal_errors(true);
libxml_clear_errors();
/**
* Instantiate DOMDocument object and load the page
*/
$page = new DOMDocument;
$page->loadHtmlFile($url);
/**
* Find all hyperlinks with spell class and return it's value
*/
foreach($page->getElementsByTagName('a') as $link) {
if($link->getAttribute('class') == 'spell') {
return $link->nodeValue;
}
}
return false;
}
}