__init__.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # coding:utf-8
  2. from a2s.a2s_client import a2s_execute
  3. from a2s.tools import json_serialize, json_deserialize
  4. from loguru import logger
  5. from docs.config import FieldServer, RecognitionServer, ProductServer
  6. def fields_ner(text):
  7. """
  8. 抽取文本字段
  9. :param text:
  10. :return:
  11. """
  12. try:
  13. if len(text) > 2000: return {}
  14. request_body = {"text": text}
  15. for r in range(FieldServer["retry_times"]):
  16. bytes_data = json_serialize(request_body)
  17. result = a2s_execute(FieldServer["a2s_ip"], FieldServer["topic"], FieldServer["timeout"], bytes_data)
  18. if result is None:
  19. continue
  20. response_json = json_deserialize(result)
  21. if response_json.get("ucode", 0) != 200:
  22. return {}
  23. return response_json
  24. except Exception as e:
  25. logger.info(str(e))
  26. return {}
  27. def org_ner(text):
  28. """
  29. 抽取物品
  30. :param text:
  31. :return:
  32. """
  33. try:
  34. companies = []
  35. if len(text) > 2000:
  36. return companies
  37. request_body = {"text": text}
  38. bytes_data = json_serialize(request_body)
  39. for r in range(RecognitionServer["retry_times"]):
  40. result = a2s_execute(RecognitionServer["a2s_ip"], RecognitionServer["topic"], RecognitionServer["timeout"],
  41. bytes_data)
  42. if result is None:
  43. continue
  44. response_json = json_deserialize(result)
  45. if response_json.get("ucode", 0) != 200:
  46. return companies
  47. output = response_json.get("output", [])
  48. for text in output:
  49. text_type = text.get("type", "")
  50. if text_type == "ORG":
  51. companies.append(text.get("span", ""))
  52. return companies
  53. except Exception as e:
  54. logger.info(str(e))
  55. return {}
  56. def product_detail_server(text):
  57. """
  58. 物品
  59. :param text:
  60. :return:
  61. """
  62. try:
  63. ret = a2s_execute(ProductServer["a2s_ip"], ProductServer["topic"], ProductServer["timeout"],
  64. bytes_data=json_serialize({"text": text}))
  65. model_dict = json_deserialize(ret)
  66. model_dict = model_dict if isinstance(model_dict, dict) else {}
  67. return model_dict
  68. except Exception as e:
  69. print(e)
  70. return {}
  71. if __name__ == '__main__':
  72. data = "型号:清廉长沙水350ml"
  73. # result = org_ner(data)
  74. result = product_detail_server(data)
  75. print(result)