C#从零搭建微信机器人(二)分词匹配组件【jieba】的使用

本文介绍了在微信机器人的开发中,如何使用C#的jieba组件进行中文分词,并通过CosineSimilarity计算相似度,实现在模糊匹配搜索时的高效匹配功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇文章我们讲解了微信机器人的环境搭建及演示,这期我们来说一下其中在模糊匹配搜索时用到的Segement子项目,也就是其中的中文分词匹配器。

一、原理介绍:

其实这个子项目中的分词插件和solr的IK分词器类似,都是可以支持将一句完整的中文拆成多个单词。例如:我是中国人。 分词器会拆分成:我,是,中国,中国人。然后将拆分的词组和你要匹配的内容进行一个相似度匹配。如果超过预设的匹配值。则匹配成功。

二、分词组件的引用:

首先我们需要再创建一个子项目Segements,然后在项目中引用jieba这个组件,这里需要使用nuget(你可以理解是c#的maven),在nuget中搜索jieba,然后选择安装。
在这里插入图片描述
安装后在项目中查看是否引用到,如下图所示
在这里插入图片描述

三、使用代码详解

using JiebaNet.Segmenter.PosSeg;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Segments
{
    public class Segments
    {
       public static double CalculateCosineSimilarity(List<String> array1, List<String> array2)
        {
            Dictionary<string, int> wordCount1 = new Dictionary<string, int>();
            Dictionary<string, int> wordCount2 = new Dictionary<string, int>();

            foreach (string word in array1)
            {
                if (wordCount1.ContainsKey(word))
                    wordCount1[word]++;
                else
                    wordCount1[word] = 1;
            }

            foreach (string word in array2)
            {
                if (wordCount2.ContainsKey(word))
                    wordCount2[word]++;
                else
                    wordCount2[word] = 1;
            }

            double dotProduct = 0;
            double magnitude1 = 0;
            double magnitude2 = 0;

            foreach (KeyValuePair<string, int> entry in wordCount1)
            {
                int count1 = entry.Value;
                int count2 = wordCount2.ContainsKey(entry.Key) ? wordCount2[entry.Key] : 0;

                dotProduct += count1 * count2;
                magnitude1 += count1 * count1;
            }

            foreach (KeyValuePair<string, int> entry in wordCount2)
            {
                int count2 = entry.Value;
                magnitude2 += count2 * count2;
            }

            magnitude1 = Math.Sqrt(magnitude1);
            magnitude2 = Math.Sqrt(magnitude2);

            if (magnitude1 == 0 || magnitude2 == 0)
                return 0;

            return dotProduct / (magnitude1 * magnitude2);
        }

        private static string getErrorCode(string question)
        {
            string pattern = @"\d+";
            Match match = Regex.Match(question, pattern);
            if (match.Success)
            {
                return match.Value;
            }
            else
            {
                return "";
            }
        }

        public static List<JObject> selectQuestion(string userInput, List<JObject> param2)
        {
            //问题答案表
            var questionAnswerTable = new Dictionary<string, JObject> { };
            String temp = getErrorCode(userInput);
            if (!temp.Equals(""))
            {
                userInput = temp;
            }
            for (int i = 0; i < param2.Count; i++)
            {
                String matchStr = "";
                    matchStr = ""+param2[i]["errorCode"] + param2[i]["errorDetail"];

                if (!questionAnswerTable.ContainsKey(matchStr)) { 
                questionAnswerTable.Add(matchStr, param2[i]);
                }
            }


            //  用户输入问题
            //  string userInput = "好像是什么检查好像失败不正确?";
            //使用PosSegmenter对用户输入进行分词和词性标注

            PosSegmenter segmenter = new PosSegmenter();
            var segments = segmenter.Cut(userInput);
            //构造分词列表
            var queryTokens = segments.Select(segment => segment.Word).ToList();
            //在问题答案表中进行匹配
            string bestMatchAnswer = "";

            String mMostMatch_Sentence = "";
            //*******************************【相似匹配度设置】**************************************
            double mMostMatch_Sentence_double = 0.1;
            List<JObject> answerList = new List<JObject>();
            foreach (var kvp in questionAnswerTable)
            {
                var question = kvp.Key;
                var answer = kvp.Value;
                //使用PosSegmenter对问题进行分词和词性标注
                var questionSegments = segmenter.Cut(question);
                //构造问题的分词列表
                var questionTokens = questionSegments.Select(segment => segment.Word).ToList();
                //进行分词匹配,这里可以使用自定义的相似度算法

                //if (queryTokens.SequenceEqual(questionTokens))
                {
                    if (mMostMatch_Sentence_double < CalculateCosineSimilarity(queryTokens, questionTokens))
                    {
                        mMostMatch_Sentence_double = CalculateCosineSimilarity(queryTokens, questionTokens);
                        mMostMatch_Sentence = kvp.Key;
                        if (answerList.Count >= 10)
                        {
                            break;
                        }
                        JObject tempObj =  kvp.Value;
                        tempObj.Add("similar",mMostMatch_Sentence_double);
                        answerList.Add(kvp.Value);
                    }
                    else
                    {

                    }


                    //bestMatchAnswer = answer;
                    // break;
                }
            }

            //string[] array1 = { "我", "爱", "自然语言处理技术" };
            //string[] array2 = { "我", "喜欢", "自然语言处理技术" };

            //double similarity = CalculateCosineSimilarity(array1, array2);
            //Console.WriteLine("相似度: " + similarity);
            answerList = answerList.OrderByDescending(obj => (int)obj["similar"]).ToList();
            return answerList;
        }
    }
}

主要的使用逻辑在这个方法中:
List <JObject> selectQuestion(string userInput, List<JObject> param2)
传入参数有两个:用户提出的问题,和待匹配语料集合。
返回参数:匹配到的语料集合。

            var questionAnswerTable = new Dictionary<string, JObject> { };

这里的questionAnswerTable的key为待匹配的问题,value为整个问题实体。下面会先填充这个集合。然后遍历它与userInput进行匹配。

 //*******************************【相似匹配度设置】**************************************
            double mMostMatch_Sentence_double = 0.1;

注意这里是配置相似度的地方。大家在调试的时候可以通过修改它来控制匹配的精确程度。

差不多就是这些。剩下的代码基本都有注释。大家可以慢慢学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

stalin_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值