-
Notifications
You must be signed in to change notification settings - Fork 13
/
example.php
81 lines (71 loc) · 1.77 KB
/
example.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
<?php
header("content-type: text/plain; charset=utf-8");
function load_dict($mss, $filename) {
$fp = fopen($filename, "r");
while (!feof($fp)) {
$line = trim(fgets($fp));
$pos = strpos($line, "#");
if ($pos !== false) {
$line = strstr($line, 0, $pos);
}
if (!$line) {
continue;
}
$line = explode(":", $line, 2);
if (count($line) == 2) {
mss_add($mss, trim($line[0]), trim($line[1]));
} else {
mss_add($mss, trim($line[0]));
}
}
}
$mss = mss_create("example");
$timestamp = mss_timestamp($mss);
$is_ready = mss_is_ready($mss);
if ($is_ready) {
$stat = stat("example.dic");
if ($stat['mtime'] > $timestamp) {
mss_destroy($mss);
$mss = mss_create("example");
$timestamp = mss_timestamp($mss);
$is_ready = mss_is_ready($mss);
}
}
if (!$is_ready) {
echo "Load dict\n";
load_dict($mss, "example.dic");
}
$text = file_get_contents("example.txt");
//
//
echo "mss_creation: " . date("Y-m-d H:i:s", $timestamp) . "\n";
//
//
echo "mms_match(): original text\n";
$ret = mss_match($mss, $text);
echo " ", ($ret ? "matched" : "not matched"), "\n";
echo "\n";
//
//
echo "mms_search() array:\n";
$ret = mss_search($mss, $text);
echo " ", "count: ", count($ret), "\n";
echo "\n";
//
//
echo "mms_search() callback: \n";
$count = 0;
$ret = mss_search($mss, $text, function($kw, $idx, $type, $ext) {
$ext[0]++;
for ($i = strlen($kw) - 1; $i >= 0; $i--) {
$ext[1][$idx + $i] = '*';
}
}, array(&$count, &$text));
echo " ", "count: ", $count, "\n";
echo "\n";
//
//
echo "mms_match(): modified text\n";
$ret = mss_match($mss, $text);
echo " ", ($ret ? "matched" : "not matched"), "\n";
echo "\n";