Skip to content

Commit 3c75e8c

Browse files
committed
Moved input preprocessing to Morphy class to fix direct morphy usage issues
1 parent 6bd9869 commit 3c75e8c

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

src/Morphy.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
namespace SEOService2020\Morphy;
44

55
use InvalidArgumentException;
6+
67
use phpMorphy;
8+
use phpMorphy_GrammemsProvider_GrammemsProviderInterface;
79

810

911
class Morphy extends phpMorphy
1012
{
13+
use PreprocessWord;
14+
1115
public const russianLang = 'ru_RU';
1216
public const englishLang = 'en_EN';
1317
public const ukrainianLang = 'uk_UA';
@@ -55,4 +59,100 @@ public function __construct(
5559

5660
parent::__construct($this->dictsPath, $this->language, $this->options);
5761
}
62+
63+
public function findWord($word, $type = self::NORMAL) {
64+
return parent::findWord(self::preprocessedWord($word, $this), $type);
65+
}
66+
67+
public function getBaseForm($word, $type = self::NORMAL) {
68+
return parent::getBaseForm(self::preprocessedWord($word, $this), $type);
69+
}
70+
71+
public function getAllForms($word, $type = self::NORMAL) {
72+
return parent::getAllForms(self::preprocessedWord($word, $this), $type);
73+
}
74+
75+
public function getPseudoRoot($word, $type = self::NORMAL) {
76+
return parent::getPseudoRoot(self::preprocessedWord($word, $this), $type);
77+
}
78+
79+
public function getPartOfSpeech($word, $type = self::NORMAL) {
80+
return parent::getPartOfSpeech(self::preprocessedWord($word, $this), $type);
81+
}
82+
83+
public function getAllFormsWithAncodes($word, $type = self::NORMAL) {
84+
return parent::getAllFormsWithAncodes(self::preprocessedWord($word, $this), $type);
85+
}
86+
87+
public function getAllFormsWithGramInfo($word, $asText = true, $type = self::NORMAL) {
88+
return parent::getAllFormsWithGramInfo(
89+
self::preprocessedWord($word, $this), $asText, $type
90+
);
91+
}
92+
93+
public function getAncode($word, $type = self::NORMAL) {
94+
return parent::getAncode(self::preprocessedWord($word, $this), $type);
95+
}
96+
97+
public function getGramInfo($word, $type = self::NORMAL) {
98+
return parent::getGramInfo(self::preprocessedWord($word, $this), $type);
99+
}
100+
101+
public function getGramInfoMergeForms($word, $type = self::NORMAL) {
102+
return parent::getGramInfoMergeForms(self::preprocessedWord($word, $this), $type);
103+
}
104+
105+
public function castFormByAncode(
106+
$word,
107+
$ancode,
108+
$commonAncode = null,
109+
$returnOnlyWord = false,
110+
$callback = null,
111+
$type = self::NORMAL
112+
) {
113+
return parent::castFormByAncode(
114+
self::preprocessedWord($word, $this),
115+
self::preprocessedWord($ancode, $this),
116+
self::preprocessedWord($commonAncode, $this),
117+
$returnOnlyWord,
118+
$callback,
119+
$type
120+
);
121+
}
122+
123+
public function castFormByGramInfo(
124+
$word,
125+
$partOfSpeech,
126+
$grammems,
127+
$returnOnlyWord = false,
128+
$callback = null,
129+
$type = self::NORMAL
130+
) {
131+
return parent::castFormByGramInfo(
132+
self::preprocessedWord($word, $this),
133+
self::preprocessedWord($partOfSpeech, $this),
134+
$grammems,
135+
$returnOnlyWord,
136+
$callback,
137+
$type
138+
);
139+
}
140+
141+
public function castFormByPattern(
142+
$word,
143+
$patternWord,
144+
phpMorphy_GrammemsProvider_GrammemsProviderInterface $grammemsProvider = null,
145+
$returnOnlyWord = false,
146+
$callback = null,
147+
$type = self::NORMAL
148+
) {
149+
return parent::castFormByPattern(
150+
self::preprocessedWord($word, $this),
151+
self::preprocessedWord($patternWord, $this),
152+
$grammemsProvider,
153+
$returnOnlyWord,
154+
$callback,
155+
$type
156+
);
157+
}
58158
}

src/MorphyManager.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
use ReflectionMethod;
66
use BadMethodCallException;
77
use InvalidArgumentException;
8+
89
use phpMorphy;
910

1011

1112
class MorphyManager
1213
{
13-
use PreprocessWord;
14-
1514
protected $morphies;
1615

1716
public function __construct(array $morphies)
@@ -55,12 +54,7 @@ public function __call($name, $arguments)
5554
throw new InvalidArgumentException("Unknown morphy: $morphyName");
5655
}
5756

58-
$morphy = $this->morphies[$morphyName];
59-
if (self::needWordPreprocess($name) && !empty($arguments)) {
60-
$arguments[0] = self::preprocessedWord($arguments[0], $morphy);
61-
}
62-
63-
return call_user_func_array([$morphy, $name], $arguments);
57+
return call_user_func_array([$this->morphies[$morphyName], $name], $arguments);
6458
}
6559

6660
public static function __callStatic($name, $arguments)

src/PreprocessWord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected static function preprocessedWord(string $word, Morphy $morphy): string
1212
return $morphy->isInUpperCase() ? Str::upper(trim($word)) : Str::lower(trim($word));
1313
}
1414

15-
protected static function needWordPreprocess(string $methodName): bool
15+
public static function takesWordParameter(string $methodName): bool
1616
{
1717
return in_array($methodName, [
1818
'findWord',

0 commit comments

Comments
 (0)