python 实现倒排索引,建立简单的搜索引擎
生活随笔
收集整理的這篇文章主要介紹了
python 实现倒排索引,建立简单的搜索引擎
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文將用python實現倒排索引
如下,一個數據表docu_set中有三篇文章的,d1,d2,d3,如下
docu_set={'d1':'i love shanghai','d2':'i am from shanghai now i study in tongji university','d3':'i am from lanzhou now i study in lanzhou university of science and technolgy',}下面用這張表做一個簡單的搜索引擎,采用倒排索引
首先對所有文檔做分詞,得到文章的詞向量集合
首先對所有文檔做分詞,得到文章的詞向量集合
{'now', 'study', 'shanghai', 'am', 'in', 'university', 'and', 'from', 'tongji', 'i', 'of', 'lanzhou', 'love', 'technolgy', 'science'}構建倒排索引
invert_index=dict() for b in set_all_words:temp=[]for j in docu_set.keys():field=docu_set[j]split_field=field.split()if b in split_field:temp.append(j)invert_index[b]=temp print(invert_index)倒排索引如下
{'now': ['d2', 'd3'],'study': ['d2', 'd3'],'shanghai': ['d1', 'd2'],'am': ['d2', 'd3'],'in': ['d2', 'd3'],'university': ['d2', 'd3'],'and': ['d3'],'from': ['d2', 'd3'],'tongji': ['d2'],'i': ['d1', 'd2', 'd3'],'of': ['d3'],'lanzhou': ['d3'],'love': ['d1'],'technolgy': ['d3'],'science': ['d3']}全文搜索 ‘university’
invert_index['university']‘university’,在文檔 ‘d2’, 'd3’中,完成搜索
['d2', 'd3']代碼在git
參考文章
總結
以上是生活随笔為你收集整理的python 实现倒排索引,建立简单的搜索引擎的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 程序网页运行
- 下一篇: python 栈道实现