utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import re
  2. from decimal import Decimal
  3. from django.conf import settings
  4. from .models import Staff
  5. from review.models import Review
  6. from facebook_app.models import FacebookReview, FacebookPage
  7. from yelp.models import YelpReview, YelpLocation
  8. ner_model = getattr(settings, 'SPACY_NER_MODEL')
  9. def get_all_names(text):
  10. doc = ner_model(text)
  11. names = {ent.text for ent in doc.ents if ent.label_ in ['PERSON', 'ORG', 'PRODUCT']}
  12. return names
  13. def all_staffs(location_id):
  14. names = []
  15. staffs = Staff.objects.filter(location_id=location_id)
  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(text, location_id):
  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(location_id) else None
  33. return cleaned_names
  34. def add_point_to_staff_profile(text, location_id, platform):
  35. staffs = Staff.objects.filter(location_id=location_id)
  36. names = extract_names(text, location_id)
  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. if platform == 'google':
  42. staff.name_mentioned_google += 1
  43. elif platform == 'facebook':
  44. staff.name_mentioned_facebook += 1
  45. elif platform == 'yelp':
  46. staff.name_mentioned_yelp += 1
  47. else:
  48. raise AttributeError('No such platform found.')
  49. staff.total_units += point_unit
  50. staff.save()
  51. def make_all_staffs_point_zero():
  52. staffs = Staff.objects.all()
  53. for s in staffs:
  54. s.total_units = 0
  55. s.name_mentioned_google = 0
  56. s.name_mentioned_facebook = 0
  57. s.name_mentioned_yelp = 0
  58. s.save()
  59. def extract_names_from_reviews(start_date, end_date, location_id=None):
  60. # For google reviews
  61. google_reviews = Review.objects.filter(
  62. location_id=location_id,
  63. create_time__range=(start_date, end_date)
  64. ).exclude(comment=None)
  65. for review in google_reviews:
  66. add_point_to_staff_profile(text=review.comment, location_id=location_id, platform='google')
  67. # For facebook review
  68. facebook_reviews = FacebookReview.objects.filter(
  69. page__location__location_id=location_id,
  70. create_time__range=(start_date, end_date)
  71. ).exclude(review_text=None)
  72. for review in facebook_reviews:
  73. add_point_to_staff_profile(text=review.review_text, location_id=location_id, platform='facebook')
  74. # For yelp reviews
  75. yelp_reviews = YelpReview.objects.filter(
  76. location__location_id=location_id,
  77. date_posted__range=(start_date, end_date)
  78. ).exclude(comment=None)
  79. for review in yelp_reviews:
  80. add_point_to_staff_profile(text=review.comment, location_id=location_id, platform='yelp')