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: class CachedStemmer implements StemmerInterface
12: {
13: protected $cache;
14:
15: protected $delegatedStemmer;
16:
17: public function __construct(Cache\CacheInterface $cache, StemmerInterface $delegatedStemmer)
18: {
19: $this->cache = $cache;
20: $this->delegatedStemmer = $delegatedStemmer;
21: }
22:
23: public function stem($text)
24: {
25: $normalizedText = Filter\TextNormalizer::normalizeText($text);
26:
27: $words = explode(' ', $normalizedText);
28: $stems = array();
29:
30: foreach ($words as $word) {
31: if ($this->cache->has($word)) {
32: $stems[] = $this->cache->get($word);
33: } else {
34: $stem = $this->delegatedStemmer->stem($word);
35: $this->cache->set($word, $stem);
36: $stems[] = $stem;
37: }
38: }
39:
40: return implode(' ', $stems);
41: }
42:
43: public function getCache()
44: {
45: return $this->cache;
46: }
47: }
48: