資源簡介
10000網(wǎng)頁簡單上手,代碼和配置環(huán)境全部寫好,傻瓜式操作。
執(zhí)行文件順序是
1.Spider.py進行爬取網(wǎng)頁并分析
2.index.py生成倒排索引,idf文件
3.app.py運行搜索引擎
4.在默認瀏覽器輸入 http://localhost:8080/ 進行檢索
若要使用數(shù)據(jù)庫
1.writeDB.py寫倒排索引與idf(時間較長)
2.可以在summary.py切換from DB_search import s使用數(shù)據(jù)庫
注意:
還需要停用詞是stopwords.txt
執(zhí)行文件順序是
1.Spider.py進行爬取網(wǎng)頁并分析
2.index.py生成倒排索引,idf文件
3.app.py運行搜索引擎
4.在默認瀏覽器輸入 http://localhost:8080/ 進行檢索
若要使用數(shù)據(jù)庫
1.writeDB.py寫倒排索引與idf(時間較長)
2.可以在summary.py切換from DB_search import s使用數(shù)據(jù)庫
注意:
還需要停用詞是stopwords.txt

代碼片段和文件信息
#?coding:utf-8
from?flask?import?Flask?request?render_template?redirect?url_for
import?jieba
from?flask_bootstrap?import?Bootstrap
from?summary?import??getsummary
from?flask_paginate?import?Pagination?get_page_args
import?json
import?time
app?=?Flask(__name__?static_url_path=‘‘)
bootstrap?=?Bootstrap(app)
with?open(‘idf.json‘?‘r‘)?as?f:#預(yù)加載idf文件
????idf?=?json.load(f)????
????
@app.route(“/“?methods=[‘POST‘?‘GET‘])#設(shè)置檢索首頁的路由
def?main():
????if?request.method?==?‘POST‘?and?request.form.get(‘query‘):
????????query?=?request.form[‘query‘]
????????print(query)
????????return?redirect(url_for(‘search‘?query=query))
????
????return?render_template(‘baidu.html‘)
@app.route(“/q/“?methods=[‘POST‘?‘GET‘])#設(shè)置檢索頁的路由
def?search(query):
????start=time.time()#開始計時
????page?per_page?offset?=?get_page_args(page_parameter=‘page‘
???????????????????????????????????????????per_page_parameter=‘per_page‘)
????terms?=?[i?for?i?in?jieba.cut_for_search(query.replace(“?“?““))?if?len(i)>1?and?i?in?idf]#處理檢索式
????terms.sort(key?=?lambda?i:len(i)reverse=True)
????#獲得摘要等信息????
????allllength=?getsummary(termspage)#page=0返回0-9,page=1返回10-19
????result?=?highlight(alll?terms)
????#實現(xiàn)分頁
????pagination?=?Pagination(page=page?per_page=10?total=length-1
????????????????????????????css_framework=‘bootstrap4‘)
????a=time.time()-start#結(jié)束計時
????#將參數(shù)返回給網(wǎng)頁
????return?render_template(‘search.html‘?docs=result?value=query?length=lengthpagination=paginationt=str(‘%.3f‘%a))
#對關(guān)鍵詞進行高亮
def?highlight(alll?terms):
????result?=?[]
????titles=[]
????contents=[]
????for?doc?in?alll[0]:#標題高亮
????????for?term?in?terms:
????????????doc=doc.replace(term?‘{}‘.format(term))
????????titles.append(doc)
????i=0
????for?doc?in?alll[1]:#正文高亮
????????print(i)
????????i+=1
????????if?doc?!=?None:
????????????for?term?in?terms:
????????????????doc=doc.replace(term?‘{}‘.format(term))
????????else:
????????????doc=“無法查詢“
????????contents.append(doc+“...“)
????for?i?in?range(len(titles)):
????????result.append([alll[2][i]contents[i]titles[i]])
????return?result
if?__name__?==?“__main__“:
????jieba.add_word(‘重慶理工大學‘)#jieba詞典擴充
????jieba.add_word(‘馬克思‘)
????app.run(host=‘localhost‘?port=8080?debug=True)#http://localhost:8080/
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2565??2020-01-07?10:50??小型搜索引擎程序\app.py
?????文件???????3914??2020-01-07?10:51??小型搜索引擎程序\DB_search.py
?????文件???????2756??2020-01-07?10:53??小型搜索引擎程序\index.py
?????文件???????3634??2020-01-07?10:53??小型搜索引擎程序\search.py
?????文件???????5177??2020-01-07?10:54??小型搜索引擎程序\Spider.py
?????文件????????661??2020-01-02?17:04??小型搜索引擎程序\static\baidu.html
?????文件????????628??2020-01-02?17:04??小型搜索引擎程序\static\css\baidu.css
?????文件???????5876??2019-12-24?10:35??小型搜索引擎程序\static\images\1.png
?????文件???????4230??2020-01-02?16:53??小型搜索引擎程序\static\images\2.jpg
?????文件??????85600??2020-01-02?16:45??小型搜索引擎程序\static\images\2.png
?????文件???????7877??2019-12-04?19:40??小型搜索引擎程序\static\images\baidu.jpg
?????文件???????2280??2019-12-24?10:36??小型搜索引擎程序\static\images\icon_book.png
?????文件???????3127??2020-01-02?09:40??小型搜索引擎程序\static\search.html
?????文件??????????0??2018-12-22?15:22??小型搜索引擎程序\static\__init__.py
?????文件???????4753??2020-01-07?10:55??小型搜索引擎程序\summary.py
?????文件????????714??2020-01-02?17:46??小型搜索引擎程序\templates\baidu.html
?????文件???????3279??2020-01-02?17:45??小型搜索引擎程序\templates\search.html
?????文件???????1145??2020-01-07?10:55??小型搜索引擎程序\top_k.py
?????文件???????3200??2020-01-07?10:56??小型搜索引擎程序\writeDB.py
?????文件???????2668??2020-01-06?17:16??小型搜索引擎程序\__pycache__\DB_search.cpython-37.pyc
?????文件???????2401??2020-01-06?17:22??小型搜索引擎程序\__pycache__\search.cpython-37.pyc
?????文件???????4573??2020-01-06?17:22??小型搜索引擎程序\__pycache__\summary.cpython-37.pyc
?????文件????????880??2020-01-02?19:57??小型搜索引擎程序\__pycache__\top_k.cpython-37.pyc
?????文件???????2389??2020-01-06?17:16??小型搜索引擎程序\__pycache__\writeDB.cpython-37.pyc
?????文件????????581??2020-01-07?10:44??小型搜索引擎程序\環(huán)境配置文檔.txt
?????目錄??????????0??2020-01-07?10:29??小型搜索引擎程序\static\css
?????目錄??????????0??2020-01-07?10:29??小型搜索引擎程序\static\images
?????目錄??????????0??2020-01-07?10:29??小型搜索引擎程序\static
?????目錄??????????0??2020-01-07?10:29??小型搜索引擎程序\templates
?????目錄??????????0??2020-01-07?10:29??小型搜索引擎程序\__pycache__
............此處省略4個文件信息
評論
共有 條評論