utils.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import re
  2. from decimal import Decimal
  3. from django.conf import settings
  4. from .models import Staff
  5. from nlu_job.nlu_utils import model_inference
  6. nlu_url = settings.NLU_SERVER_URI
  7. def get_all_names(text):
  8. res = model_inference(text)
  9. entities = res.get('entities')
  10. names = {ent.get('value') for ent in entities if ent.get('entity') in ['PERSON', 'ORG']}
  11. return names
  12. def get_all_people_names(text):
  13. res = model_inference(text)
  14. entities = res.get('entities')
  15. names = {ent.get('value') for ent in entities if ent.get('entity') == 'PERSON'}
  16. return names
  17. def all_staffs(location):
  18. names = []
  19. staffs = Staff.objects.filter(location=location)
  20. for s in staffs:
  21. names.extend(s.get_nick_names)
  22. return set(names)
  23. def clean_text(text):
  24. # replace some letter in text for getting better performance
  25. text = re.sub(r':\s*', ' ', text)
  26. text = re.sub(r'&', ', ', text)
  27. text = re.sub(r'/', ', ', text)
  28. text = re.sub(r'\.*\n\.*', '.', text)
  29. text = re.sub(r'[dD][rR](\.|\s*)*', 'Dr. ', text)
  30. return text
  31. def extract_names(review):
  32. text = clean_text(review.comment)
  33. names = get_all_names(text)
  34. cleaned_names = []
  35. for name in names:
  36. for n in name.split():
  37. cleaned_names.append(n) if n.lower() in all_staffs(review.location) else None
  38. return cleaned_names
  39. def add_point_to_staff_profile(review):
  40. staffs = Staff.objects.filter(location=review.location)
  41. names = extract_names(review)
  42. point_unit = Decimal(1/len(names)) if not names == [] else 0
  43. for name in names:
  44. for staff in staffs:
  45. if name.lower() in staff.get_nick_names:
  46. staff.name_mentioned += 1
  47. staff.total_units += point_unit
  48. staff.save()
  49. def make_all_staffs_point_zero():
  50. staffs = Staff.objects.all()
  51. for s in staffs:
  52. s.total_units = 0
  53. s.name_mentioned = 0
  54. s.save()
  55. def extract_names_from_reviews(reviews):
  56. for review in reviews:
  57. add_point_to_staff_profile(review)