nlu_utils.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import re
  2. from django.conf import settings
  3. from requests import post
  4. import json
  5. from difflib import SequenceMatcher
  6. from review.models import Review
  7. nlu_server_url = settings.NLU_SERVER_URI
  8. def filter_with_last_ten_reviews(location_id, replies):
  9. replies = list(replies)
  10. revs = Review.objects.filter(location_id=location_id).exclude(reply=None).order_by('-update_time')[:10]
  11. for r in revs:
  12. s1 = r.reply.replied_text
  13. for rep in replies:
  14. s2 = rep.reply
  15. similarity = SequenceMatcher(None, s1, s2).ratio()
  16. if similarity > 0.7:
  17. replies.remove(rep)
  18. print('%.2f'%similarity, ' -------------- ', rep.reply_category)
  19. return replies
  20. def clean_text(text):
  21. # replace some letter in text for getting better performance
  22. text = re.sub(r':\s*', ' ', text)
  23. text = re.sub(r'&', ', ', text)
  24. text = re.sub(r'/', ', ', text)
  25. text = re.sub(r'\.*\n\.*', ', ', text)
  26. text = re.sub(r'[dD][rR](\.|\s*)*', 'Dr. ', text)
  27. emoji_pattern = re.compile("["
  28. u"\U0001F600-\U0001F64F" # emoticons
  29. u"\U0001F300-\U0001F5FF" # symbols & pictographs
  30. u"\U0001F680-\U0001F6FF" # transport & map symbols
  31. u"\U0001F1E0-\U0001F1FF" # flags (iOS)
  32. "]+", flags=re.UNICODE)
  33. text = re.sub(emoji_pattern, ' ', text)
  34. return text
  35. def model_inference(text):
  36. url = nlu_server_url + '/model/parse'
  37. text = clean_text(text)
  38. payload = {'text': text}
  39. headers = {'content-type': 'application/json'}
  40. response = post(url, data=json.dumps(payload), headers=headers)
  41. if response.status_code == 200:
  42. return response.json()
  43. return response
  44. def is_a_name(name):
  45. '''
  46. function that decide whether it is a person name or not
  47. :param -> a string usually reviewer name:
  48. :return -> a boolean True/False:
  49. '''
  50. response = model_inference(name.title())
  51. entities = response.get('entities')
  52. if not entities:
  53. return False
  54. entity = entities[0]
  55. if entity.get('entity') == 'PERSON':
  56. return True
  57. else:
  58. return False
  59. def analyze_inference(response):
  60. '''
  61. response has four property
  62. ['intent', 'entities', 'intent_ranking', 'text']
  63. we took all intents that has more than 10% of intent confident.
  64. all the intents that has bellow confidence has been omitted.
  65. :param response: JSON -> a json response that RASA NLU server respond.
  66. :return: DICT ->dictionary with key of intent and value of it's confident.
  67. '''
  68. res_intents = response.get('intent_ranking')
  69. intents = {}
  70. for intent in res_intents:
  71. key = intent.get('name')
  72. values = intent.get('confidence')
  73. if values > 0.1:
  74. intents[key] = int(values*100)
  75. return intents