KeywordLanguages.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\admin\model;
  3. use app\BaseModel;
  4. use think\facade\Db;
  5. class KeywordLanguages extends BaseModel
  6. {
  7. protected $autoWriteTimestamp = true;
  8. protected $createTime = 'created_at';
  9. protected $updateTime = 'updated_at';
  10. // 语言表对应关键词
  11. public function keyword()
  12. {
  13. return $this->belongsTo(Keyword::class, 'keyword_id');
  14. }
  15. //根据聊天内容匹配关键词
  16. public static function getKeywordByContent($content){
  17. $content = strip_tags($content);
  18. //缓存关键词列表
  19. $keywordList = cache('keywordList');
  20. //if (!$keywordList) {
  21. $keywordList = self::where('status', 1)->field(['id','language_code', 'name'])->order('weight','desc')->select()->toArray();
  22. cache('keywordList', $keywordList);
  23. //}
  24. //$keyword_ids = KeywordLanguages::getKeywordByContent($param['content']);
  25. $list = [];
  26. $all_match = [];//完全匹配的关键词
  27. $match = []; //模糊匹配的关键词
  28. foreach ($keywordList as $keyword) {
  29. //判断content是否包含关键词
  30. if (strpos($content, $keyword['name']) !== false) {
  31. if ($keyword['name'] == $content) {
  32. $all_match[] = $keyword['id'];
  33. } else {
  34. $match[] = $keyword['id'];
  35. }
  36. $list[] = $keyword['id'];
  37. }
  38. }
  39. if ($all_match) {
  40. Db::name('keyword_languages')->whereIn('id', $all_match)->inc('all_match_num', 1)->update();
  41. }
  42. if ($match) {
  43. Db::name('keyword_languages')->whereIn('id', $match)->inc('match_num', 1)->update();
  44. }
  45. return $list;
  46. }
  47. }