栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 实现jieba分词搜索引擎

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 实现jieba分词搜索引擎

今天需要将安全相关的词汇做成一个中文搜索引擎,词已经存入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! 吃水果"
)

writer.add_document(
    title="document3",
    path="/c",
    content="买水果然后来世博园。"
)

writer.add_document(
    title="document4",
    path="/c",
    content="工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
)

writer.add_document(
    title="document4",
    path="/c",
    content="咱俩交换一下吧。"
)
'''
path = r'D:\filename.txt'
fd = open(path,encoding="utf-8")
for t in fd:
   writer.add_document(
    title= t,
    path="/c",
    content=t
) 
writer.commit()
searcher = ix.searcher()
parser = QueryParser("content", schema=ix.schema)

for keyword in ["安全等级",'信息安全']:
    print("result of ",keyword)
    q = parser.parse(keyword)
    results = searcher.search(q)
    for hit in results:
        print(hit['title'])
    print("="*10)

参考文档
信通院 软件开发包-SDK安全与合规报告-2020
信通院 软件开发包-SDK安全研究报告-2021年
GB/T 25070-2019 信息安全技术 网络安全等级保护安全设计技术要求
GB/T 28449-2018 信息安全技术 网络安全等级保护测评过程指南
GB/T 36627-2018 信息安全技术 网络安全等级保护测试评估技术指南
GB/T 36958-2018 信息安全技术 网络安全等级保护安全管理中心技术要求

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1005535.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号