maxodrom / yii2-phpmorphy
Yii2 PHPMorphy 组件
1.0.2
2018-06-07 15:53 UTC
Requires
- maxakawizard/phpmorphy: ~1.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- codeception/codeception: ~2.0
- codeception/specify: ~1.1
- codeception/verify: ~1.0
- phpunit/phpunit: ^7.0
This package is not auto-updated.
Last update: 2024-09-19 19:59:57 UTC
README
为包装 maxakawizard/phpmorphy 并移植到 Yii2 组件基础的封装器。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require maxodrom/yii2-phpmorphy:~1.0
或者
"maxodrom/yii2-phpmorphy": "~1.0"
将以下内容添加到您的 composer.json 的 require 部分。
使用方法
在您的 Yii2 应用程序配置中定义新组件 (@app/config/web.php)
$config = [ 'id' => 'app-frontend', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'language' => 'ru-RU', 'aliases' => [ '@bower' => '@vendor/bower-asset', '@npm' => '@vendor/npm-asset', ], 'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 'cookieValidationKey' => '', ], 'cache' => [ 'class' => 'yii\caching\FileCache', ], 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, ], 'db' => $db, /** * Set component here! */ 'yiimorphy' => [ 'class' => 'maxodrom\phpmorphy\components\YiiMorphy', // 'language' => 'ru', // or 'uk', or 'de' // 'options' => [], // your options which will be passed to \phpMorphy's constructor ], ], 'modules' => [], 'params' => $params, ];
在您的代码中,您可以通过标准方式使用组件
/** @var \phpMorphy $morphy */ $morphy = Yii::$app->yiimorphy->morphy; $word_one = 'КОТ'; $word_two = 'СОБАКА'; try { // word by word processing // each function return array with result or FALSE when no form(s) for given word found(or predicted) $base_form = $morphy->getBaseForm($word_one); $all_forms = $morphy->getAllForms($word_one); $pseudo_root = $morphy->getPseudoRoot($word_one); if(false === $base_form || false === $all_forms || false === $pseudo_root) { die("Can`t find or predict $word_one word"); } echo 'base form = ' . implode(', ', $base_form) . "\n"; echo 'all forms = ' . implode(', ', $all_forms) . "\n"; echo "Testing bulk mode...\n"; // bulk mode speed-ups processing up to 50-100%(mainly for getBaseForm method) // in bulk mode all function always return array $bulk_words = array($word_one, $word_two); $base_form = $morphy->getBaseForm($bulk_words); $all_forms = $morphy->getAllForms($bulk_words); $pseudo_root = $morphy->getPseudoRoot($bulk_words); // Bulk result format: // array( // INPUT_WORD1 => array(OUTWORD1, OUTWORD2, ... etc) // INPUT_WORD2 => FALSE <-- when no form for word found(or predicted) // ) echo 'bulk mode base form = ' . implode(', ', $base_form[$word_one]) . ' ' . implode(', ', $base_form[$word_two]) . "\n"; echo 'bulk mode all forms = ' . implode(', ', $all_forms[$word_one]) . ' ' . implode(', ', $all_forms[$word_two]) . "\n"; // You can also retrieve all word forms with graminfo via getAllFormsWithGramInfo method call // $all_forms_with_gram = $morphy->getAllFormsWithGramInfo($word_one); exit; } catch(\phpMorphy_Exception $e) { die('Error occured while text processing: ' . $e->getMessage()); }