-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
42 lines (33 loc) · 1.25 KB
/
app.py
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
40
41
42
from flask import Flask, render_template, request
import pandas as pd
import textdistance
import re
from collections import Counter
app = Flask(__name__)
words = []
with open('text book.txt', 'r', encoding='utf-8') as f:
data = f.read().lower()
words = re.findall('\w+', data)
words += words
V = set(words)
words_freq_dict = Counter(words)
Total = sum(words_freq_dict.values())
probs = {}
for k in words_freq_dict.keys():
probs[k] = words_freq_dict[k] / Total
@app.route('/')
def index():
return render_template('index.html', suggestions=None)
@app.route('/suggest', methods=['POST'])
def suggest():
keyword = request.form['keyword'].lower()
if keyword:
similarities = [1 - textdistance.Jaccard(qval=2).distance(v, keyword) for v in words_freq_dict.keys()]
df = pd.DataFrame.from_dict(probs, orient='index').reset_index()
df.columns = ['Word', 'Prob']
df['Similarity'] = similarities
suggestions = df.sort_values(['Similarity', 'Prob'], ascending=False)[['Word', 'Similarity']]
suggestions_list = suggestions.to_dict('records') # Convert DataFrame to list of dictionaries
return render_template('index.html', suggestions=suggestions_list)
if __name__ == '__main__':
app.run(debug=True)