|
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
|
|
|
public function __construct( $target ) { |
|
36
|
|
|
try { |
|
37
|
|
|
$this->cdb = Reader::open( $target ); |
|
|
|
|
|
|
38
|
|
|
} catch( Exception $e ) { |
|
39
|
|
|
// Do nothing |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
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
|
|
|
public static function getLocation() { |
|
60
|
|
|
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
|
|
|
public static function getTargetByLanguage( $language ) { |
|
71
|
|
|
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
|
|
|
public function isStopWord( $word ) { |
|
82
|
|
|
|
|
83
|
|
|
if ( $this->cdb !== null && $this->cdb->get( $word ) !== false ) { |
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @since 0.1 |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $location |
|
94
|
|
|
* @param string $language |
|
95
|
|
|
* |
|
96
|
|
|
* @return boolean |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function createCdbByLanguage( $location, $language ) { |
|
99
|
|
|
|
|
100
|
|
|
$language = strtolower( $language ); |
|
101
|
|
|
$source = $location . $language . '.json'; |
|
102
|
|
|
|
|
103
|
|
|
if ( !file_exists( $source ) ) { |
|
104
|
|
|
throw new RuntimeException( "{$source} is not available." ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$contents = json_decode( file_get_contents( $source ), true ); |
|
108
|
|
|
|
|
109
|
|
|
if ( !isset( $contents['list'] ) ) { |
|
110
|
|
|
throw new RuntimeException( "JSON is missing the `list` index." ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$writer = Writer::open( |
|
114
|
|
|
self::getTargetByLanguage( $language ) |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
foreach ( $contents['list'] as $words ) { |
|
118
|
|
|
$writer->set( trim( $words ), true ); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$writer->close(); |
|
122
|
|
|
|
|
123
|
|
|
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..