gaurav22iiserb's picture
first
305a7f0
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)