实例 搜索引擎

  一个搜索引擎由搜索器、索引器、检索器和用户接口四个部分组成

  搜索器就是爬虫(scrawler),爬出的内容送给索引器生成索引(Index)存储在内部数据库。用户通过用户接口发出询问(query),询问解析后送达检索器,检索器高效检索后,将结果返回给用户。

  以下5个文件为爬取的搜索样本。

# # 1.txt
# I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character. I have a dream today. # # 2.txt
# I have a dream that one day down in Alabama, with its vicious racists, . . . one day right there in Alabama little black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. I have a dream today. # # 3.txt
# I have a dream that one day every valley shall be exalted, every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see it together. # # 4.txt
# This is our hope. . . With this faith we will be able to hew out of the mountain of despair a stone of hope. With this faith we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. With this faith we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom together, knowing that we will be free one day. . . . # # 5.txt
# And when this happens, and when we allow freedom ring, when we let it ring from every village and every hamlet, from every state and every city, we will be able to speed up that day when all of God's children, black men and white men, Jews and Gentiles, Protestants and Catholics, will be able to join hands and sing in the words of the old Negro spiritual: "Free at last! Free at last! Thank God Almighty, we are free at last!"

简单搜索引擎

  SearchEngineBase 基类,corpus(语料)
class SearchEngineBase(object):
def __init__(self):
pass
#将文件作为id,与内容一起送到process_corpus
def add_corpus(self, file_path):
with open(file_path, 'r') as fin:
text = fin.read()
self.process_corpus(file_path, text)
#索引器 将文件路径作为id,将处理的内容作为索引存储
def process_corpus(self, id, text):
raise Exception('process_corpus not implemented.')
#检索器
def search(self, query):
raise Exception('search not implemented.') #多态
def main(search_engine):
for file_path in ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt']:
search_engine.add_corpus(file_path) while True:
query = input()
results = search_engine.search(query)
print('found {} result(s):'.format(len(results)))
for result in results:
print(result) class SimpleEngine(SearchEngineBase):
def __init__(self):
super(SimpleEngine, self).__init__()
self.__id_to_texts = dict() def process_corpus(self, id, text):
self.__id_to_texts[id] = text def search(self, query):
results = []
for id, text in self.__id_to_texts.items():
if query in text:
results.append(id)
return results search_engine = SimpleEngine()
main(search_engine) ########## 输出 ##########
# simple
# found 0 result(s):
# whe
# found 2 result(s):
# 1.txt
# 5.txt
  缺点:每次索引与检索都需要占用大量空间,还有查询只能是一个词或连续的几个词,对分散的在不同位置的多个词无能为力

词袋模型 (bag of words model)

  运用词袋模型 (bag of words model),NLP领域最简单的模型之一。
  process_corpus函数中调用parse_text_to_words把文档中的各个单词装进set集合中。
  search函数中把包含查询关键字也打碎成set,与索引的文档核对,将匹配的id加入结果集。
 
import re
class BOWEngine(SearchEngineBase):
def __init__(self):
super(BOWEngine, self).__init__()
self.__id_to_words = dict() def process_corpus(self, id, text):
self.__id_to_words[id] = self.parse_text_to_words(text) def search(self, query):
query_words = self.parse_text_to_words(query)
results = []
for id, words in self.__id_to_words.items():
if self.query_match(query_words, words):
results.append(id)
return results @staticmethod
def query_match(query_words, words):
for query_word in query_words:
if query_word not in words:
return False
return True
#for query_word in query_words:
# return False if query_word not in words else True #result = filter(lambda x:x not in words,query_words)
#return False if (len(list(result)) > 0) else True @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) search_engine = BOWEngine()
main(search_engine) ########## 输出 ##########
# i have a dream
# found 3 result(s):
# 1.txt
# 2.txt
# 3.txt
# freedom children
# found 1 result(s):
# 5.txt

  缺点:每次search还是需要遍历所有文档

Inverted index 倒序索引

  Inverted index 倒序索引,现在保留的是 word -> id 的字典
import re
class BOWInvertedIndexEngine(SearchEngineBase):
def __init__(self):
super(BOWInvertedIndexEngine, self).__init__()
self.inverted_index = dict() #生成索引 word -> id
def process_corpus(self, id, text):
words = self.parse_text_to_words(text)
for word in words:
if word not in self.inverted_index:
self.inverted_index[word] = []
self.inverted_index[word].append(id) #{'little':['1.txt','2.txt'],...} def search(self, query):
query_words = list(self.parse_text_to_words(query))
query_words_index = list()
for query_word in query_words:
query_words_index.append(0) # 如果某一个查询单词的倒序索引为空,我们就立刻返回
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] #['1.txt','2.txt'] # 已经遍历到了某一个倒序索引的末尾,结束 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) search_engine = BOWInvertedIndexEngine()
main(search_engine) ########## 输出 ########## # little
# found 2 result(s):
# 1.txt
# 2.txt
# little vicious
# found 1 result(s):
# 2.txt
 

LRUCache

  如果90%以上都是重复搜索,为了提高性能,考虑增加缓存,使用Least Recently Used 近期最少使用算法实现
import pylru
class LRUCache(object):
def __init__(self, size=32):
self.cache = pylru.lrucache(size) def has(self, key):
return key in self.cache def get(self, key):
return self.cache[key] def set(self, key, value):
self.cache[key] = value class BOWInvertedIndexEngineWithCache(BOWInvertedIndexEngine, LRUCache):
def __init__(self):
super(BOWInvertedIndexEngineWithCache, self).__init__()
LRUCache.__init__(self) def search(self, query):
if self.has(query):
print('cache hit!')
return self.get(query) result = super(BOWInvertedIndexEngineWithCache, self).search(query)
self.set(query, result) return result search_engine = BOWInvertedIndexEngineWithCache()
main(search_engine) ########## 输出 ##########
# little
# found 2 result(s):
# 1.txt
# 2.txt
# little
# cache hit!
# found 2 result(s):
# 1.txt
# 2.txt

  注意BOWInvertedIndexEngineWithCache继承了两个类。

  在构造函数里直接使用super(BOWInvertedIndexEngineWithCache, self).__init__()来初始化BOWInvertedIndexEngine父类
  对于多重继承的父类就要使用LRUCache.__init__(self)来初始化
  
  BOWInvertedIndexEngineWithCache里重载了search函数,在函数里面要调用父类BOWInvertedIndexEngine的search函数,使用:
  result = super(BOWInvertedIndexEngineWithCache, self).search(query)

参考

  极客时间《Python核心技术与实战》专栏

  https://time.geekbang.org/column/intro/176

Python基础:一起来面向对象 (二) 之搜索引擎的更多相关文章

  1. Python基础学习笔记(二)变量类型

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  2. (Python基础教程之十二)Python读写CSV文件

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  3. python基础整理4——面向对象装饰器惰性器及高级模块

    面向对象编程 面向过程:根据业务逻辑从上到下写代码 面向对象:将数据与函数绑定到一起,进行封装,这样能够更快速的开发程序,减少了重复代码的重写过程 面向对象编程(Object Oriented Pro ...

  4. python基础-9.1 面向对象进阶 super 类对象成员 类属性 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理

    上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...

  5. python基础篇_006_面向对象

    面向对象 1.初识类: # 定义一个函数,我们使用关键字 def """ def 函数名(参数): '''函数说明''' 函数体 return 返回值 "&qu ...

  6. python基础学习 Day19 面向对象的三大特性之多态、封装 property的用法(1)

    一.课前内容回顾 继承作用:提高代码的重用性(要继承父类的子类都实现相同的方法:抽象类.接口) 继承解释:当你开始编写两个类的时候,出现了重复的代码,通过继承来简化代码,把重复的代码放在父类中. 单继 ...

  7. python基础学习 Day19 面向对象的三大特性之多态、封装

    一.课前内容回顾 继承作用:提高代码的重用性(要继承父类的子类都实现相同的方法:抽象类.接口) 继承解释:当你开始编写两个类的时候,出现了重复的代码,通过继承来简化代码,把重复的代码放在父类中. 单继 ...

  8. python基础学习Day17 面向对象的三大特性之继承、类与对象名称空间小试

    一.课前回顾 类:具有相同属性和方法的一类事物 实例化:类名() 过程: 开辟了一块内存空间 执行init方法 封装属性 自动的把self返回给实例化对象的地方 对象:实例 一个实实在在存在的实体 组 ...

  9. python基础笔记之面向对象

    # class Foo:# name="kevin"## def __init__(self,puppy):# self.tomato= 'red'# self.dog = pup ...

随机推荐

  1. 【C语言天天练(九)】动态内存分配

    引言:数组的元素存储于内存中连续的位置上.当一个数组被声明时.它所须要的内存在编译时就被分配. 可是,我们能够使用动态内存分配在执行时为它分配内存. 一块内存的生命周期能够分为四个阶段:分配.初始化. ...

  2. HTML页面中点击按钮关闭页面几种方式与取消

    1.不带任何提示关闭窗口的js代码 <input type="button" name="close" value="关闭" oncl ...

  3. Xammp修改端口

    How can I get XAMPP working on port 80 under Windows 10? By default, Windows 10 starts Microsoft IIS ...

  4. WM_GETMINMAXINFO的作用 .

    如果想要实现窗口全屏,并且还有状态栏,会出现问题,那就是OnGetMinMaxInfo函数的作用.你可以试一下,如果把这个函数去掉,则当你按下工具栏中的全屏显示按钮时,框架视图确实变大了,但没有想象的 ...

  5. java File文件操作共用方法整理

    package org.jelly.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io ...

  6. Vue 之 npm 及 安装的包

    1  npm相关 1.1 npm 是 基于Node.js 的,所以要先安装Node.js 在浏览器地址栏输入https://nodejs.org/en/, 进入Node.js官网后,点击下载左边的稳定 ...

  7. sanic官方文档解析之Custom Protocols(自定义协议)和Socket(网络套接字)

    1,Custom Protocol:自定义协议 温馨提示:自定义协议是一个高级用法,大多数的读者不需要用到此功能 通过特殊的自定义协议,你可以改变sanic的协议,自定义协议需要继承子类asyncio ...

  8. 20170228 METHOD handle_data_changed-

    CALL METHOD er_data_changed->add_protocol_entry   METHOD handle_data_changed. DATA: ls_modi TYPE  ...

  9. redis的图形界面管理工具

    大部分人都知道redis是一款用在缓存服务器上的软件,它与memcache类似,都可以存储海量的数据,用在大访问量的web网站.聊天记录存放等方面,但是又与memcache不同: 1.缓存数据可以持久 ...

  10. Mysql常见函数

    一.单行函数 1.字符函数 concat拼接 substr截取子串 upper转换成大写 lower转换成小写 trim去前后指定的空格和字符 ltrim去左边空格 rtrim去右边空格 replac ...