utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import re
  2. import requests
  3. import json
  4. from decimal import Decimal
  5. from django.conf import settings
  6. from .models import Staff
  7. from review.models import Review
  8. from facebook_app.models import FacebookReview, FacebookPage
  9. from yelp.models import YelpReview, YelpLocation
  10. ner_model_url = getattr(settings, 'NER_SERVER_URI')
  11. def get_all_names(text):
  12. url = ner_model_url + '/names/all'
  13. payload = {'text': text}
  14. headers = {'content-type': 'application/json'}
  15. res = requests.post(url, data=json.dumps(payload), headers=headers).json()
  16. return res.get('names')
  17. def all_staffs(location_id):
  18. names = []
  19. staffs = Staff.objects.filter(location_id=location_id)
  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(text, location_id):
  32. names = get_all_names(text)
  33. cleaned_names = []
  34. for name in names:
  35. for n in name.split():
  36. cleaned_names.append(n) if n.lower() in all_staffs(location_id) else None
  37. return cleaned_names
  38. def add_point_to_staff_profile(text, location_id, platform):
  39. staffs = Staff.objects.filter(location_id=location_id)
  40. names = extract_names(text, location_id)
  41. point_unit = Decimal(1/len(names)) if not names == [] else 0
  42. for name in names:
  43. for staff in staffs:
  44. if name.lower() in staff.get_nick_names:
  45. if platform == 'google':
  46. staff.name_mentioned_google += 1
  47. elif platform == 'facebook':
  48. staff.name_mentioned_facebook += 1
  49. # elif platform == 'yelp':
  50. # staff.name_mentioned_yelp += 1
  51. else:
  52. raise AttributeError('No such platform found.')
  53. staff.total_units += point_unit
  54. staff.save()
  55. def make_all_staffs_point_zero():
  56. staffs = Staff.objects.all()
  57. for s in staffs:
  58. s.total_units = 0
  59. s.name_mentioned_google = 0
  60. s.name_mentioned_facebook = 0
  61. s.name_mentioned_yelp = 0
  62. s.save()
  63. def extract_names_from_reviews(start_date, end_date, location_id=None):
  64. # For google reviews
  65. google_reviews = Review.objects.filter(
  66. location_id=location_id,
  67. create_time__range=(start_date, end_date)
  68. ).exclude(comment=None)
  69. for review in google_reviews:
  70. add_point_to_staff_profile(text=review.comment, location_id=location_id, platform='google')
  71. # For facebook review
  72. facebook_reviews = FacebookReview.objects.filter(
  73. page__location__location_id=location_id,
  74. create_time__range=(start_date, end_date)
  75. ).exclude(review_text=None)
  76. for review in facebook_reviews:
  77. add_point_to_staff_profile(text=review.review_text, location_id=location_id, platform='facebook')
  78. # # For yelp reviews
  79. # yelp_reviews = YelpReview.objects.filter(
  80. # location__location_id=location_id,
  81. # date_posted__range=(start_date, end_date)
  82. # ).exclude(comment=None)
  83. #
  84. # for review in yelp_reviews:
  85. # add_point_to_staff_profile(text=review.comment, location_id=location_id, platform='yelp')