最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python 全文检索引擎是什么
时间:2017-05-01 编辑:简简单单 来源:一聚教程网
python 全文检索引擎详解
最近一直在探索着如何用Python实现像百度那样的关键词检索功能。说起关键词检索,我们会不由自主地联想到正则表达式。正则表达式是所有检索的基础,python中有个re类,是专门用于正则匹配。然而,光光是正则表达式是不能很好实现检索功能的。
python有一个whoosh包,是专门用于全文搜索引擎。
whoosh在国内使用的比较少,而它的性能还没有sphinx/coreseek成熟,不过不同于前者,这是一个纯python库,对python的爱好者更为方便使用。具体的代码如下
安装
输入命令行 pip install whoosh
需要导入的包有:
代码如下 | 复制代码 |
fromwhoosh.indeximportcreate_in
fromwhoosh.fieldsimport*
fromwhoosh.analysisimportRegexAnalyzer
fromwhoosh.analysisimportTokenizer,Token 中文分词解析器 classChineseTokenizer(Tokenizer): """ 中文分词解析器 """ def__call__(self, value, positions=False, chars=False, keeporiginal=True, removestops=True, start_pos=0, start_char=0, mode='',**kwargs): assertisinstance(value, text_type),"%r is not unicode "%value t=Token(positions, chars, removestops=removestops, mode=mode,**kwargs) list_seg=jieba.cut_for_search(value) forwinlist_seg: t.original=t.text=w t.boost=0.5 ifpositions: t.pos=start_pos+value.find(w) ifchars: t.startchar=start_char+value.find(w) t.endchar=start_char+value.find(w)+len(w) yieldt
defchinese_analyzer(): returnChineseTokenizer() |
构建索引的函数
代码如下 | 复制代码 |
@staticmethod defcreate_index(document_dir): analyzer=chinese_analyzer() schema=Schema(titel=TEXT(stored=True, analyzer=analyzer), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer)) ix=create_in("./", schema) writer=ix.writer() forparents, dirnames, filenamesinos.walk(document_dir): forfilenameinfilenames: title=filename.replace(".txt", "").decode('utf8') printtitle content=open(document_dir+'/'+filename,'r').read().decode('utf-8') path=u"/b" writer.add_document(titel=title, path=path, content=content) writer.commit() |
检索函数
代码如下 | 复制代码 |
@staticmethod defsearch(search_str): title_list=[] print'here' ix=open_dir("./") searcher=ix.searcher() printsearch_str,type(search_str) results=searcher.find("content", search_str) forhitinresults: printhit['titel'] printhit.score printhit.highlights("content", top=10) title_list.append(hit['titel']) print'tt',title_list returntitle_list |
-
上一个: Python中怎么生成Epoch
-
下一个: 实例:实现自动清除日期目录shell脚本
相关文章
- java索引是什么?java怎么使用索引? 02-18
- MongoDB string字段索引方案 08-09
- 搜索引擎竞价入门教程 09-09
- coreseek sphinx 创建表和索引的语句 08-05
- 搜索引擎都喜欢什么样的外链? 06-09
- 搜索引擎之Site 和 Filetype的使用例子 03-27