-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.php
More file actions
88 lines (72 loc) · 2.29 KB
/
Copy pathfunction.php
File metadata and controls
88 lines (72 loc) · 2.29 KB
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
<?php
public function run_stemmer($target, $array, $lang)
{
$lenghts = array();
$exploded = preg_split("/([\ \"\'\.\,\?])/", $target, -1, PREG_SPLIT_DELIM_CAPTURE);
$requests[] = $exploded;
$lenghts[] = count($exploded);
foreach($array AS $k => $t)
{
$exploded = preg_split("/([\ \"\'\.\,\?])/", $t, -1, PREG_SPLIT_DELIM_CAPTURE);
$requests[] = $exploded;
$lenghts[] = count($exploded);
}
$descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"));
$process = proc_open('/w/lib/libstemmer/stemwords -l ' . $lang, $descriptorspec, $pipes);
if(is_resource($process))
{
$raw_in = '';
foreach($requests AS $ke => $re)
foreach($re as $k => $r)
if($k % 2 == 0)
$raw_in .= "{$r}\n";
fwrite($pipes[0], $raw_in);
fclose($pipes[0]);
$raw_out = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($process);
$lto = 0;
$lof = 0;
$raw_out = explode("\n", $raw_out);
foreach($raw_out as $key => $line)
{
if($lto == 0 && $key > $lof + intval($lenghts[$lto] / 2))
{
// If this is the end of the title we can't match an answer's title just yet
$lto++;
$lof = $key + 0;
}
else if($key > $lof + intval($lenghts[$lto] / 2))
{
$stem_map = array();
foreach($title_stemmed as $title_stem)
foreach($answer_stemmed_bit AS $ask => $asb)
if($answer_stemmed_bit[$ask] == $title_stem)
$stem_map[$ask] = 1;
$title_out_tmp = '';
foreach($answer_stemmed_bit AS $ask => $asb)
{
if(isset($stem_map[$ask]))
$title_out_tmp .= "<b>" . $requests[$lto][$ask * 2] . "</b>";
else
$title_out_tmp .= $requests[$lto][$ask * 2];
if(isset($requests[$lto][($ask * 2) + 1]))
$title_out_tmp .= $requests[$lto][($ask * 2) + 1];
}
$array[$lto] = $title_out_tmp;
$answer_stemmed[] = $answer_stemmed_bit;
$answer_stemmed_bit = array();
$lto++;
if($lto == count($lenghts))
break;
$lof = $key + 0;
}
if($lto != 0)
$answer_stemmed_bit[] = $line;
else
$title_stemmed[] = $line;
}
}
array_shift($array);
return $array;
}