-
Notifications
You must be signed in to change notification settings - Fork 28
/
city_name.py
59 lines (47 loc) · 1.5 KB
/
city_name.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
54
55
56
57
58
59
from pathlib import Path
from flask import Flask, Response, abort, jsonify, request
from fuzzywuzzy import process
with open(Path("prefered_labels.txt").resolve(), "r") as f:
prefered_labels = f.readlines()
def correct_text(text: str) -> str:
"""[summary]
Arguments:
text {str} -- [Input label for the name]
Returns:
str -- closest fuzzy string match to the input if
fuzz.ratio is greater than 85 else the same input str
"""
if not text:
return
output = {}
output[process.extractOne(text, prefered_labels)[0]] = process.extractOne(
text, prefered_labels
)[1]
if len(text.split()) > 1:
output[
process.extractOne(text.split()[0], prefered_labels)[0]
] = process.extractOne(text, prefered_labels)[1]
for key, value in output.items():
if value == 100:
return key
elif value > 85:
return key
return text
app = Flask(__name__)
@app.route("/city_name", methods=["POST"])
def correct_city_name():
try:
req_data = request.get_json()
results = {
"correct-port-of-origin-of-journey": correct_text(
req_data["port-of-origin-of-journey"]
).replace("\n", "")
}
except TypeError:
# abort when not JSON
abort(400)
except KeyError:
# return error when no org paramter
return jsonify(error="Not the correct request format!")
return jsonify(results)
app.run()