utils.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 all_staffs(location):
  13. names = []
  14. staffs = Staff.objects.filter(location=location)
  15. for s in staffs:
  16. names.extend(s.get_nick_names)
  17. return set(names)
  18. def clean_text(text):
  19. # replace some letter in text for getting better performance
  20. text = re.sub(r':\s*', ' ', text)
  21. text = re.sub(r'&', ', ', text)
  22. text = re.sub(r'/', ', ', text)
  23. text = re.sub(r'\.*\n\.*', '.', text)
  24. text = re.sub(r'[dD][rR](\.|\s*)*', 'Dr. ', text)
  25. return text
  26. def extract_names(review):
  27. text = clean_text(review.comment)
  28. names = get_all_names(text)
  29. cleaned_names = []
  30. for name in names:
  31. for n in name.split():
  32. cleaned_names.append(n) if n.lower() in all_staffs(review.location) else None
  33. return cleaned_names
  34. def add_point_to_staff_profile(review):
  35. staffs = Staff.objects.filter(location=review.location)
  36. names = extract_names(review)
  37. point_unit = Decimal(1/len(names)) if not names == [] else 0
  38. for name in names:
  39. for staff in staffs:
  40. if name.lower() in staff.get_nick_names:
  41. staff.name_mentioned += 1
  42. staff.total_units += point_unit
  43. staff.save()
  44. def make_all_staffs_point_zero():
  45. staffs = Staff.objects.all()
  46. for s in staffs:
  47. s.total_units = 0
  48. s.name_mentioned = 0
  49. s.save()
  50. def extract_names_from_reviews(reviews):
  51. for review in reviews:
  52. add_point_to_staff_profile(review)