VicWordService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use PullWord\PullWord;
  13. /**
  14. * 分词搜索类
  15. * Class VicWordService
  16. * @package crmeb\services
  17. */
  18. class VicWordService
  19. {
  20. private static $instance = null;
  21. /**
  22. * 请求地址
  23. * @var string
  24. */
  25. protected $url = 'https://sms.crmeb.net/api/v2/open/keyword';
  26. // protected $url = 'http://test-api.crmeb.com/api/v2/open/keyword';
  27. public function __construct()
  28. {
  29. }
  30. private function __clone()
  31. {
  32. }
  33. public static function instance()
  34. {
  35. if (self::$instance === null) {
  36. self::$instance = new self();
  37. }
  38. return self::$instance;
  39. }
  40. public function getWord($str)
  41. {
  42. try {
  43. $res = HttpService::getRequest($this->url ,['keyword' => $str]);
  44. $data = json_decode($res, true) ?: [];
  45. $data = array_column($data['data'] ?? [], 0);
  46. } catch (\Throwable $e) {
  47. $data = [];
  48. }
  49. //没有获取分词时,加上原始的词
  50. if (!count($data)) {
  51. $data[] = $str;
  52. }
  53. return $data;
  54. }
  55. public function getWordV1($str)
  56. {
  57. try {
  58. $pullWord = new PullWord($str);
  59. $result = $pullWord->pull()->toJson()->get();
  60. $result = json_decode($result, true);
  61. $data = is_array($result) ? array_column($result, 't') : [];
  62. } catch (\Throwable $e) {
  63. $data = [];
  64. }
  65. //没有获取分词时,加上原始的词
  66. if (!count($data)) {
  67. $data[] = $str;
  68. }
  69. return $data;
  70. }
  71. }