def test_get_text_entity_detection_data(self, mock_get_detection): input_data = { "messages": ["I want to go to Mumbai"], "bot_message": None, "language_script": "en", "source_language": "en", "entities": { "city": { "structured_value": None, "fallback_value": None, "predetected_values": None, "fuzziness": 4, "min_token_len_fuzziness": 4, "ignore_message": None }, "restaurant": { "structured_value": None, "fallback_value": None, "predetected_values": None, "fuzziness": None, "min_token_len_fuzziness": None, "ignore_message": True }, } } request = HttpRequest() request._body = json.dumps(input_data) mock_get_detection.return_value = [{'entities': {'city': [ {'entity_value': {'value': 'Mumbai', 'datastore_verified': True, 'model_verified': False}, 'detection': 'message', 'original_text': 'mumbai', 'language': 'en'}], 'restaurant': []}, 'language': 'en'}] output = get_text_entity_detection_data(request) assert_output = [{ 'entities': {'entities': {'city': [ {'entity_value': {'value': 'Mumbai', 'datastore_verified': True, 'model_verified': False}, 'detection': 'message', 'original_text': 'mumbai', 'language': 'en'}], 'restaurant': []}, 'language': 'en', 'restaurant': []}, 'language': 'en'}] self.assertListEqual(output, assert_output)
def text(request): """ Uses TextDetector to the get the values of multiple text entity detection. This is used for both single text message or multiple text message detection. Currently only POST method is supported. Args: request: request for text detection Request parameters message (list of str): list of message string for which detection logic needs to be run on. source_language (str): language for which the phone numbers have to be detected bot_message (str): previous message from a bot/agent. entities (dict): dictionary of entties to be detected, each entity dict will contain following details: entity_name (str): name of the entity. Also acts as elastic-search dictionary name if entity uses elastic-search lookup structured_value (str): [Optional] Value obtained from any structured elements. Note if structured value is detection is run on structured value instead of message (For example, UI elements like form, payload, etc) fallback_value (str): [Optional] If the detection logic fails to detect any value either from structured_value or message then we return a fallback_value as an output. use_fallback (bool): Default as False, if this is present for a single message fallback value will be used. fuzziness (int): [Optional] Fuzziness value for each entity min_token_size_for_fuzziness (int): [Optional] minimum size for token match Returns: response (django.http.response.HttpResponse): HttpResponse object Examples: 1) For single message: input request: { "message": ["I want to go to Jabalpur"], "bot_message": null, "language_script": "en", "source_language": "en", "entities": { "city": { "structured_value": "Delhi", "fallback_value": null, "predetected_values": ["Mumbai"], "fuzziness": null, "min_token_len_fuzziness": null, "use_fallback": false }, "restaurant": { "structured_value": null, "fallback_value": null, "predetected_values": null, "fuzziness": null, "min_token_len_fuzziness": null, "use_fallback": false } } } output response: { "success": true, "error": null, "data": [ { "entities": { "restaurant": [], "city": [ { "entity_value": { "value": "New Delhi", "datastore_verified": true, "model_verified": false }, "detection": "structure_value_verified", "original_text": "delhi", "language": "en" }, { "entity_value": { "value": "Mumbai", "datastore_verified": false, "model_verified": true }, "detection": "structure_value_verified", "original_text": "Mumbai", "language": "en" } ] }, "language": "en" } ] } """ data = [] if request.method == "GET": response = {"success": False, "error": "Get method is not allowed"} return JsonResponse(response, status=405) elif request.method == "POST": ner_logger.debug("Fetching result") try: verify_text_request(request) # if verify success get detection data data = get_text_entity_detection_data(request) except KeyError as err: response = {"success": False, "error": str(err)} # TODO: move to ner_logger.error ner_logger.exception(response) return JsonResponse(response, status=400) except TypeError as err: response = {"success": False, "error": str(err)} ner_logger.exception(response) return JsonResponse(response, status=400) except Exception as err: response = {"success": False, "error": str(err)} ner_logger.exception(response) return JsonResponse(response, status=500) if data: response = {"success": True, "error": None, "data": data} return JsonResponse(response, status=200) else: response = {"success": False, "error": "Some error while parsing"} return JsonResponse(response, status=500)