AI 智能搜索 开源hanlp实现

 

AI智能搜索

通过网络资源可知有很多种开源方式实现智能搜索,其中hanlpGitHub中响应居高

参考链接:

https://www.hanlp.com/

Java版:https://github.com/hankcs/HanLP
Python版:https://github.com/hankcs/pyhanlp

https://hanlp.hankcs.com/docs/api/restful_java.html

http://www.hankcs.com/nlp/hanlp.html

https://github.com/hankcs/HanLP/releases

 

浅谈拙见

Hanlp说明

想要调用hanlp,首先需要有相应jar包,具体下载地址已在参考链接中

其次,hanlp中包含一个配置文件hanlp.properties

如果想进行相似对比,文本推荐等需要hanlp语料库的支持,故还需语料库文件data-for-1.7.5.zip

 

由于现有需求,需要将jar包转换为dll文件在.net core 文件中引用

转换dll的方法可自行百度,但注意,.net framework .net core所需的dll并非是同一种,不然会报错。

稍后我将我转换的dll文件上传,链接后补

 https://files.cnblogs.com/files/zwbsoft/hanlp-core-1.8.3.dll.zip?t=1668498093   记得去掉zip后缀

所有准备工作完成,将相当于jvm的文件IKVM.OpenJDK.Core.dll和将jar包解析出来的hanlp-core-1.8.3.dll同时引用到.net core 项目中

具体使用如下,相关使用参考官网文档

本人core程序,实践可行几种方式如下

  1. nlp分词

        static void Main(string[] args)

        {

            Console.WriteLine("Hello World!");

            try

            {

                java.lang.System.getProperties().setProperty("java.class.path", @"E:\\install\\hanlp");

                Console.WriteLine(HanLP.segment("你好,欢迎在CSharp中调用HanLP的API! "));}

            catch (Exception ex)

            {

                Console.WriteLine(ex);

            }

        }

说明一下E:\\install\\hanlp”需要把jar包和配置文件,及解压后的data语料库文件夹放在同一目录,该方法其实是通过jvm调用java,使得.net core 能够对其进行方法调用

 

  1. 关键字提取

Console.WriteLine(HanLP.extractKeyword("商品和服务",2));

输出结果

 

 

 

  1. 汉字转拼音

 private static void py()

        {

            string stringpy = "每个青年都应当有远大的抱负";

            //var z = HanLP.convertToPinyinString("报复","",true);

            //Console.WriteLine(z);

            //汉字转拼音搜索   

            var listpy = HanLP.convertToPinyinList(stringpy);

            Console.WriteLine(listpy);

            foreach (var item in listpy.toArray())

            {

                Console.WriteLine(item);

            }

            var firstpyt = HanLP.convertToPinyinFirstCharString(stringpy, "", true);

            Console.WriteLine(firstpyt);

            var firstpyf = HanLP.convertToPinyinFirstCharString(stringpy, " ", false);

            Console.WriteLine(firstpyf);

            //拼音转汉字

 

 

 

            String text = "重载不是重任";

            java.util.List pinyinList = HanLP.convertToPinyinList(text);

            Console.WriteLine("原文,");

            foreach (char c in text.ToCharArray())

            {

                Console.WriteLine("%c,", c);

            }

            Console.WriteLine();

            Console.WriteLine("拼音(数字音调),");

            foreach (Pinyin pinyin in pinyinList.toArray())

            {

                Console.Write("%s,", pinyin);

            }

            Console.WriteLine();

            Console.WriteLine("拼音(符号音调),");

            foreach (Pinyin pinyin in pinyinList.toArray())

            {

                Console.Write("%s,", pinyin.getPinyinWithToneMark());

            }

            Console.WriteLine();

            Console.WriteLine("拼音(无音调),");

            foreach (Pinyin pinyin in pinyinList.toArray())

            {

                Console.WriteLine("%s,", pinyin.getPinyinWithoutTone());

            }

            Console.WriteLine();

            Console.WriteLine("声调,");

            foreach (Pinyin pinyin in pinyinList.toArray())

            {

                Console.Write("%s,", pinyin.getTone());

            }

        }

  1. 繁简互转

string fttext = "知識沒有盡頭,就像海沒有邊際";

      Console.WriteLine(HanLP.convertToSimplifiedChinese(fttext));

效果如下

 

 

 

  1. 文本推荐,耗时最长,如有解决,烦请指教(未成功)

文本推荐原理

假设“你爱我们”和“我们爱你”进行相似度计算,计算机会将每一句话的每个词,根据语料库进行计算,算出词向量值,在通过对每个词向量进行累加就变成了句向量,再将两个计算的句向量值,进行cos余弦计算,最终得出相似匹配度

先贴代码后赘述

 

        public static Vector query(string content)

        {

            if (content == null || content.Length == 0)

            { Console.WriteLine("-------------------");return null; }

            //对句子进行分词,我爱你们--->["我"、"爱"、"你们"]

            object z = NotionalTokenizer.segment(content);//

            java.util.List termList = NotionalTokenizer.segment(content);

            Vector result = new Vector(200);

            int n = 0;

            //WordVectorModel wordVectorModelss = new WordVectorModel("en-vectors.txt");

            WordVectorModel wordVectorModelss = new WordVectorModel("E:\\install\\hanlp\\data\\model\\sogouyuliaoku.txt");

            foreach (Term term in termList.toArray())

            {

                //从word2vec词典中查出这个词的 词向量

                Vector vector = wordVectorModelss.vector(term.word);

                if (vector == null)

                {

                    //如果这是一个oov词,则直接忽略

                    continue;

                }

                ++n;

                //将 句子分词后的每个词 的词向量 相加

                result.addToSelf(vector);

            }

            if (n == 0)

            {

                Console.WriteLine("-------------------");

                return null;

            }

            //归一化

            result.normalize();

            //句子--->分词--->查询词向量--->词向量相加作为"句向量"

            Console.WriteLine(result);

            return result;

        }

        /**

        * 文档相似度计算

        * @param what

        * @param with

        * @return

        */

        public static float similarity(string what, string with)

        {

            //what 文档的 向量

            Vector A = query(what);

            if (A == null) return -1f;

            //to 文档的 向量

            Vector B = query(with);

            if (B == null) return -1f;

            //计算余弦相似度

            return A.cosineForUnitVector(B);

        }

其他均可运行,但通过java.Util对其进行获取WordVectorModel wordVectorModelss = new WordVectorModel("E:\\install\\hanlp\\data\\model\\sogouyuliaoku.txt");

指定语料库时,报错,报错信息如图

 

 

 

 

 

 Unable to cast object of type 'java.util.PropertyResourceBundle' to type 'sun.util.resources.OpenListResourceBundle'.

对其无法追究其根本,故从java中进行测试,能获取到文件,但同样会报错

报错信息如下

 

 

 

查询具体报错是,大致意思是说足彩这种从文本中获取的文字数据无法计算词向量,计算出的值为-1就报异常,但其实应该是0-9之间的区间值。

 

posted @ 2022-11-15 15:17  zwbsoft  阅读(321)  评论(0编辑  收藏  举报