-
Notifications
You must be signed in to change notification settings - Fork 20
/
functions.php
131 lines (122 loc) · 3.53 KB
/
functions.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
function BuildTrigrams($keyword)
{
$t = "__" . $keyword . "__";
$trigrams = "";
for ($i = 0; $i < strlen($t) - 2; $i++)
$trigrams .= substr($t, $i, 3) . " ";
return $trigrams;
}
function MakeQSuggest($keyword,$index,$ln_sph)
{
$stmt = $ln_sph->prepare("CALL SUGGEST(:keyword,'$index')");
$stmt->bindValue(':keyword', $keyword,PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['suggest'];
}
function MakeMultiQSuggest($words,$query,$index,$ln_sph)
{
$suggested = array();
$llimf = 0;
$i = 0;
foreach ($words as $key => $word) {
if ($word['docs'] != 0)
$llimf +=$word['docs'];$i++;
}
$llimf = $llimf / ($i * $i);
foreach ($words as $key => $word) {
if ($word['docs'] == 0 | $word['docs'] < $llimf) {
$mis[] = $word['keyword'];
}
}
if (count($mis) > 0) {
foreach ($mis as $m) {
$re = MakeQSuggest($m,$index, $ln_sph);
if ($re) {
if($m!=$re)
$suggested[$m] = $re;
}
}
if(count($words) ==1 && empty($suggested)) {
return false;
}
$phrase = explode(' ', $query);
foreach ($phrase as $k => $word) {
if (isset($suggested[strtolower($word)]))
$phrase[$k] = $suggested[strtolower($word)];
}
$phrase = implode(' ', $phrase);
return $phrase;
}else{
return false;
}
}
function MakeSuggestion($keyword,$ln)
{
$trigrams = BuildTrigrams($keyword);
$query = "\"$trigrams\"/1";
$len = strlen($keyword);
$delta = LENGTH_THRESHOLD;
$weight = 'weight()';
if(SPHINX_20 == true) {
$weight ='@weight';
}
$stmt = $ln->prepare("SELECT *, $weight as w, w+:delta-ABS(len-:len) as myrank FROM suggest WHERE MATCH(:match) AND len BETWEEN :lowlen AND :highlen
ORDER BY myrank DESC, freq DESC
LIMIT 0,:topcount OPTION ranker=wordcount");
$stmt->bindValue(':match', $query, PDO::PARAM_STR);
$stmt->bindValue(':len', $len, PDO::PARAM_INT);
$stmt->bindValue(':delta', $delta, PDO::PARAM_INT);
$stmt->bindValue(':lowlen', $len - $delta, PDO::PARAM_INT);
$stmt->bindValue(':highlen', $len + $delta, PDO::PARAM_INT);
$stmt->bindValue(':topcount',TOP_COUNT, PDO::PARAM_INT);
$stmt->execute();
if (!$rows = $stmt->fetchAll())
return false;
// further restrict trigram matches with a sane Levenshtein distance limit
foreach ($rows as $match) {
$suggested = $match["keyword"];
if (levenshtein($keyword, $suggested) <= LEVENSHTEIN_THRESHOLD)
return $suggested;
}
return $keyword;
}
function MakePhaseSuggestion($words,$query,$ln_sph)
{
$suggested = array();
$llimf = 0;
$i = 0;
foreach ($words as $key => $word) {
if ($word['docs'] != 0)
$llimf +=$word['docs'];$i++;
}
$llimf = $llimf / ($i * $i);
foreach ($words as $key => $word) {
if ($word['docs'] == 0 | $word['docs'] < $llimf) {
$mis[] = $word['keyword'];
}
}
if (count($mis) > 0) {
foreach ($mis as $m) {
$re = MakeSuggestion($m, $ln_sph);
if ($re) {
if($m!=$re)
$suggested[$m] = $re;
}
}
if(count($words) ==1 && empty($suggested)) {
return false;
}
$phrase = explode(' ', $query);
foreach ($phrase as $k => $word) {
if (isset($suggested[strtolower($word)]))
$phrase[$k] = $suggested[strtolower($word)];
}
$phrase = implode(' ', $phrase);
return $phrase;
}else{
return false;
}
}
?>