Spaces:
No application file
No application file
File size: 1,012 Bytes
305a7f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from flask import Flask, render_template, request, jsonify
from rank_bm25 import BM25Okapi
import numpy as np
app = Flask(__name__)
corpus = [
"Hello there good man!",
"It is quite windy in London",
"How is the weather today?"
]
tokenized_corpus = [doc.split(" ") for doc in corpus]
bm25 = BM25Okapi(tokenized_corpus)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/search', methods=['POST'])
def search():
query = request.form['query']
if not query:
return render_template('index.html', error='Query is required')
tokenized_query = query.split(" ")
doc_scores = bm25.get_scores(tokenized_query)
doc_scores = -np.sort(-np.array(doc_scores))
doc_scores=doc_scores[:20]
top_n_docs = bm25.get_top_n(tokenized_query, corpus, n=3)
return render_template('index.html', query=query, doc_scores=doc_scores, top_n_docs=top_n_docs)
if __name__ == '__main__':
app.run(debug=True)
|