1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Sastrawi\Stemmer\Context\Visitor;
10:
11: use Sastrawi\Stemmer\Context\ContextInterface;
12: use Sastrawi\Stemmer\Context\Removal;
13: use Sastrawi\Morphology\Disambiguator\DisambiguatorInterface;
14:
15: abstract class AbstractDisambiguatePrefixRule implements VisitorInterface
16: {
17: protected $disambiguators = array();
18:
19: abstract protected function initDisambiguators();
20:
21: public function visit(ContextInterface $context)
22: {
23: if (empty($this->disambiguators)) {
24: $this->initDisambiguators();
25: }
26:
27: $result = null;
28:
29: foreach ($this->disambiguators as $disambiguator) {
30: $result = $disambiguator->disambiguate($context->getCurrentWord());
31:
32: if ($context->getDictionary()->contains($result)) {
33: break;
34: }
35: }
36:
37: if ($result === null) {
38: return;
39: }
40:
41: $removedPart = preg_replace("/$result/", '', $context->getCurrentWord(), 1);
42:
43: $removal = new Removal(
44: $this,
45: $context->getCurrentWord(),
46: $result,
47: $removedPart,
48: 'DP'
49: );
50:
51: $context->addRemoval($removal);
52: $context->setCurrentWord($result);
53: }
54:
55: public function addDisambiguators(array $disambiguators)
56: {
57: foreach ($disambiguators as $disambiguator) {
58: $this->addDisambiguator($disambiguator);
59: }
60: }
61:
62: public function addDisambiguator(DisambiguatorInterface $disambiguator)
63: {
64: $this->disambiguators[] = $disambiguator;
65: }
66: }
67: