首页
统计
友链
留言
Search
1
Windows下搭建Laravel开发环境~homestead
2,887 阅读
2
php解答leetcode第7题- 整数反转
2,820 阅读
3
基于php的sphinx和coreseek全文搜索,中文分词(一)
2,683 阅读
4
Laravel 安装 jwt 及基本使用
2,518 阅读
5
php解答leetcode第26题-删除排序数组中的重复项
2,010 阅读
PHP
laravel
svn
nginx
leetcode
算法
Golang
登录
Search
标签搜索
设计模式
leetcode
laravel
sphinx
coreseek
全文搜索
中文分词
compsoer
svn
centos
jwt
homestead
算法
php
yangpanyao
累计撰写
45
篇文章
累计收到
78
条评论
首页
栏目
PHP
laravel
svn
nginx
leetcode
算法
Golang
页面
统计
友链
留言
搜索到
2
篇与
中文分词
的结果
2019-08-22
基于php的sphinx和coreseek全文搜索,中文分词(二)
一丶续前在上一片文章php之sphinx/coreseek全文搜索,分词(一)中我们介绍了使用sphinx进行全文搜索,然后我们了解到sphinx对中文分词不是太智能所以接下来我将带着大家一起来安装使用sphinx(coreseek)进行全文搜索中文分词 以下内容请在新环境进行配置测试二丶coreseek简介coreseek是一款基于sphinx开源的搜索引擎,专门为用户提供免费的中文全文检索系统,coreseek被称为带有中文分词的sphinx,与sphinx不同的是coreseek增加了一个带有中文分司的词库,目前coreseek的官网已经不能访问,且该开源项目已不再维护,但这并不妨碍我们使用coreseek进行全文搜索,中文分词三丶安装1.系统环境centos7.2 +lnmp环境2.下载coreseek并上传到服务器下载链接:https://pan.baidu.com/s/1g74zH9tCE3lQH9UyQUnyyw 密码:o9gj3.安装mmseg 分词 tar -zxvf coreseek-4.1-beta.tar.gz cd coreseek-4.1-beta/mmseg-3.2.14/ ./bootstrap echo $? ./configure --prefix=/usr/local/mmseg make make install4.安装coreseek cd ../csft-4.1/ sh buildconf.sh ##警告可以忽略 ./configure --prefix=/usr/local/coreseek --without-python --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/ --with-mysql make ##警告可忽略 make install ###可能会报的错 1. sh buildconf.sh 报错 automake: warnings are treated as errors #解决办法 将configure.ac文件的 AM_INIT_AUTOMAKE([-Wall -Werror foreign]) 修改为 AM_INIT_AUTOMAKE([-Wall foreign])subdir-objects 2.make报错1 make[2]: *** [sphinxexpr.o] Error 1 make[2]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src' make[1]: *** [all] Error 2 make[1]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src' make: *** [all-recursive] Error 1 ##解决办法 将src目录下sphinxexpr.cpp 文件的所有的 T val = ExprEval ( this-m_pArg, tMatch ); 修改为 T val = this->ExprEval ( this-m_pArg, tMatch ); 然后执行 make clean 在执行make 3.make 报错2 collect2: error: ld returned 1 exit status make[2]: *** [indexer] Error 1 make[2]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src' make[1]: *** [all] Error 2 make[1]: Leaving directory `/root/coreseek-4.1-beta/csft-4.1/src' ##解决办法 编辑configure 文件 修改 #define USE_LIBICONV 1 的 1 修改为0四丶配置coreseek 以及demo测试1. coreseek配置 cd /usr/local/coreseek/etc vim sphinx.conf.dist ##配置基本与sphinx相同 不同的是 coreseek需要在索引attr_node 里添加 charset_type = zh_cn.utf-8 charset_dictpath = /usr/local/mmseg/etc/ 添加这两行,意思是把中文分词加入到配置文件中 修改保存完执行 egrep -v '#|^$' sphinx.conf.dist >sphinx.conf ## sphinx.conf 的内容如下: source node { type = mysql sql_host = localhost sql_user = root sql_pass = 123456 sql_db = test sql_sock = /tmp/mysql.sock sql_query_pre = SET NAMES utf8 sql_query_pre = SET SESSION query_cache_type=OFF sql_query = SELECT id,title,content,created FROM node sql_field_string = title sql_field_string = content sql_field_string = created sql_ranged_throttle = 0 } index attr_node { source = node path = /usr/local/coreseek/var/data/attr_node docinfo = extern mlock = 0 morphology = none min_word_len = 1 charset_type = zh_cn.utf-8 charset_dictpath = /usr/local/mmseg/etc/ html_strip = 0 } indexer { mem_limit = 128M } searchd { listen = 9312 listen = 9306:mysql41 log = /usr/local/coreseek/var/log/searchd.log query_log = /usr/local/coreseek/var/log/query.log read_timeout = 5 client_timeout = 300 max_children = 30 pid_file = /usr/local/coreseek/var/log/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 mva_updates_pool = 1M max_packet_size = 8M max_filters = 256 max_filter_values = 4096 max_batch_queries = 32 }2.创建启动索引 cd ../bin ./indexer --config /usr/local/coreseek/etc/sphinx.conf -all ./searchd --config /usr/local/coreseek/etc/sphinx.conf3.测试demo编写cd /home/wwwroot/default/ mkdir coreseek cd cd coreseek-4.1-bata/csft-4.1/api/ cp sphinxapi.php /home/wwwroot/default/coreseek/ cd /home/wwwroot/default/coreseek/ ##创建并编辑 index.html vim index.html ##写入以下代码 <!DOCTYPE html> <html> <head> <title>欢迎使用sphinx!</title> <meta charset="utf-8"> </head> <body> <h1>欢迎使用sphinx搜索</h1> <form method="post" action="sphinxtest.php" > <input type="text" name="centent"/> <input type="submit" vlaue="搜索"> </form> </body> </html> ##然后保存退出 然后我们 新建并编辑一个 sphinxtest.php vim sphinxtest.php ##写入以下代码 <?php require('sphinxapi.php'); $cl = new SphinxClient (); $q = $_POST['centent']; $host = "localhost"; $port = 9312; $index = "attr_node"; $cl->SetServer ( $host, $port ); $cl->SetConnectTimeout ( 1 ); $cl->SetArrayResult ( true ); //匹配查询词中的任意一个 $cl->SetMatchMode(SPH_MATCH_ANY); $res = $cl->Query ( $q, $index ); echo '<pre>'; var_dump($res); echo '</pre>'; ?> ##保存并退出 4.测试我们在搜索框输入 “阳光明媚”显示如下:array(10) { ["error"]=> string(0) "" ["warning"]=> string(0) "" ["status"]=> int(0) ["fields"]=> array(3) { [0]=> string(5) "title" [1]=> string(7) "content" [2]=> string(7) "created" } ["attrs"]=> array(3) { ["title"]=> int(7) ["content"]=> int(7) ["created"]=> int(7) } ["matches"]=> array(3) { [0]=> array(3) { ["id"]=> int(6) ["weight"]=> string(1) "8" ["attrs"]=> array(3) { ["title"]=> string(33) "笔墨流芬芳,醉爱文字情" ["content"]=> string(366) "文字,是一米阳光,温暖着我的心。阳光明媚,我在阳光里享受着它的融融暖意。文字,发着光,发着热,让我的灵魂不再四处漂游。文字,好暖,让我结冰的心湖慢慢融化。阳光,让早已心如死灰的心感受到从未有过的温暖。只要抓住那一米阳光,就等于抓住了一颗救命稻草" ["created"]=> string(19) "2018-07-17 11:12:00" } } [1]=> array(3) { ["id"]=> int(10) ["weight"]=> string(1) "8" ["attrs"]=> array(3) { ["title"]=> string(24) "走在迷途的拾荒人" ["content"]=> string(423) "时光就像一个美少女,在低眉浅笑中,就将有些人一些事隔到了光阴的对面。其实光阴从不曾厚过谁也不曾薄过谁,生活就是一种积累,你若储存的温暖多,你的生活就会阳光明媚,你若储存太多寒凉,你的生活就会阴云密布。放下烦恼与忧愁,带着最美的微笑出发,脚下路在,前方希望在,回眸处爱与温暖一直都在。" ["created"]=> string(19) "2018-07-17 11:12:00" } } [2]=> array(3) { ["id"]=> int(9) ["weight"]=> string(1) "1" ["attrs"]=> array(3) { ["title"]=> string(15) "阳光的滋味" ["content"]=> string(336) "明眸善睐,云袖轻舒,花影婆娑,夜鸟伏声。望月,终团圆。融化一季相思苦楚,泪凝妙目。开襟解怀,邀月畅饮。一季愁情尽遣。蘸墨狂书,满腹恩爱无度。羞愧了月里仙子,忧郁了河汉星辰。明月缱惓,夜色飘香。尝遍了相思苦涩,领略这相聚欢欣。" ["created"]=> string(19) "2018-07-17 11:12:00" } } } ["total"]=> string(1) "3" ["total_found"]=> string(1) "3" ["time"]=> string(5) "0.003" ["words"]=> array(2) { ["阳光"]=> array(2) { ["docs"]=> string(1) "3" ["hits"]=> string(1) "7" } ["明媚"]=> array(2) { ["docs"]=> string(1) "2" ["hits"]=> string(1) "2" } }在words里我们可以发现 阳光明媚被拆分成了两个词组 而不是像sphinx 切割成一个一个的字符 相比sphinx 又智能了许多!到此sphinx以及coreseek的安装 配置以及基本使用就介绍到这里了 sphinx和coreseek 还有更多其他功能等例如分页 排序 指定查询等 功能十分强大,感兴趣的小伙伴赶紧试试吧。
2019年08月22日
1,509 阅读
1 评论
0 点赞
2019-08-22
基于php的sphinx和coreseek全文搜索,中文分词(一)
一.sphinx简介1.什么是sphinxSphinx是由俄罗斯人Andrew Aksyonoff开发的一个全文检索引擎。意图为其他应用提供高速、低空间占用、高结果 相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。当前系统内置MySQL和PostgreSQL 数据库数据源的支持,也支持从标准输入读取特定格式 的XML数据。通过修改源代码,用户可以自行增加新的数据源(例如:其他类型的DBMS 的原生支持)2.sphinx的特性高速的建立索引(在当代CPU上,峰值性能可达到10 MB/秒);高性能的搜索(在2 – 4GB 的文本数据上,平均每次检索响应时间小于0.1秒);可处理海量数据(目前已知可以处理超过100 GB的文本数据, 在单一CPU的系统上可 处理100 M 文档);提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;支持分布式搜索;支持短语搜索提供文档摘要生成可作为MySQL的存储引擎提供搜索服务;支持布尔、短语、词语相似度等多种检索模式;文档支持多个全文检索字段(最大不超过32个);文档支持多个额外的属性信息(例如:分组信息,时间戳等);支持断词;二.安装与配置1.系统环境centos7.2 +lnmp环境2.安装sphinxwget http://sphinxsearch.com/files/sphinx-2.2.11-release.tar.gz ##下载sphinx tar -zxvf sphinx-2.2.11-release.tar.gz ##解压 cd sphinx-2.2.11-release ./configure --prefix=/usr/local/sphinx/ echo $? make echo $? make install (这里为了方便查看每一步是否执行成功以及查看错误我把散步拆开了 (echo $? 返回数字0 执行成功 返回其他失败 )3.安装 sphinx 可能遇见的错误##make错误 collect2: error: ld returned 1 exit status make[2]: *** [indexer] Error 1 make[2]: Leaving directory `/root/sphinx-2.2.11-release/src' make[1]: *** [all] Error 2 make[1]: Leaving directory `/root/sphinx-2.2.11-release/src' make: *** [all-recursive] Error 1 ##解决办法 先执行 make clean 然后修改 configure 文件 把 #define USE_LIBICONV 0 最后的数值由1改为0三.sphinx的配置与以及测试小demosphinx安装成功后我们进入sphinx目录,会发现有这四个目录其中 bin 存放索引启动文件 etc 存放sphinx配置文件 var存放生成的索引文件然后我们不着急进行sphinx 我们先准备一份数据表CREATE TABLE IF NOT EXISTS `node` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `content` text NOT NULL, `created` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试文章表' AUTO_INCREMENT=11 ; INSERT INTO `node` (`id`, `title`, `content`, `created`) VALUES (1, '三月醉一场青春的流年', '三月,醉一场青春的流年。慢步在三月的春光里,走走停停,看花开嫣然,看春雨绵绵,感受春风拂面,春天,就是青春的流年。青春,是人生中最美的风景。青春,是一场花开的遇见', '2018-07-17 11:12:00'), (2, '在爱的岁月山河里思念', '夜色如梦,没有人陪,坐在沙发里,一杯红酒配电影,似乎看透红尘,实际上钻心的孤独。远在他方的你,是否也一样在深夜里买醉?谁在爱的国度里,想念着谁?谁在梦里,与谁不醉不休?谁在寂寞的夜晚,想象着与谁缠绵悱恻?在爱的幸福国度,你就是我的唯一。', '2018-07-17 11:12:00'), (3, 'php是世界上最好的语言', '女神:你能让这个知乎程序员板块的人都吵起来,我今晚就跟你走,但是提问不能问xx是不是最好的语言。程序员提问:“PHP是最好的语言”这个梗是怎么来的?女神:这能吵起来?程序员:走着瞧半小时后,知乎炸开了锅,数百回答讨论PHP的是是非非。女神:我了个去,这特么都行。服了,跟你回去就是了。程序员:不行,我一定要说服他们,PHP是最好的语言', '2018-07-17 11:12:00'), (4, 'mysql', 'mysql is the best database?', '2018-07-17 11:12:00'), (5, '夏色斑斓,夏意阑珊', '岁月,在风声里歌唱,一如往昔,撩起时光的衣裙。慢步在光阴的旷野,走走停停,看人来人往,看花开花谢,看日出日落。风景如画,往事如诗。那段凯歌,奏响风中的依恋,为你,为我,祭奠旧时的风景。相思豆,将你我的思念串成一个圆圈,你在地球的那头,我在地球的这头。', '2018-07-17 11:12:00'), (6, '笔墨流芬芳,醉爱文字情', '文字,是一米阳光,温暖着我的心。阳光明媚,我在阳光里享受着它的融融暖意。文字,发着光,发着热,让我的灵魂不再四处漂游。文字,好暖,让我结冰的心湖慢慢融化。阳光,让早已心如死灰的心感受到从未有过的温暖。只要抓住那一米阳光,就等于抓住了一颗救命稻草', '2018-07-17 11:12:00'), (7, '冬风中一朵紫罗兰', '当时我在窗前。窗里光线幽暗,冷冷清清,窗玻璃紧紧地闭着,木头的窗棂子似乎不堪冷风的肆虐有些瑟瑟发抖,这一切让我原本晦暗的心情更加晦暗,失落的灵魂更加无着无落了。', '2018-07-17 11:12:00'), (8, '我在红尘中等你', '在这红尘里,我渴望陪你变老,不求一路轰轰烈烈,只愿一世倾心,像山野花儿把最后的生命情葬在秋天,因为你为梦想去漂泊,去了遥远的他乡,我后悔没有与你同行,致使离我而去,你知道我喜欢家乡的宁静,喜欢云的淡然恬静,喜欢山色的空蒙幽深,我喜欢握那一只短笛,诉说心中忧郁,想想走过的人生路,曾经说过相伴一生的你,如今去了南方,那些曾经许下的诺言,早已风吹云散。曾经的温暖,美好的记忆,也许只是一个转身的距离,让我在红尘中等你,今生是如何的结局。', '2018-07-17 11:12:00'), (9, '阳光的滋味', '明眸善睐,云袖轻舒,花影婆娑,夜鸟伏声。望月,终团圆。融化一季相思苦楚,泪凝妙目。开襟解怀,邀月畅饮。一季愁情尽遣。蘸墨狂书,满腹恩爱无度。羞愧了月里仙子,忧郁了河汉星辰。明月缱惓,夜色飘香。尝遍了相思苦涩,领略这相聚欢欣。', '2018-07-17 11:12:00'), (10, '走在迷途的拾荒人', '时光就像一个美少女,在低眉浅笑中,就将有些人一些事隔到了光阴的对面。其实光阴从不曾厚过谁也不曾薄过谁,生活就是一种积累,你若储存的温暖多,你的生活就会阳光明媚,你若储存太多寒凉,你的生活就会阴云密布。放下烦恼与忧愁,带着最美的微笑出发,脚下路在,前方希望在,回眸处爱与温暖一直都在。', '2018-07-17 11:12:00');接下来开始我们对sphinxd的配置cd etc vim sphinx.conf.dist然后我们对配置文件进行修改配置完后 wq 保存退出运行:egrep -v '#|^$' sphinx.conf.dist >sphinx.conf将注释代码去除并生成新文件 sphinx.conf 我们打开sphinx.conf 如下所示:source node { type = mysql sql_host = localhost sql_user = root sql_pass = 123456 sql_db = test sql_sock = /tmp/mysql.sock sql_query_pre = SET NAMES utf8 sql_query_pre = SET SESSION query_cache_type=OFF sql_query = SELECT ,id, title, content ,created FROM node sql_field_string = title sql_field_string = content sql_field_string = created sql_ranged_throttle = 0 } index attr_node { source = node path = /usr/local/sphinx/var/data/attr_node docinfo = extern dict = keywords mlock = 0 morphology = none min_word_len = 1 ngram_len = 1 ngram_chars = U+3000..U+2FA1F html_strip = 0 } indexer { mem_limit = 128M } searchd { listen = 9312 listen = 9306:mysql41 log = /usr/local/sphinx/var/log/searchd.log query_log = /usr/local/sphinx/var/log/query.log read_timeout = 5 client_timeout = 300 max_children = 30 persistent_connections_limit = 30 pid_file = /usr/local/sphinx/var/log/searchd.pid seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 mva_updates_pool = 1M max_packet_size = 8M max_filters = 256 max_filter_values = 4096 max_batch_queries = 32 }接下来我们创建并启动索引 cd ../bin #创建索引 ./indexer --config /usr/local/sphinx/etc/sphinx.conf --all ## --all创建全部索引 -- + 索引名 例如:--attr_node 创建attr_node索引 #启动索引 ./searchd --config /usr/local/sphinx/etc/sphinx.conf #####其他命令###### ./indexer --config /usr/local/sphinx/etc/sphinx.conf --rotate --all ##重建索引接下来我们开始demo的编写:cd cd /home/wwwroot/default mkdir test //创建一个文件夹 cd cd sphinx-2.2.11-release/api cp sphinxapi.php /home/wwwroot/default/test/ cd cd /home/wwwroot/default/test/ ##创建并编辑 index.html vim index.html ##写入以下代码 <!DOCTYPE html> <html> <head> <title>欢迎使用sphinx!</title> <meta charset="utf-8"> </head> <body> <h1>欢迎使用sphinx搜索</h1> <form method="post" action="sphinxtest.php" > <input type="text" name="centent"/> <input type="submit" vlaue="搜索"> </form> </body> </html> ##然后保存退出 然后我们 新建并编辑一个 sphinxtest.php vim sphinxtest.php ##写入以下代码 <?php require('sphinxapi.php'); $cl = new SphinxClient (); $q = $_POST['centent']; $host = "localhost"; $port = 9312; $index = "attr_node"; $cl->SetServer ( $host, $port ); $cl->SetConnectTimeout ( 1 ); $cl->SetArrayResult ( true ); //匹配查询词中的任意一个 $cl->SetMatchMode(SPH_MATCH_ANY); $res = $cl->Query ( $q, $index ); echo '<pre>'; var_dump($res); echo '</pre>'; ?> ##保存并退出 接下来我们通过网页访问index.html输入”岁月”二字就会如下所示:array(10) { ["error"]=> string(0) "" ["warning"]=> string(0) "" ["status"]=> int(0) ["fields"]=> array(3) { [0]=> string(5) "title" [1]=> string(7) "content" [2]=> string(7) "created" } ["attrs"]=> array(3) { ["title"]=> int(7) ["content"]=> int(7) ["created"]=> int(7) } ["matches"]=> array(4) { [0]=> array(3) { ["id"]=> int(2) ["weight"]=> string(1) "8" ["attrs"]=> array(3) { ["title"]=> string(30) "在爱的岁月山河里思念" ["content"]=> string(354) "夜色如梦,没有人陪,坐在沙发里,一杯红酒配电影,似乎看透红尘,实际上钻心的孤独。远在他方的你,是否也一样在深夜里买醉?谁在爱的国度里,想念着谁?谁在梦里,与谁不醉不休?谁在寂寞的夜晚,想象着与谁缠绵悱恻?在爱的幸福国度,你就是我的唯一。" ["created"]=> string(19) "2018-07-17 11:12:00" } } [1]=> array(3) { ["id"]=> int(5) ["weight"]=> string(1) "8" ["attrs"]=> array(3) { ["title"]=> string(27) "夏色斑斓,夏意阑珊" ["content"]=> string(375) "岁月,在风声里歌唱,一如往昔,撩起时光的衣裙。慢步在光阴的旷野,走走停停,看人来人往,看花开花谢,看日出日落。风景如画,往事如诗。那段凯歌,奏响风中的依恋,为你,为我,祭奠旧时的风景。相思豆,将你我的思念串成一个圆圈,你在地球的那头,我在地球的这头。" ["created"]=> string(19) "2018-07-17 11:12:00" } } [2]=> array(3) { ["id"]=> int(1) ["weight"]=> string(1) "2" ["attrs"]=> array(3) { ["title"]=> string(30) "三月醉一场青春的流年" ["content"]=> string(243) "三月,醉一场青春的流年。慢步在三月的春光里,走走停停,看花开嫣然,看春雨绵绵,感受春风拂面,春天,就是青春的流年。青春,是人生中最美的风景。青春,是一场花开的遇见" ["created"]=> string(19) "2018-07-17 11:12:00" } } [3]=> array(3) { ["id"]=> int(9) ["weight"]=> string(1) "1" ["attrs"]=> array(3) { ["title"]=> string(15) "阳光的滋味" ["content"]=> string(336) "明眸善睐,云袖轻舒,花影婆娑,夜鸟伏声。望月,终团圆。融化一季相思苦楚,泪凝妙目。开襟解怀,邀月畅饮。一季愁情尽遣。蘸墨狂书,满腹恩爱无度。羞愧了月里仙子,忧郁了河汉星辰。明月缱惓,夜色飘香。尝遍了相思苦涩,领略这相聚欢欣。" ["created"]=> string(19) "2018-07-17 11:12:00" } } } ["total"]=> string(1) "4" ["total_found"]=> string(1) "4" ["time"]=> string(5) "0.000" ["words"]=> array(2) { ["岁"]=> array(2) { ["docs"]=> string(1) "2" ["hits"]=> string(1) "2" } ["月"]=> array(2) { ["docs"]=> string(1) "4" ["hits"]=> string(1) "9" } } }matches 里边是我们想要的数据 total 是数据总数 因为我们设置的中文切割为 长度1 ,所以 他也搜索了包含”岁”字,的和包含”月”字的数据 words为切割的词,岁月本身也是一个词组 在words并没有显示而是拆分为了岁和月 中文分词并不是特别的智能 本片文章就到此结束,由于本文测试的数据量有限也无法让大家感受到sphinx查询的速度之快,感兴趣的小伙伴,有条件的话可以自己分别使用sql语句 like 和 sphinx 来做下测试, sphinx更多的对英文进行搜索,对中文的支持并不是特别的智能,所以在下篇文章我将教大家使用sphinx(coreseek)全文搜索中文分词
2019年08月22日
2,683 阅读
3 评论
0 点赞