igramnet/laravel-phpmorphy

laravel-phpmorphy - Laravel 对 phpMorphy 库的封装。

dev-main 2024-05-15 18:37 UTC

This package is auto-updated.

Last update: 2024-09-15 19:19:54 UTC


README

Latest Stable Version Total Downloads tests codecov License

laravel-phpmorphy 是一个支持 PHP7-8 的 phpMorphy 库的 Laravel 封装。此扩展基于 SEOService2020/laravel-phpmorphy

phpMorphy 是一个针对俄语、乌克兰语、英语和德语的形态分析库。

安装

从您的终端运行以下命令

composer require igramnet/laravel-phpmorphy

或者将此添加到您的 composer.json 文件中的 require 部分

{
    "require": {
        "igramnet/laravel-phpmorphy": "~1.0"
    }
}

然后运行 composer update

配置

默认设置在 config/morphy.php 中。将此文件复制到您的配置目录以修改值。您可以使用以下命令发布配置

php artisan vendor:publish --provider="igramnet\Morphy\MorphyServiceProvider"

这是已发布文件的目录内容

return [
    'common_options' => [
        'storage' => phpMorphy::STORAGE_FILE,
        'predict_by_suffix' => true,
        'predict_by_db' => true,
        'graminfo_as_text' => true,
    ],

    'morphies' => [
        [
            // phpMorphy instance name
            // specific morphy can be accessed through Morphy::morphy($name) method
            'name' => igramnet\Morphy\Morphy::russianLang,

            // phpMorphy language
            // see Morphy class for available values
            'language' => igramnet\Morphy\Morphy::russianLang,

            // phpMorphy options
            // if not specified or null specified, default options will be used
            // to use common options from this config, specify []
            'options' => [],
            
            // dicts directory path
            // if not specified or null specified, default dicts path will be used
            'dicts_path' => null,

            // values are 'storage', 'filesystem'
            // 'storage': dicts will be taken from laravel storage (storage/app folder)
            // 'filesystem': dicts will be taken by specified path as-is
            // must be specified when dicts_path is not null
            'dicts_storage' => 'storage',
        ],
    ],
];

用法

Wrapper 会自动准备输入词以供 phpMorphy 使用:它会应用 trim 并根据词典选项将词转换为大写或小写。

直接使用 Wrapper

use igramnet\Morphy\Morphy;
$morphy = new Morphy(Morphy::englishLang);
echo $morphy->getPseudoRoot('fighty');

或通过 Laravel Facade

use igramnet\Morphy\Facade\Morphy as Morphies;
// first parameter is the name of morphy in config
Morphies::getPseudoRoot('ru', 'Бойцовый');

// get morphy and call methods in regular manner
Morphies::morphy('ru')->lemmatize('   бойцовый');  // word will be trimmed

// get all morphies, returns array like ['name' => Morphy]
Morphies::morphies();

// get all morphies with specific locale, returns array like ['name' => Morphy]
Morphies::morphies(Morphy::russianLang);

// you can call phpMorphy static methods as well
Morphies::getDefaultDictsDir();

注意

您只能从 morphy 对象直接访问 morphy 属性,而不能从 facade 访问。

添加 facade 支持

此包允许 Laravel 直接支持 facade,但您可以在 config/app.php 中显式添加 facade 支持

部分 providers

igramnet\Morphy\MorphyServiceProvider::class,

部分 aliases

'Morphy' => igramnet\Morphy\Facade\Morphy::class,

变更日志

有关最近更改的详细信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全性

如果您发现任何安全问题,请通过电子邮件 sergotail@mail.ru 而不是使用问题跟踪器。

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件