-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
53 lines (43 loc) · 1.64 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
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, request, jsonify
from symspellpy import SymSpell, Verbosity
import os
app = Flask(__name__)
# 1. Create SymSpell object and load dictionary
max_edit_distance_dictionary = 2
prefix_length = 7
sym_spell = SymSpell(max_edit_distance_dictionary, prefix_length)
# Path to your frequency dictionary
dictionary_path = "frequency_dictionary_az_80k.txt"
# Check if dictionary file exists
if not os.path.exists(dictionary_path):
raise FileNotFoundError(f"Dictionary file not found: {dictionary_path}")
# Load dictionary
term_index = 0 # Column index for terms
count_index = 1 # Column index for frequencies
if not sym_spell.load_dictionary(dictionary_path, term_index, count_index):
raise Exception("Failed to load dictionary")
# 2. Define API endpoint for word correction
@app.route('/correct', methods=['GET'])
def correct():
# Get word or phrase from query parameters
input_term = request.args.get('text', '')
if not input_term:
return jsonify({'error': 'Parameter "text" is missing'}), 400
# Perform lookup for suggestions
max_edit_distance_lookup = 2
suggestions = sym_spell.lookup(input_term, Verbosity.CLOSEST, max_edit_distance_lookup)
# Format response
results = []
for suggestion in suggestions:
results.append({
'term': suggestion.term,
'distance': suggestion.distance,
'count': suggestion.count
})
if results:
return jsonify({'suggestions': results})
else:
return jsonify({'message': 'No suggestions found'}), 200
if __name__ == '__main__':
# Run server on port 5000
app.run(host='0.0.0.0', port=5000)