From 414491b3de5b4154b1caa1bf9090becfb0f4b848 Mon Sep 17 00:00:00 2001 From: zetan Date: Tue, 28 Jan 2020 21:04:21 +0800 Subject: [PATCH] Add search hospital search feature --- const.py | 9 +++++++++ utils.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/const.py b/const.py index 6b4cfd4..e3c0499 100644 --- a/const.py +++ b/const.py @@ -17,6 +17,15 @@ 'factory_capacity', 'factory_contact', ] +HOSPICAL_HEADERS = [ + 'hospital_name', + 'hospital_addr', + 'hospital_headcount', + 'hospital_requirement', + 'hospital_link', + 'hospital_contact', + 'hospital_comment' +] CLINIC_HEADERS = [ 'clinic_unit', 'clinic_contact', diff --git a/utils.py b/utils.py index 7f2684b..1d9603d 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- from flask import Blueprint +from flask import request +import codecs import os import json import datetime @@ -40,12 +42,35 @@ """ def csv_helper(fpath, headers): result = [] - with open(fpath) as f: + with codecs.open(fpath, encoding='utf-8') as f: for line in f.readlines()[1:]: csv_data = line.strip().split(",") result.append(dict(zip(headers,csv_data))) return result +def filter_entity(entity_list, search_fields, keywords): + recall_set = [] + for entity in entity_list: + if is_valid_entity(entity, search_fields, keywords): + recall_set.append(entity) + return recall_set + +def is_valid_entity(entity, search_fields, keywords): + field_values = [] + for search_field in search_fields: + if search_field in entity: + field_values.append(entity[search_field]) + field_val_concat = ' '.join(field_values).lower() + is_valid = True + for keyword in keywords.split(' '): + try: + if keyword.lower() not in field_val_concat: + is_valid = False + break + except Exception as ex: + is_valid = False + return is_valid + def yaml_helper(fpath): result = [] with open(fpath, 'r') as f: @@ -61,7 +86,7 @@ def hospital_list(): 'msg': '', } try: - resp_data = csv_helper(HOSPITAL_PATH,HOTEL_HEADERS) + resp_data = csv_helper(HOSPITAL_PATH,HOSPICAL_HEADERS) resp['success'] = True resp['data'] = resp_data except Exception as e: @@ -69,7 +94,25 @@ def hospital_list(): return json.dumps(resp, ensure_ascii=False) - +@data.route('/hospital_search') +def hospital_search(): + keyword = request.args.get('keyword') + print('keyword: %s' % keyword) + resp = { + 'success': False, + 'data': [], + 'msg': '', + } + try: + hospital_all = csv_helper(HOSPITAL_PATH,HOSPICAL_HEADERS) + search_fields = ['hospital_name', 'hospital_addr', 'hospital_requirement'] + print('search_fields created: %s' % search_fields) + search_result = filter_entity(hospital_all, search_fields, keyword) + resp['success'] = True + resp['data'] = search_result + except Exception as e: + resp['msg'] = str(e) + return json.dumps(resp, ensure_ascii=False) @data.route('/hotel_list') def hotel_list():