1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Tesa; |
4
|
|
|
|
5
|
|
|
use Onoi\Cache\Cache; |
6
|
|
|
use Onoi\Cache\NullCache; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @license GNU GPL v2+ |
10
|
|
|
* @since 0.1 |
11
|
|
|
* |
12
|
|
|
* @author mwjames |
13
|
|
|
*/ |
14
|
|
|
class StopwordAnalyzer { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Any change to the content of its data files should be reflected in a |
18
|
|
|
* version change (the version number does not necessarily correlate with |
19
|
|
|
* the library version) |
20
|
|
|
*/ |
21
|
|
|
const VERSION = '0.1.3'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Prefix |
25
|
|
|
*/ |
26
|
|
|
const CACHE = 'onoi:tesa:stopword:'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Cache |
30
|
|
|
*/ |
31
|
|
|
private $cache; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
private $ttl = 3600; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array|null |
40
|
|
|
*/ |
41
|
|
|
private static $internalLookupCache = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $languageList = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @since 0.1 |
50
|
|
|
* |
51
|
|
|
* @param Cache|null $cache |
52
|
|
|
* @param integer $ttl |
53
|
|
|
*/ |
54
|
16 |
|
public function __construct( Cache $cache = null, $ttl = 3600 ) { |
55
|
16 |
|
$this->cache = $cache; |
56
|
16 |
|
$this->ttl = $ttl; |
57
|
|
|
|
58
|
16 |
|
if ( $this->cache === null ) { |
59
|
15 |
|
$this->cache = new NullCache(); |
60
|
15 |
|
} |
61
|
|
|
|
62
|
16 |
|
self::$internalLookupCache = array(); |
63
|
16 |
|
$this->languageList = array(); |
64
|
16 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @since 0.1 |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
2 |
|
public function getLanguageList() { |
72
|
2 |
|
return $this->languageList; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @since 0.1 |
77
|
|
|
* |
78
|
|
|
* @param integer $flag |
|
|
|
|
79
|
|
|
*/ |
80
|
6 |
|
public function loadListByDefaultLanguages() { |
81
|
6 |
|
$this->languageList = array( 'en', 'de', 'es', 'fr' ); |
82
|
6 |
|
$this->loadListByLanguage( $this->languageList ); |
83
|
6 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @since 0.1 |
87
|
|
|
* |
88
|
|
|
* @param string $location |
89
|
|
|
* @param string|array $languageCode |
90
|
|
|
*/ |
91
|
1 |
|
public function loadListFromCustomLocation( $location, $languageCode ) { |
92
|
|
|
|
93
|
1 |
|
$this->languageList = (array)$languageCode; |
94
|
|
|
|
95
|
1 |
|
$this->loadListFromCache( |
96
|
1 |
|
str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $location ), |
97
|
1 |
|
$this->languageList |
98
|
1 |
|
); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @since 0.1 |
103
|
|
|
* |
104
|
|
|
* @param string|array $languageCode |
105
|
|
|
*/ |
106
|
13 |
|
public function loadListByLanguage( $languageCode ) { |
107
|
|
|
|
108
|
13 |
|
$this->languageList = (array)$languageCode; |
109
|
|
|
|
110
|
13 |
|
$this->loadListFromCache( |
111
|
13 |
|
str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, __DIR__ . '/../data/stopwords/' ), |
112
|
13 |
|
$this->languageList |
113
|
13 |
|
); |
114
|
13 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The expected form is array( $languageCode => array( 'foo', 'bar' ) ) |
118
|
|
|
* |
119
|
|
|
* @since 0.1 |
120
|
|
|
* |
121
|
|
|
* @param array $customStopwordList |
122
|
|
|
*/ |
123
|
1 |
|
public function setCustomStopwordList( array $customStopwordList ) { |
124
|
|
|
|
125
|
1 |
|
self::$internalLookupCache = array(); |
126
|
1 |
|
$this->languageList = array(); |
127
|
|
|
|
128
|
1 |
|
foreach ( $customStopwordList as $languageCode => $contents ) { |
129
|
1 |
|
self::$internalLookupCache += array_fill_keys( $contents, true ); |
130
|
1 |
|
$this->languageList[$languageCode] = true; |
131
|
1 |
|
} |
132
|
|
|
|
133
|
1 |
|
$this->languageList = array_keys( $this->languageList ); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @since 0.1 |
138
|
|
|
* |
139
|
|
|
* @param string $word |
140
|
|
|
* |
141
|
|
|
* @return boolean |
142
|
|
|
*/ |
143
|
14 |
|
public function isStopWord( $word ) { |
144
|
14 |
|
return isset( self::$internalLookupCache[$word] ); |
145
|
|
|
} |
146
|
|
|
|
147
|
14 |
|
private function loadListFromCache( $location, $languages ) { |
148
|
|
|
|
149
|
14 |
|
self::$internalLookupCache = array(); |
150
|
14 |
|
$id = self::CACHE . md5( json_encode( $languages ) . $location . $this->ttl . self::VERSION ); |
151
|
|
|
|
152
|
14 |
|
if ( $this->cache->contains( $id ) ) { |
153
|
1 |
|
return self::$internalLookupCache = $this->cache->fetch( $id ); |
154
|
|
|
} |
155
|
|
|
|
156
|
13 |
|
foreach ( $languages as $languageCode ) { |
157
|
|
|
|
158
|
|
|
// We silently ignore any error on purpose |
159
|
13 |
|
$contents = json_decode( |
160
|
13 |
|
@file_get_contents( $location . $languageCode . '.json' ), |
161
|
|
|
true |
162
|
13 |
|
); |
163
|
|
|
|
164
|
13 |
|
if ( $contents === null || json_last_error() !== JSON_ERROR_NONE || !isset( $contents[$languageCode] ) ) { |
165
|
1 |
|
continue; |
166
|
|
|
} |
167
|
|
|
|
168
|
12 |
|
self::$internalLookupCache += array_fill_keys( $contents[$languageCode], true ); |
169
|
13 |
|
} |
170
|
|
|
|
171
|
13 |
|
$this->cache->save( $id, self::$internalLookupCache ); |
172
|
13 |
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.