Python学习打卡--day33(练习:优化搜索引擎)

优化搜索引擎

"""
搜索引擎:支持按照顺序搜索单词,返回所在文件位置
"""
from test06.test0605.search_base import *
import re


class BOWInvertedIndexEngine(SearchEngineBase):
    def __init__(self):
        super(BOWInvertedIndexEngine, self).__init__()
        self.inverted_index = {}

    # 将文件内容插入字典中 {单词:文件路径。。)
    def process_corpus(self, id, text):
        words = self.parse_text_to_words(text)  # 文件内容,集合模式
        # print('文件内容words:\n', words)
        for word in words:
            if word not in self.inverted_index:
                self.inverted_index[word] = []
            self.inverted_index[word].append(id)
        # print(self.inverted_index)   # {'four': ['./1.txt'], 'one': ['./1.txt'],..}

    def search(self, query):
        query_words = list(self.parse_text_to_words(query))  # 查询的单词换行成list
        print(query_words)  # i have a dream  --》 ['a', 'have', 'i', 'dream']
        query_words_index = list()
        for query_word in query_words:
            query_words_index.append(0)
        print(query_words_index)

        # 如果某一个查询单词的倒序索引为空,我们就立刻返回
        for query_word in query_words:
            if query_word not in self.inverted_index:
                return []

            result = []
            while True:
                # 首先,获得当前状态下所有倒序索引的index
                current_ids = []
                for idx, query_word in enumerate(query_words):
                    current_index = query_words_index[idx]
                    current_inverted_list = self.inverted_index[query_word]

                    # 已经遍历到了某一个倒序索引的末尾,结束 search
                    if current_index >= len(current_inverted_list):
                        return result

                    current_ids.append(current_inverted_list[current_index])

                    # 然后,如果current_ids的所有元素都一样,那么表明这个单词在这个元素对应的文档中
                    if all(x == current_ids[0] for x in current_ids):
                        result.append(current_ids[0])
                        query_words_index = [x + 1 for x in query_words_index]
                        continue

                    # 如果不是,我们就把最小的元素加一
                    min_val = min(current_ids)
                    min_val_pos = current_ids.index(min_val)
                    query_words_index[min_val_pos] += 1

    @staticmethod
    def parse_text_to_words(text):
        # 使用正则去除标点符号和换行符
        text = re.sub(r'[^\w]', ' ', text)
        # 转为小写
        text = text.lower()
        # 生成所有单词的列表
        word_list = text.split(' ')
        # 去除空白单词
        word_list = filter(None, word_list)
        # 返回单词的set,单词去重
        return set(word_list)


serch_engin = BOWInvertedIndexEngine()
main(serch_engin)

ps:参考书极客时间python课程编写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值