commit | author | age
|
779694
|
1 |
<?php |
JK |
2 |
class bot_synonimy_module implements BotModule { |
|
3 |
function handle($msg, $params) { |
|
4 |
$args = trim($msg->args); |
|
5 |
|
|
6 |
if(empty($args)) { |
|
7 |
return new BotMsg('Funkcja <b>synonimy</b> wymaga argumentu.<br />' |
|
8 |
. '<br />'."\n" |
|
9 |
. '<u>Przykład:</u><br />'."\n" |
|
10 |
. 'synonimy abecadło<br />'."\n" |
|
11 |
. 'synonimy wyspa'); |
|
12 |
} |
|
13 |
|
|
14 |
$args = funcs::utfToAscii($args); |
|
15 |
|
|
16 |
$fp = fopen('./data/synonimy/thesaurus.res', 'r'); |
|
17 |
list($rows, $len) = explode('x', trim(fgets($fp, 50))); |
|
18 |
$start = ftell($fp); |
|
19 |
|
|
20 |
$l = 0; |
|
21 |
$p = $rows-1; |
|
22 |
|
|
23 |
while($l<$p) { |
|
24 |
//echo 'L: '.$l.' - P: '.$p."\n"; |
|
25 |
$s = floor(($l+$p)/2); |
|
26 |
|
|
27 |
$poz = $start + ($s*$len); |
|
28 |
fseek($fp, $poz); |
|
29 |
|
|
30 |
$line = fread($fp, $len); |
|
31 |
|
|
32 |
$word = strtok($line, ';'); |
|
33 |
$cmp = strcmp($word, $args); |
|
34 |
|
|
35 |
if($cmp >= 0) { |
|
36 |
//echo 'Right'."\n"; |
|
37 |
$p = $s; |
|
38 |
} |
|
39 |
else |
|
40 |
{ |
|
41 |
//echo 'Left'."\n"; |
|
42 |
$l = $s+1; |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
$poz = $start + ($l*$len); |
|
47 |
fseek($fp, $poz); |
|
48 |
|
|
49 |
$i = 1; |
|
50 |
$return = ''; |
|
51 |
while(TRUE) { |
|
52 |
$line = fread($fp, $len); |
|
53 |
$word = strtok($line, ';'); |
|
54 |
|
|
55 |
if($word != $args) break; |
|
56 |
$return .= '<br />'."\n".($i++).'. '.rtrim(substr(strstr($line, ';'), 1)); |
|
57 |
} |
|
58 |
|
|
59 |
if(!empty($return)) { |
|
60 |
return new BotMsg('Znalezione synonimy:'.$return); |
|
61 |
} |
|
62 |
else |
|
63 |
{ |
|
64 |
return new BotMsg('Nie znaleziono synonimów podanego słowa'); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
?> |