今天需要将安全相关的词汇做成一个中文搜索引擎,词已经存入filename.txt中,使用python版jieba分词 进行实现
使用jieba分词实现的中文搜索引擎
- 先读取filename.txt中词列表 每行一个
- 使用jieba分词
- 检索测试
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
import sys,os
sys.path.append("../")
from whoosh.index import create_in,open_dir
from whoosh.fields import *
from whoosh.qparser import QueryParser
from jieba.analyse.analyzer import ChineseAnalyzer
analyzer = ChineseAnalyzer()
schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))
if not os.path.exists("tmp"):
os.mkdir("tmp")
ix = create_in("tmp", schema) # for create new index
#ix = open_dir("tmp") # for read only
writer = ix.writer()
'''
writer.add_document(
title="document1",
path="/a",
content="This is the first document we’ve added!"
)
writer.add_document(
title="document2",
path="/b",
content="The second one 你 中文测试中文 is even more interesting!

本文介绍了一个使用jieba分词实现的中文搜索引擎案例。该搜索引擎通过读取文件中的词汇,并利用Whoosh建立索引,实现了对中文内容的有效检索。文章演示了如何添加文档到索引以及如何进行关键词搜索。
最低0.47元/天 解锁文章
680





