-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
44 lines (34 loc) · 1.17 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
from flask import Flask, request
from flask_restful import Resource, Api
from similarity.normalized_levenshtein import NormalizedLevenshtein
from flask_cors import CORS
from fuzzywuzzy import fuzz
from operator import itemgetter
app = Flask(__name__)
CORS(app)
api = Api(app)
from krs import get_krs_obj
companies_arr = get_krs_obj()
class Company(Resource):
def get(self, company_id):
print(company_id)
for obj in companies_arr:
if int(obj['id']) == company_id:
return obj
# TODO throw error
class Companies(Resource):
def get(self):
return companies_arr
class Search(Resource):
def get(self, company_name):
ratios = []
for obj in companies_arr:
ratio = fuzz.token_set_ratio(company_name, obj['nazwa'])
ratios.append((obj, ratio))
sorted_ratios = sorted(ratios, key=itemgetter(1), reverse=True)
return [obj for obj, ratio in sorted_ratios[:5]]
api.add_resource(Company, '/companies/<int:company_id>')
api.add_resource(Companies, '/companies')
api.add_resource(Search, '/search/<string:company_name>')
if __name__ == '__main__':
app.run(debug=True)