Skip to content

Commit dc90f21

Browse files
committed
Added input word auto preprocessing, fixed minor reflection error
1 parent 286f124 commit dc90f21

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

config/config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
return [
44
'common_options' => [
55
'storage' => phpMorphy::STORAGE_FILE,
6-
'predict_by_suffix' => true,
7-
'predict_by_db' => true,
8-
'graminfo_as_text' => true,
6+
'predict_by_suffix' => true,
7+
'predict_by_db' => true,
8+
'graminfo_as_text' => true,
99
],
1010

1111
'morphies' => [

src/MorphyManager.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace SEOService2020\Morphy;
44

5+
use ReflectionMethod;
56
use BadMethodCallException;
67
use InvalidArgumentException;
78
use phpMorphy;
89

910

1011
class MorphyManager
1112
{
13+
use PreprocessWord;
14+
1215
protected $morphies;
1316

1417
public function __construct(array $morphies)
@@ -37,6 +40,10 @@ public function morphy(string $name): ?Morphy
3740

3841
public function __call($name, $arguments)
3942
{
43+
if ((new ReflectionMethod(phpMorphy::class, $name))->isStatic()) {
44+
return self::__callStatic($name, $arguments);
45+
}
46+
4047
if (count($arguments) < 1) {
4148
throw new BadMethodCallException(
4249
"Too few arguments provided: at least morphy name expected"
@@ -48,7 +55,12 @@ public function __call($name, $arguments)
4855
throw new InvalidArgumentException("Unknown morphy: $morphyName");
4956
}
5057

51-
return call_user_func_array([$this->morphies[$morphyName], $name], $arguments);
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);
5264
}
5365

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

src/PreprocessWord.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace SEOService2020\Morphy;
4+
5+
use Illuminate\Support\Str;
6+
7+
8+
trait PreprocessWord
9+
{
10+
protected static function preprocessedWord(string $word, Morphy $morphy): string
11+
{
12+
return $morphy->isInUpperCase() ? Str::upper(trim($word)) : Str::lower(trim($word));
13+
}
14+
15+
protected static function needWordPreprocess(string $methodName): bool
16+
{
17+
return in_array($methodName, [
18+
'findWord',
19+
'lemmatize',
20+
'getBaseForm',
21+
'getAllForms',
22+
'getPseudoRoot',
23+
'getPartOfSpeech',
24+
'getAllFormsWithAncodes',
25+
'getAllFormsWithGramInfo',
26+
'getAncode',
27+
'getGramInfo',
28+
'getGramInfoMergeForms',
29+
'getAnnotForWord',
30+
'castFormByAncode',
31+
'castFormByGramInfo',
32+
'castFormByPattern',
33+
'getAncode',
34+
]);
35+
}
36+
}

0 commit comments

Comments
 (0)