KeywordLanguages.php 1.5 KB

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