1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Tesa\StopwordAnalyzer; |
4
|
|
|
|
5
|
|
|
use Cdb\Reader; |
6
|
|
|
use Cdb\Writer; |
7
|
|
|
use Exception; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 0.1 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class CdbStopwordAnalyzer implements StopwordAnalyzer { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Any change to the content of its data files should be reflected in a |
20
|
|
|
* version change (the version number does not necessarily correlate with |
21
|
|
|
* the library version) |
22
|
|
|
*/ |
23
|
|
|
const VERSION = '0.1.cdb'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Cdb |
27
|
|
|
*/ |
28
|
|
|
private $cdb; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 0.1 |
32
|
|
|
* |
33
|
|
|
* @param string $target |
34
|
|
|
*/ |
35
|
12 |
|
public function __construct( $target ) { |
36
|
|
|
try { |
37
|
12 |
|
$this->cdb = Reader::open( $target ); |
|
|
|
|
38
|
12 |
|
} catch( Exception $e ) { |
39
|
|
|
// Do nothing |
40
|
|
|
} |
41
|
12 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @since 0.1 |
45
|
|
|
* |
46
|
|
|
* @return boolean |
47
|
|
|
*/ |
48
|
|
|
public function isAvailable() { |
49
|
|
|
return $this->cdb !== null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @since 0.1 |
54
|
|
|
* |
55
|
|
|
* @param string $language |
|
|
|
|
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
21 |
|
public static function getLocation() { |
60
|
21 |
|
return str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, __DIR__ . '/data/' ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @since 0.1 |
65
|
|
|
* |
66
|
|
|
* @param string $language |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
20 |
|
public static function getTargetByLanguage( $language ) { |
71
|
20 |
|
return self::getLocation() . 'cdb/' . strtolower( $language ) . '.cdb'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @since 0.1 |
76
|
|
|
* |
77
|
|
|
* @param string $word |
78
|
|
|
* |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
12 |
|
public function isStopWord( $word ) { |
82
|
|
|
|
83
|
12 |
|
if ( $this->cdb !== null && $this->cdb->get( $word ) !== false ) { |
84
|
11 |
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
5 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @since 0.1 |
92
|
|
|
* |
93
|
|
|
* @param string $location |
94
|
|
|
* @param string $language |
95
|
|
|
* |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
11 |
|
public static function createCdbByLanguage( $location, $language ) { |
99
|
|
|
|
100
|
11 |
|
$language = strtolower( $language ); |
101
|
11 |
|
$source = $location . $language . '.json'; |
102
|
|
|
|
103
|
11 |
|
if ( !file_exists( $source ) ) { |
104
|
1 |
|
throw new RuntimeException( "{$source} is not available." ); |
105
|
|
|
} |
106
|
|
|
|
107
|
10 |
|
$contents = json_decode( file_get_contents( $source ), true ); |
108
|
|
|
|
109
|
10 |
|
if ( !isset( $contents['list'] ) ) { |
110
|
2 |
|
throw new RuntimeException( "JSON is missing the `list` index." ); |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
$writer = Writer::open( |
114
|
8 |
|
self::getTargetByLanguage( $language ) |
115
|
8 |
|
); |
116
|
|
|
|
117
|
8 |
|
foreach ( $contents['list'] as $words ) { |
118
|
8 |
|
$writer->set( trim( $words ), true ); |
|
|
|
|
119
|
8 |
|
} |
120
|
|
|
|
121
|
8 |
|
$writer->close(); |
122
|
|
|
|
123
|
8 |
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..