You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VicWordService.php 937B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace crmeb\services;
  3. use PullWord\PullWord;
  4. /**
  5. * 分词搜索类
  6. * Class VicWordService
  7. */
  8. class VicWordService
  9. {
  10. private static $instance = null;
  11. public function __construct()
  12. {
  13. }
  14. private function __clone()
  15. {
  16. }
  17. public static function instance()
  18. {
  19. if (self::$instance === null) {
  20. self::$instance = new self();
  21. }
  22. return self::$instance;
  23. }
  24. public function getWord($str)
  25. {
  26. try {
  27. $pullWord = new PullWord($str);
  28. $result = $pullWord->pull()->toJson()->get();
  29. $result = json_decode($result, true);
  30. $data = is_array($result) ? array_column($result, 't') : [];
  31. } catch (\Throwable $e) {
  32. $data = [];
  33. }
  34. //没有获取分词时,加上原始的词
  35. if (!count($data)) {
  36. $data[] = $str;
  37. }
  38. return $data;
  39. }
  40. }