celestial/lexicology

PHP 词汇学库。

0.0.3 2017-08-22 05:38 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:46:39 UTC


README

Build Status

Lexicology

PHP 词汇学库。

虽然我们只测试 PHP 7+,但这个库也可以与 PHP 5.x 一起使用。PHP 7+ 中排序方法在处理相等的排序值方面发生了变化,因此建议数组将有所不同。

  • 根据词汇比较从数组中建议值
  • 基于词汇比较返回排序数组
  • 从数组中选择最佳匹配
  • 从数组中选择最佳匹配关联

核心词汇比较

还可以通过扩展 Celestial\Lexicology\Method\AbstractMethod 并实现 Celestial\Lexicology\Method\MethodInterface 来自定义词汇比较。请参阅自定义方法

安装

Composer

composer require celestial\lexicology

或 composer.json

{
   "require": {
       "celestial/lexicology": "^0.1"
   }
}

使用

建议

Suggestion 类将建议一个与针值密切匹配的数组或值。默认方法是 PregGrep,但可以更改为其他方法或自定义方法。

<?php

  use Celestial\Lexicology\Suggestion;
  
  $suggestionOptions = [
    'string',  
    'new string',  
    'value',  
    'variable'  
  ];
  
  $suggestion = new Suggestion();
  $suggestions = $suggestion->getSuggestions('string', $suggestionOptions);
  
  print_r($suggestions);
  
//Array
//(
//    [0] => string
//    [1] => new string
//)

尝试获取单个“最佳”建议值将返回一个字符串或抛出异常。如果您需要抑制异常并返回非标准或共享值(例如元字段或常量),请使用第四个参数来覆盖结果。

<?php

  use Celestial\Lexicology\Suggestion;
  
  $suggestionOptions = [
    'string',  
    'new string',  
    'value',  
    'variable'  
  ];
  
  $suggestion = new Suggestion();
  $suggestions = $suggestion->getSingleSuggestion('string', $suggestionOptions);
  print_r($suggestions);
    // ['string']

抑制异常

<?php
    use Celestial\Lexicology\Suggestion;
    $suggestion = new Suggestion();
    $suggestions = $suggestion->getSingleSuggestion('string',[], null, 'meta');
    print_r($suggestions);
    // ['meta']

自定义方法

自定义方法定义必须实现 FilterInterfaceRateInterface

<?php
  use Celestial\Lexicology\Method\AbstractMethod;
  use Celestial\Lexicology\Method\Interfaces\FilterInterface;
  use Celestial\Lexicology\Method\Interfaces\SortInterface;
  
  class CustomMethod extends AbstractMethod implements SortInterface, FilterInterface
  {
      use \Celestial\Lexicology\Method\Traits\SortTrait;
      
      /**
       * Return a sort value if either a or b match.
       * 
       * @inheritdoc 
       */
      public function sortPair($a, $b) {
        if ($a === $b) {
            return 0;
        } elseif ($a === $this->getField()) {
            return 1;
        } elseif ($b === $this->getField()) {
            return -1;
        }
        return null;
      }
      
      /**
       * Return a filter array of string that have more than 5 characters
       * 
       * @inheritdoc
       */
      public function filter($possibleValues) {
        return array_values(array_filter($possibleValues, function($value){
            return (strlen($value) > 5);
        }));
      }
  
  }

虽然这个自定义方法没有做什么特别的事情,但它是一个词汇方法的接口的基本示例。

<?php


  use Celestial\Lexicology\Suggestion;
  use Lexicology\Test\Method\CustomMethod;
  
  $suggestionOptions = [
    'string',  
    'strings',  
    'new string',  
    'value',  
    'variable'  
  ];
  
  $suggestion = new Suggestion();
  $suggestions = $suggestion->getSuggestions('string', $suggestionOptions, CustomMethod::class);
  
  print_r($suggestions);
  
//Array
//(
//    [0] => new string
//    [1] => strings
//    [2] => variable
//    [3] => string
//)