12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
-
-
-
- namespace crmeb\services;
-
-
- use PullWord\PullWord;
-
- /**
- * 分词搜索类
- * Class VicWordService
- */
- class VicWordService
- {
-
- private static $instance = null;
-
-
- public function __construct()
- {
- }
-
- private function __clone()
- {
- }
-
- public static function instance()
- {
- if (self::$instance === null) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
- public function getWord($str)
- {
- try {
- $pullWord = new PullWord($str);
- $result = $pullWord->pull()->toJson()->get();
- $result = json_decode($result, true);
- $data = is_array($result) ? array_column($result, 't') : [];
- } catch (\Throwable $e) {
- $data = [];
- }
- //没有获取分词时,加上原始的词
- if (!count($data)) {
- $data[] = $str;
- }
- return $data;
- }
- }
|