package service import "strings" // searchKeywordReplys 搜索关键词并返回对应的回复内容 func SearchKeywordReplys(replys string, keyword string) string { var replyContent string if replys == "" || keyword == "" { return replyContent } replyLines := strings.Split(replys, "\n") maxMatch := 0 // 最大匹配关键词数量 bestReply := "" // 最佳回复内容 for _, reply := range replyLines { qa := strings.Split(reply, "#") if len(qa) == 2 { questions := strings.Split(qa[0], "|") matchCount := countMatchingKeywords(keyword, questions) if matchCount > maxMatch { bestReply = qa[1] // 更新最佳回复 maxMatch = matchCount } } } return bestReply } // countMatchingKeywords 计算字符串包含的关键词数量 func countMatchingKeywords(inputString string, keywords []string) int { count := 0 for _, kw := range keywords { if kw == "" { continue } if strings.Contains(inputString, kw) { count++ } } return count }