Commit e6b742b4 by lichenggang

增加批量识别接口

parent c1f8622e
......@@ -29,6 +29,18 @@ class KwHandler(BaseHandler):
res['status'] = code
self.write(res)
async def post(self):
target = self.request.body
list_data = json.loads(self.request.body)
predict_type = unquote(self.get_argument('type', 'all'))
bom_log.info(f'http收到识别关键词列表: {target}, 预测类型为{predict_type}')
code, res = self.predictorfac.predict(list_data, predict_type)
if code != 1:
res = code2msg(code)
bom_log.error(f'http关键词批量识别报错: ' + res['message'])
else:
res['status'] = code
self.write(res)
class DictHandler(BaseHandler):
async def post(self):
......
No preview for this file type
No preview for this file type
......@@ -14,31 +14,34 @@ class PredictorFac:
self.dict_predictor = dict_predict.DicPredict('dict')
self.txt_predictor = txt_predict.TxtPredict('txt')
def _get_predictor(self, data):
def _get_predictor(self, data, predict_type):
if isinstance(data, dict):
return self.dict_predictor
elif isinstance(data, list):
return self.txt_predictor
if predict_type == 'all':
return self.txt_predictor
elif predict_type == 'param':
return self.kw_predictor
else:
return self.kw_predictor
def predict(self, dic_data, predict_type='all'):
predictor = self._get_predictor(dic_data)
try:
res = predictor.predict(dic_data, predict_type)
if isinstance(predictor, dict_predict.DicPredict):
if '数量' not in str(res):
raise self.Status.NoQtyError
if '参数' not in str(res) and '型号' not in str(res):
raise self.Status.NoParamGnError
predictor = self._get_predictor(dic_data, predict_type)
res = predictor.predict(dic_data, predict_type)
if predict_type == 'all':
try:
if isinstance(predictor, dict_predict.DicPredict):
if '数量' not in str(res):
raise self.Status.NoQtyError
if '参数' not in str(res) and '型号' not in str(res):
raise self.Status.NoParamGnError
return 1, res
except Status.StatusErrTup as e:
stat_code = e.value
return stat_code, code2msg(stat_code)
elif predict_type == 'param':
return 1, res
except Status.StatusErrTup as e:
stat_code = e.value
return stat_code, code2msg(stat_code)
if __name__ == "__main__":
def get_test_data():
import pandas as pd
......
......@@ -5,16 +5,23 @@ from static_config import EN_TO_ZH_MAP
class KwPredict(BasePredictor):
def predict(self, string, predict_type='all'):
self.info.info('预测类型为: %s, 接收数据: %s' % (predict_type, string))
def predict(self, string_or_list, predict_type='all'):
self.info.info('预测类型为: %s, 接收数据: %s' % (predict_type, string_or_list))
if predict_type == 'all':
result, prab = self.get_single_predict(string, BasePredictor.model, BasePredictor.extractor)
result, prab = self.get_single_predict(string_or_list, BasePredictor.model, BasePredictor.extractor)
zh_prob = {EN_TO_ZH_MAP[k]: v for k, v in prab.items()}
res = {'result': EN_TO_ZH_MAP[result], 'probably_dict': zh_prob, 'predict_type': predict_type}
else:
model = getattr(BasePredictor, predict_type + '_model')
extractor = getattr(BasePredictor, predict_type + '_extractor')
result, prab = self.get_single_predict(string, model, extractor)
res = {'result': int(result), 'probably_dict': prab, 'predict_type': predict_type}
dic_result = {}
if isinstance(string_or_list, list):
for i in string_or_list:
result, prab = self.get_single_predict(i, model, extractor)
dic_result[i] = int(result[0])
res = {'result': dic_result, 'predict_type': predict_type}
else:
result= self.get_single_predict(string_or_list, model, extractor)
res = {'result': int(result[0]), 'predict_type': predict_type}
return res
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment