1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Sastrawi\Stemmer;
10:
11: use Sastrawi\Dictionary\ArrayDictionary;
12:
13: 14: 15:
16: class StemmerFactory
17: {
18: const APC_KEY = 'sastrawi_cache_dictionary';
19:
20: 21: 22:
23: public function createStemmer($isDev = false)
24: {
25: $words = $this->getWords($isDev);
26: $dictionary = new ArrayDictionary($words);
27: $stemmer = new Stemmer($dictionary);
28:
29: $resultCache = new Cache\ArrayCache();
30: $cachedStemmer = new CachedStemmer($resultCache, $stemmer);
31:
32: return $cachedStemmer;
33: }
34:
35: protected function getWords($isDev = false)
36: {
37: if ($isDev || !function_exists('apc_fetch')) {
38: $words = $this->getWordsFromFile();
39: } else {
40:
41: $words = apc_fetch(self::APC_KEY);
42:
43: if ($words === false) {
44: $words = $this->getWordsFromFile();
45: apc_store(self::APC_KEY, $words);
46: }
47: }
48:
49: return $words;
50: }
51:
52: protected function getWordsFromFile()
53: {
54: $dictionaryFile = __DIR__ . '/../../../data/kata-dasar.txt';
55: if (!is_readable($dictionaryFile)) {
56: throw new \Exception('Dictionary file is missing. It seems that your installation is corrupted.');
57: }
58:
59: return explode(PHP_EOL, file_get_contents($dictionaryFile));
60: }
61: }
62: