utils.py 1.9 KB

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