[TextMatch框架] 简介
TextMatch
TextMatch is a semantic matching model library for QA & text search … It’s easy to train models and to export representation vectors.
[TextMatch框架] : 文本匹配/文本分类/文本embedding/文本聚类/文本检索(bow/ifidf/ngramtf-df/bert/albert/bm25/…/nn/gbdt/xgb/kmeans/dscan/faiss/….):https://github.com/MachineLP/TextMatch
run examples
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
export PYTHONPATH=${PYTHONPATH}:../TextMatch
python examples/text_search.py
examples/text_search.py
import sys
from textmatch.models.text_embedding.model_factory_sklearn import ModelFactoryif __name__ == '__main__':# docdoc_dict = {"0":"我去玉龙雪山并且喜欢玉龙雪山玉龙雪山", "1":"我在玉龙雪山并且喜欢玉龙雪山", "2":"我在九寨沟", "3":"你好"} # queryquery = "我在九寨沟,很喜欢"# 模型工厂,选择需要的模型加到列表中: 'bow', 'tfidf', 'ngram_tfidf', 'bert', 'albert', 'w2v'mf = ModelFactory( match_models=['bow', 'tfidf', 'ngram_tfidf'] )# 模型处理初始化mf.init(words_dict=doc_dict, update=True)# query 与 doc的相似度search_res = mf.predict(query)print ('search_res>>>>>', search_res) # search_res>>>>> {'bow': [('0', 0.2773500981126146), ('1', 0.5303300858899106), ('2', 0.8660254037844388), ('3', 0.0)], 'tfidf': [('0', 0.2201159065358879), ('1', 0.46476266418455736), ('2', 0.8749225357988296), ('3', 0.0)], 'ngram_tfidf': [('0', 0.035719486884261346), ('1', 0.09654705406841395), ('2', 0.9561288696241232), ('3', 0.0)]}# query的embeddingquery_emb = mf.predict_emb(query)print ('query_emb>>>>>', query_emb) '''pre_emb>>>>> {'bow': array([1., 0., 0., 1., 1., 0., 1., 0.]), 'tfidf': array([0.61422608, 0. , 0. , 0.4842629 , 0.4842629 ,0. , 0.39205255, 0. ]), 'ngram_tfidf': array([0. , 0. , 0.37156534, 0.37156534, 0. ,0. , 0. , 0.29294639, 0. , 0.37156534,0.37156534, 0. , 0. , 0.37156534, 0. ,0. , 0. , 0. , 0. , 0. ,0. , 0. , 0.29294639, 0.37156534, 0. ,0. , 0. , 0. , 0. , 0. ,0. , 0. , 0. , 0. ])}'''
run examples
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
export PYTHONPATH=${PYTHONPATH}:../TextMatch
python tests/tools_test/faiss_test.py
tests/tools_test/faiss_test.py
import sys
import json
import time
import faiss
import numpy as np
from faiss import normalize_L2
from textmatch.config.constant import Constant as const
from textmatch.core.text_embedding import TextEmbedding
from textmatch.tools.decomposition.pca import PCADecomposition
from textmatch.tools.faiss.faiss import FaissSearchtest_dict = {"id0": "其实事物发展有自己的潮流和规律","id1": "当你身处潮流之中的时候,要紧紧抓住潮流的机会","id2": "想办法脱颖而出,即使没有成功,也会更加洞悉时代的脉搏","id3": "收获珍贵的知识和经验。而如果潮流已经退去","id4": "这个时候再去往这个方向上努力,只会收获迷茫与压抑","id5": "对时代、对自己都没有什么帮助","id6": "但是时代的浪潮犹如海滩上的浪花,总是一浪接着一浪,只要你站在海边,身处这个行业之中,下一个浪潮很快又会到来。你需要敏感而又深刻地去观察,略去那些浮躁的泡沫,抓住真正潮流的机会,奋力一搏,不管成败,都不会遗憾。","id7": "其实事物发展有自己的潮流和规律","id8": "当你身处潮流之中的时候,要紧紧抓住潮流的机会" }if __name__ == '__main__':# ['bow', 'tfidf', 'ngram_tfidf', 'bert']# ['bow', 'tfidf', 'ngram_tfidf', 'bert', 'w2v']# text_embedding = TextEmbedding( match_models=['bow', 'tfidf', 'ngram_tfidf', 'w2v'], words_dict=test_dict )text_embedding = TextEmbedding( match_models=['bow', 'tfidf', 'ngram_tfidf', 'w2v'], words_dict=None, update=False ) feature_list = []for sentence in test_dict.values():pre = text_embedding.predict(sentence)feature = np.concatenate([pre[model] for model in ['bow', 'tfidf', 'ngram_tfidf', 'w2v']], axis=0)feature_list.append(feature)pca = PCADecomposition(n_components=8)data = np.array( feature_list )pca.fit( data )res = pca.transform( data )print('res>>', res)pre = text_embedding.predict("潮流和规律")feature = np.concatenate([pre[model] for model in ['bow', 'tfidf', 'ngram_tfidf', 'w2v']], axis=0)test = pca.transform( [feature] )faiss_search = FaissSearch( res, sport_mode=False )faiss_res = faiss_search.predict( test )print( "faiss_res:", faiss_res )'''faiss kmeans result times 8.0108642578125e-05faiss_res: [{0: 0.7833399, 7: 0.7833399, 3: 0.63782495}]'''faiss_search = FaissSearch( res, sport_mode=True )faiss_res = faiss_search.predict( test )print( "faiss_res:", faiss_res )'''faiss kmeans result times 3.266334533691406e-05faiss_res: [{0: 0.7833399, 7: 0.7833399, 3: 0.63782495}]'''
run examples:基于召回和排序的文本搜索
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
export PYTHONPATH=${PYTHONPATH}:../TextMatch
python tests/core_test/text_search_test.py
tests/core_test/text_search_test.py
import sys
from textmatch.core.text_match import TextMatch
from textmatch.core.qa_match import QMatch, AMatch, SemanticMatch
from textmatch.models.text_search.model_factory_search import ModelFactorySearchdef text_match_recall(testword, doc_dict):# QMatchq_match = QMatch( q_dict=doc_dict, match_models=['bow', 'tfidf', 'ngram_tfidf', 'albert']) q_match_pre = q_match.predict(testword, match_strategy='score', vote_threshold=0.5, key_weight = {'bow': 1, 'tfidf': 1, 'ngram_tfidf': 1, 'albert': 1})# print ('q_match_pre>>>>>', q_match_pre )return q_match_pre def text_match_sort(testword, candidate_doc_dict):text_match = TextMatch( q_dict=candidate_doc_dict, match_models=['bm25', 'edit_sim', 'jaccard_sim'] )text_match_res = text_match.predict( query, match_strategy='score', vote_threshold=-100.0, key_weight = {'bm25': 0, 'edit_sim': 1, 'jaccard_sim': 1} )return text_match_res if __name__ == '__main__':doc_dict = {"0":"我去玉龙雪山并且喜欢玉龙雪山玉龙雪山", "1":"我在玉龙雪山并且喜欢玉龙雪山", "2":"我在九寨沟", "3":"我在九寨沟,很喜欢", "4":"很喜欢"} query = "我在九寨沟,很喜欢"# 直接搜索mf = ModelFactorySearch( match_models=['bm25', 'edit_sim', 'jaccard_sim'] )mf.init(words_dict=doc_dict)pre = mf.predict(query)print ('pre>>>>>', pre) # 先召回match_pre = text_match_recall( query, doc_dict )print( '召回的结果:', match_pre )candidate_doc_dict = dict( zip( match_pre.keys(), [doc_dict[key] for key in match_pre.keys()] ) )print ("candidate_doc_dict:", candidate_doc_dict)# 再排序# ['bm25', 'edit_sim', 'jaccard_sim']text_match_res = text_match_sort( query, candidate_doc_dict )print ('排序的score>>>>>', text_match_res) '''# 排序mf = ModelFactorySearch( match_models=['bm25', 'edit_sim', 'jaccard_sim'] )mf.init(words_dict=candidate_doc_dict) pre = mf.predict(query)print ('排序的结果>>>>>', pre) '''
run tests/core_test (qa/文本embedding)
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
pip install -r requirements.txt
export PYTHONPATH=${PYTHONPATH}:../TextMatch
python tests/core_test/qa_match_test.py
python tests/core_test/text_embedding_test.py
run tests/models_test (模型测试)
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
pip install -r requirements.txt
export PYTHONPATH=${PYTHONPATH}:../TextMatch
python tests/models_test/bm25_test.py (bm25)
python tests/models_test/edit_sim_test.py (编辑距离)
python tests/models_test/jaccard_sim_test.py (jaccard)
python tests/models_test/bow_sklearn_test.py (bow)
python tests/models_test/tf_idf_sklearn_test.py (tf_idf)
python tests/models_test/ngram_tf_idf_sklearn_test.py (ngram_tf_idf)
python tests/models_test/w2v_test.py (w2v)
python tests/models_test/albert_test.py (bert)
run tests/ml_test (机器学习模型测试)
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
pip install -r requirements.txt
export PYTHONPATH=${PYTHONPATH}:../TextMatch
python tests/ml_test/lr_test.py (lr)
python tests/ml_test/gbdt_test.py (gbdt)
python tests/ml_test/gbdt_lr_test.py (gbdt_lr)
python tests/ml_test/lgb_test.py (lgb)
python tests/ml_test/xgb_test.py (xgb)
run tests/tools_test (聚类/降维工具测试)
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
pip install -r requirements.txt
export PYTHONPATH=${PYTHONPATH}:../TextMatch
python tests/tools_test/kmeans_test.py (kmeans)
python tests/tools_test/dbscan_test.py (dbscan)
python tests/tools_test/pca_test.py (pca)
python tests/tools_test/faiss_test.py (faiss)
run tests/tools_test (词云)
git clone https://github.com/MachineLP/TextMatch
cd TextMatch
pip install -r requirements.txt
cd tests/tools_test
python generate_word_cloud.py
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y1jZFjkw-1595157268738)(./docs/pics/word_cloud.png)]
————————————————
[TextMatch框架] Run examples
[TextMatch框架] models
[TextMatch框架] train model
[TextMatch框架] tests
[TextMatch框架] server
[TextMatch框架] 简介相关推荐
- 【Spring】框架简介
[Spring]框架简介 Spring是什么 Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IOC(Inverse Of Control:反转控制)和AOP(Asp ...
- 《HiWind企业快速开发框架实战》(0)目录及框架简介
<HiWind企业快速开发框架实战>(0)目录及框架简介 本系列主要介绍一款企业管理系统快速开发框架,该框架旨在快速完成企业管理系统,并实现易维护可移植的目标. 使用逐个系统模块进行编码的 ...
- WPF Interaction框架简介(一)——Behavior
原文:WPF Interaction框架简介(一)--Behavior 在WPF 4.0中,引入了一个比较实用的库--Interactions,这个库主要是通过附加属性来对UI控件注入一些新的功能,除 ...
- go语言学习(二)——Gin 框架简介
GoWeb开发有很多框架,如Beego.Gin.Revel.Echo.IRis等,学习框架可以快速做开发,对比常见goweb框架,通过其github的活跃度,维护的team,生产环境中的使用率以及师兄 ...
- Java开源——常见J2EE框架简介
Java开源--常见J2EE框架简介 Spring Framework Spring是一个解决了许多在J2EE开发中常见的问题的强大框架. Spring提供了管理业务对象的一致方法并且鼓励了注入对接口 ...
- 【EventBus】EventBus 事件总线框架简介 ( EventBus 使用流程 )
文章目录 一.EventBus 事件总线框架简介 二.EventBus 使用流程 一.EventBus 事件总线框架简介 Android 中的事件传递机制 : 使用 Intent 在组件间传递信息 ; ...
- 【Android 热修复】热修复原理 ( 热修复框架简介 | 将 Java 字节码文件打包到 Dex 文件 )
文章目录 一. 热修复框架简介 1.类替换 2.so 替换 3.资源替换 4.全平台支持 5.生效时间 6.性能损耗 7.总结 二. 将 Java 字节码文件打包到 Dex 文件 一. 热修复框架简介 ...
- PHP微信开发框架LaneWeChat框架简介
框架名称:LaneWeChat 微信PHP开发框架 框架版本:1.4 框架简介:这是一个为快速开发微信应用而生的PHP框架.将微信的开发者功能根据文档进行了封装.为了快速开发的目的,开发者完全不需要要 ...
- Spring框架简介
Spring框架简介 Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本. ...
最新文章
- 复现经典:《统计学习方法》第 6 章 逻辑斯谛回归
- Getter DI是个好主意吗?
- ArcGIS API for Silverlight 调用GP服务准备---GP模型建立、发布、测试
- java并发-多线程方面的思考
- (22)FPGA面试技能提升篇(MicroBlaze、PowerPC)
- JSON Perl
- AngularJs学习笔记0——前言
- maven依赖avro_在MapReduce中使用Avro
- 随手记_论文读写策略
- php和java环境整合
- 手把手搭建K3cloud插件开发环境
- 微信小程序直播是怎么做的
- 1 分析计算机上网时断时续的原因,网络丢包现象分析与解决方案
- 尺寸公差分析尺寸链计算软件:新能源电池行业—模组散热系统之弹簧长度计算
- 创业前期怎么做个简单易行的计划?
- 最全动态规划题型详解
- Python自动登陆淘宝并爬取商品数据
- 什么是大数据?大数据的5V特点是什么?
- Zookeeper同步机制!!!
- 银行表内表外业务图解