nlu_utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from django.conf import settings
  2. from requests import post
  3. import json
  4. nlu_server_url = settings.NLU_SERVER_URI
  5. def model_inference(text):
  6. url = nlu_server_url + '/model/parse'
  7. payload = {'text': text}
  8. headers = {'content-type': 'application/json'}
  9. response = post(url, data=json.dumps(payload), headers=headers)
  10. if response.status_code == 200:
  11. return response.json()
  12. return response
  13. def is_a_name(name):
  14. '''
  15. function that decide whether it is a person name or not
  16. :param -> a string usually reviewer name:
  17. :return -> a boolean True/False:
  18. '''
  19. response = model_inference(name.title())
  20. entities = response.get('entities')
  21. if not entities:
  22. return False
  23. entity = entities[0]
  24. if entity.get('entity') == 'PERSON':
  25. return True
  26. else:
  27. return False
  28. def analyze_inference(response):
  29. '''
  30. response has four property
  31. ['intent', 'entities', 'intent_ranking', 'text']
  32. we took all intents that has more than 10% of intent confident.
  33. all the intents that has bellow confidence has been omitted.
  34. :param response:
  35. :return: dictionary with key of intent and value of it's confident.
  36. '''
  37. res_intents = response.get('intent_ranking')
  38. intents = {}
  39. for intent in res_intents:
  40. key = intent.get('name')
  41. values = intent.get('confidence')
  42. if values > 0.1:
  43. intents[key] = int(values*100)
  44. return intents