LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 比较两文本相似度

admin
2023年3月22日 16:41 本文热度 532

这个比较文本用到的主要是余弦定理比较文本相似度,具体原理右转某度,主要适用场景是在考试系统中的简答题概述,可根据权重自动打分,感觉实用性蛮广的。

先说下思路:

  1. 文本分词,中文于英文不同,规范的英文每个都有空格自动分词,中文则是连成长串,我们只有一一比对每个词出现的频率做简单的比较,在这里使用到了SCWS的一个分词api接口http://www.ftphp.com/scws/api.php(仅支持POST,因为要模拟Http请求,所以请求时间也是根据具体环境而定,所以可以自己手写一些字典,本地分词要来的快)。但是用此接口分词过程中,标点符号不会被去掉,所以需要自己手动写方法去掉标点。

  2. 获取两个文本的去重复并集

  3. 比较每个词出现的频率

  4. 根据余弦定理计算权重

下面是具体的代码段(请求是在网上找的,原链接找不到了。。)

  1. class Program

  2.    {

  3.        static void Main(string[] args)

  4.        {

  5.            Console.Write(Sim("床前明月光,疑是地上霜", "床前明月光,疑是地上霜"));

  6.        }

  7.        public static double Sim(string txt1, string txt2)

  8.        {

  9.            List<string> sl1 = Segment(txt1);

  10.            List<string> sl2 = Segment(txt2);

  11.            //去重

  12.            List<string> sl = sl1.union(sl2).ToList<string>();

  13.            //获取重复次数

  14.            List<int> arrA = new List<int>();

  15.            List<int> arrB = new List<int>();

  16.  

  17.            foreach (var str in sl)

  18.            {

  19.                arrA.Add(sl1.where(x => x == str).Count());

  20.                arrB.Add(sl2.where(x => x == str).Count());

  21.            }

  22.            //计算商

  23.            double num = 0;

  24.            //被除数

  25.            double numA = 0;

  26.            double numB = 0;

  27.            for (int i = 0; i < sl.Count; i++)

  28.            {

  29.                num += arrA[i] * arrB[i];

  30.                numA += Math.Pow(arrA[i], 2);

  31.                numB += Math.Pow(arrB[i], 2);

  32.            }

  33.            double cos = num / (Math.Sqrt(numA)* Math.Sqrt(numB));

  34.            return cos;

  35.        }

  36.        public static List<string> Segment(string str)

  37.        {

  38.            List<string> sl = new List<string>();

  39.            try

  40.            {

  41.                string s = string.Empty;

  42.                System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();

  43.                // 将提交的字符串数据转换成字节数组

  44.                byte[] postData = System.Text.Encoding.ASCII.GetBytes("data=" + System.Web.HttpUtility.UrlEncode(str) + "&respond=json&charset=utf8&ignore=yes&duality=no&traditional=no&multi=0");

  45.  

  46.                // 设置提交的相关参数

  47.                System.Net.HttpWebRequest request = System.Net.WebRequest.create("http://www.ftphp.com/scws/api.php") as System.Net.HttpWebRequest;

  48.                request.Method = "POST";

  49.                request.KeepAlive = false;

  50.                request.ContentType = "application/x-www-form-urlencoded";

  51.                request.CookieContainer = cookieContainer;

  52.                request.ContentLength = postData.Length;

  53.  

  54.                // 提交请求数据

  55.                System.IO.Stream outputStream = request.GetRequestStream();

  56.                outputStream.Write(postData, 0, postData.Length);

  57.                outputStream.Close();

  58.  

  59.                // 接收返回的页面

  60.                System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;

  61.                System.IO.Stream responseStream = response.GetResponseStream();

  62.                System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));

  63.                string val = reader.ReadToEnd();

  64.  

  65.                Newtonsoft.Json.Linq.JObject results = Newtonsoft.Json.Linq.JObject.Parse(val);

  66.                foreach (var item in results["words"].Children())

  67.                {

  68.                    Newtonsoft.Json.Linq.JObject word = Newtonsoft.Json.Linq.JObject.Parse(item.ToString());

  69.                    var sss = word["word"].ToString();

  70.                    //判断是否为标点符

  71.                    bool offom = false;

  72.                    foreach (char t in sss)

  73.                    {

  74.                        if (t >= 0x4e00 && t <= 0x9fbb)

  75.                        {

  76.                            offom = true;

  77.                        }

  78.                        else

  79.                        {

  80.                            offom = false;

  81.                        }

  82.                    }

  83.                    if (offom)

  84.                    {

  85.                        sl.Add(sss);

  86.                    }

  87.                }

  88.            }

  89.            catch

  90.            {

  91.            }

  92.  

  93.            return sl;

  94.        }

  95.    }

  以上 感觉很多地方都可以优化,以后想到更好的再重新来一遍


该文章在 2023/3/22 16:41:37 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved