forked from quoidautre/talentbuddy-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-tokens-stemming.php
83 lines (75 loc) · 1.83 KB
/
query-tokens-stemming.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
<?php
function token_stemming($tokens, $suffixes) {
foreach( $tokens as $t ){
$c = $t;
foreach( $suffixes as $s ){
$s = preg_quote($s);
$suf = preg_replace("/(".$s."$)/S","",$t);
$c = (strlen($c)>strlen($suf))?$suf:$c;
}
echo $c."\n";
}
}
?>
<?php
function token_stemming($tokens, $suffixes) {
// Write your code here.
$out = [];
usort($suffixes, function($a, $b) {
return strlen($b) - strlen($a);
});
foreach ($tokens as $token) {
$pattern = '/(' . implode('|', $suffixes) . ')$/';
$out[] = preg_replace($pattern, '', $token);
}
$out = implode("\n", $out);
echo $out;
}
?>
<?php
function token_stemming($tokens, $suffixes) {
function cmp($a, $b) {
return strlen($b)-strlen($a);
}
// sort suffixes in descending length
usort($suffixes, "cmp");
foreach ($tokens as $t) {
$matches = false;
foreach ($suffixes as $s) {
if (preg_match("/(.*){$s}$/", $t, $matches)) {
echo $matches[1] . PHP_EOL;
break;
}
}
if (!$matches)
echo $t . PHP_EOL;
}
}
?>
<?php
function compare_len($a, $b)
{
return (strlen($a)<strlen($b));
}
function remove_suffix(&$token, $key, $suffixes)
{
usort($suffixes, "compare_len");
foreach ($suffixes as $suffix)
{
if ($token == $suffix) //add stupid test to make this evaluation work
{
$token = "";
break;
}
if ($token!=basename($token, $suffix))
{
$token = basename($token, $suffix);
break;
}
}
}
function token_stemming($tokens, $suffixes) {
array_walk($tokens,'remove_suffix', $suffixes);
echo implode("\n", $tokens);
}
?>