A small package to fuzzy match chinese words 中文模糊匹配

fuzzychinese的Python项目详细描述


fuzzychinese

形近词中文模糊匹配

A simple tool to fuzzy match chinese words, particular useful for proper name matching and address matching.

一个可以模糊匹配形近字词的小工具。对于专有名词,地址的匹配尤其有用。

安装说明

pip install fuzzychinese

使用说明

首先使用想要匹配的字典对模型进行训练。

然后用FuzzyChineseMatch.transform(raw_words, n) 来快速查找与raw_words的词最相近的前n个词。

训练模型时有三种分析方式可以选择,笔划分析(stroke),部首分析(radical),和单字分析(char)。也可以通过调整ngram_range的值来提高模型性能。

匹配完成后返回相似度分数,匹配的相近词语及其原有索引号。

importpandasaspdfromfuzzychineseimportFuzzyChineseMatchtest_dict=pd.Series(['长白朝鲜族自治县','长阳土家族自治县','城步苗族自治县','达尔罕茂明安联合旗','汨罗市'])raw_word=pd.Series(['达茂联合旗','长阳县','汩罗市'])assert('汩罗市'!='汨罗市')# They are not the same!fcm=FuzzyChineseMatch(ngram_range=(3,3),analyzer='stroke')fcm.fit(test_dict)top2_similar=fcm.transform(raw_word,n=2)res=pd.concat([raw_word,pd.DataFrame(top2_similar,columns=['top1','top2']),pd.DataFrame(fcm.get_similarity_score(),columns=['top1_score','top2_score']),pd.DataFrame(fcm.get_index(),columns=['top1_index','top2_index'])],axis=1)
top1top2top1_scoretop2_scoretop1_indextop2_index
达茂联合旗达尔罕茂明安联合旗长白朝鲜族自治县0.8247510.28723730
长阳县长阳土家族自治县长白朝鲜族自治县0.6102850.47500010
汩罗市汨罗市长白朝鲜族自治县1.0000000.15209340

其他功能

  • 直接使用Stroke, Radical进行汉字分解。

    stroke=Stroke()radical=Radical()print("像",stroke.get_stroke("像"))print("像",radical.get_radical("像"))
    像 ㇒〡㇒㇇〡㇕一㇒㇁㇒㇒㇒㇏
    像 人象
    
  • 使用FuzzyChineseMatch.compare_two_columns(X, Y)对每一行的两个词进行比较,获得相似度分数。

  • 详情请参见说明文档.

致谢

拆字数据来自于 漢語拆字字典 by 開放詞典網

Installation

pip install fuzzychinese

Quickstart

First train a model with the target list of words you want to match to.

Then use FuzzyChineseMatch.transform(raw_words, n) to find top n most similar words in the target for your raw_words .

There are three analyzers to choose from when training a model: stroke, radical, and char. You can also change ngram_range to fine-tune the model.

After the matching, similarity score, matched words and its corresponding index are returned.

fromfuzzychineseimportFuzzyChineseMatchtest_dict=pd.Series(['长白朝鲜族自治县','长阳土家族自治县','城步苗族自治县','达尔罕茂明安联合旗','汨罗市'])raw_word=pd.Series(['达茂联合旗','长阳县','汩罗市'])assert('汩罗市'!='汨罗市')# They are not the same!fcm=FuzzyChineseMatch(ngram_range=(3,3),analyzer='stroke')fcm.fit(test_dict)top2_similar=fcm.transform(raw_word,n=2)res=pd.concat([raw_word,pd.DataFrame(top2_similar,columns=['top1','top2']),pd.DataFrame(fcm.get_similarity_score(),columns=['top1_score','top2_score']),pd.DataFrame(fcm.get_index(),columns=['top1_index','top2_index'])],axis=1)
top1top2top1_scoretop2_scoretop1_indextop2_index
达茂联合旗达尔罕茂明安联合旗长白朝鲜族自治县0.8247510.28723730
长阳县长阳土家族自治县长白朝鲜族自治县0.6102850.47500010
汩罗市汨罗市长白朝鲜族自治县1.0000000.15209340

Other use

  • Directly use Stroke, Radical to decompose Chinese character into strokes or radicals.

    stroke=Stroke()radical=Radical()print("像",stroke.get_stroke("像"))print("像",radical.get_radical("像"))
    像 ㇒〡㇒㇇〡㇕一㇒㇁㇒㇒㇒㇏
    像 人象
    
  • Use FuzzyChineseMatch.compare_two_columns(X, Y) to compare the pair of words in each row to get similarity score.

  • See documentation for details.

Credits

Data for Chinese radicals are from 漢語拆字字典 by 開放詞典網.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在验证检查中改进构建器模式?   java如何定义一个依赖项来获取快照?   java Javafx webview在运行时不显示dashplotly应用程序   java Android kotlin单元测试用例。。?   java Android:SurfaceView,为什么要使用不同的线程   Android/Java“org.apache.http.ProtocolException”,http头应答有问题   反射如何确定类成员是否是静态的以及Java中的字段   java设置EditText以接受双值的问题   类加载器如何使用类加载器将jar从我的网站加载到java应用程序的脚本中   java GWT HTMLTable colaspan功能   java无法删除/重命名文件   java Navigableset vs Navigablemap   php中java加密方法的转换   java为什么加载我的共享对象文件会在Netbeans 8.2中出现“未满足链接”错误?   java JAXRS CXF异常包装   java程序在If语句之后执行Else语句   java将对象转换为JAXBElement<Object>