-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuffixes.php
69 lines (63 loc) · 1.25 KB
/
Suffixes.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
<?php
/**
* @package go\ewp
*/
namespace go\ewp;
/**
* Word suffixes
*
* @author Oleg Grigoriev <go.vasac@gmail.com>
*/
class Suffixes
{
/**
* Constructor
*
* @param string $filename
*/
public function __construct($filename)
{
$this->suffixes = FileLoader::load($filename);
$this->lens = [];
foreach ($this->suffixes as $k => $v) {
$this->lens[$k] = \strlen($k);
}
}
/**
* Find a suffix for a word
*
* @param string $word
* @return array
* (prefix, word) or NULL
*/
public function find($word)
{
foreach ($this->lens as $suffix => $len) {
if (\substr($word, -$len) === $suffix) {
$w = \substr($word, 0, -$len);
if (\strlen($w) > 1) {
return [$w, $suffix];
}
}
}
return null;
}
/**
* Translate a suffix
*
* @param string $suffix
* @return string
*/
public function translate($suffix)
{
return isset($this->suffixes[$suffix]) ? $this->suffixes[$suffix] : null;
}
/**
* @var array
*/
private $suffixes;
/**
* @var array
*/
private $lens;
}