Overview

Namespaces

  • PHP
  • Sastrawi
    • Dictionary
    • Morphology
      • Disambiguator
    • Specification
    • Stemmer
      • Cache
      • ConfixStripping
      • Context
        • Visitor
      • Filter
    • StopWordRemover

Classes

  • CachedStemmer
  • Stemmer
  • StemmerFactory

Interfaces

  • StemmerInterface
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * Sastrawi (https://github.com/sastrawi/sastrawi)
 4:  *
 5:  * @link      http://github.com/sastrawi/sastrawi for the canonical source repository
 6:  * @license   https://github.com/sastrawi/sastrawi/blob/master/LICENSE The MIT License (MIT)
 7:  */
 8: 
 9: namespace Sastrawi\Stemmer;
10: 
11: use Sastrawi\Dictionary\ArrayDictionary;
12: 
13: /**
14:  * Stemmer factory helps creating pre-configured stemmer
15:  */
16: class StemmerFactory
17: {
18:     const APC_KEY = 'sastrawi_cache_dictionary';
19: 
20:     /**
21:      * @return \Sastrawi\Stemmer\Stemmer
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: 
API documentation generated by ApiGen 2.8.0