|
@@ -3,6 +3,10 @@ from decimal import Decimal
|
|
from django.conf import settings
|
|
from django.conf import settings
|
|
from .models import Staff
|
|
from .models import Staff
|
|
|
|
|
|
|
|
+from review.models import Review
|
|
|
|
+from facebook_app.models import FacebookReview, FacebookPage
|
|
|
|
+from yelp.models import YelpReview, YelpLocation
|
|
|
|
+
|
|
ner_model = getattr(settings, 'SPACY_NER_MODEL')
|
|
ner_model = getattr(settings, 'SPACY_NER_MODEL')
|
|
|
|
|
|
|
|
|
|
@@ -12,9 +16,9 @@ def get_all_names(text):
|
|
return names
|
|
return names
|
|
|
|
|
|
|
|
|
|
-def all_staffs(location):
|
|
|
|
|
|
+def all_staffs(location_id):
|
|
names = []
|
|
names = []
|
|
- staffs = Staff.objects.filter(location=location)
|
|
|
|
|
|
+ staffs = Staff.objects.filter(location_id=location_id)
|
|
for s in staffs:
|
|
for s in staffs:
|
|
names.extend(s.get_nick_names)
|
|
names.extend(s.get_nick_names)
|
|
return set(names)
|
|
return set(names)
|
|
@@ -30,25 +34,31 @@ def clean_text(text):
|
|
return text
|
|
return text
|
|
|
|
|
|
|
|
|
|
-def extract_names(review):
|
|
|
|
- text = clean_text(review.comment)
|
|
|
|
|
|
+def extract_names(text, location_id):
|
|
names = get_all_names(text)
|
|
names = get_all_names(text)
|
|
cleaned_names = []
|
|
cleaned_names = []
|
|
for name in names:
|
|
for name in names:
|
|
for n in name.split():
|
|
for n in name.split():
|
|
- cleaned_names.append(n) if n.lower() in all_staffs(review.location) else None
|
|
|
|
|
|
+ cleaned_names.append(n) if n.lower() in all_staffs(location_id) else None
|
|
return cleaned_names
|
|
return cleaned_names
|
|
|
|
|
|
|
|
|
|
-def add_point_to_staff_profile(review):
|
|
|
|
- staffs = Staff.objects.filter(location=review.location)
|
|
|
|
- names = extract_names(review)
|
|
|
|
|
|
+def add_point_to_staff_profile(text, location_id, platform):
|
|
|
|
+ staffs = Staff.objects.filter(location_id=location_id)
|
|
|
|
+ names = extract_names(text, location_id)
|
|
point_unit = Decimal(1/len(names)) if not names == [] else 0
|
|
point_unit = Decimal(1/len(names)) if not names == [] else 0
|
|
|
|
|
|
for name in names:
|
|
for name in names:
|
|
for staff in staffs:
|
|
for staff in staffs:
|
|
if name.lower() in staff.get_nick_names:
|
|
if name.lower() in staff.get_nick_names:
|
|
- staff.name_mentioned += 1
|
|
|
|
|
|
+ if platform == 'google':
|
|
|
|
+ staff.name_mentioned_google += 1
|
|
|
|
+ elif platform == 'facebook':
|
|
|
|
+ staff.name_mentioned_facebook += 1
|
|
|
|
+ elif platform == 'yelp':
|
|
|
|
+ staff.name_mentioned_yelp += 1
|
|
|
|
+ else:
|
|
|
|
+ raise AttributeError('No such platform found.')
|
|
staff.total_units += point_unit
|
|
staff.total_units += point_unit
|
|
staff.save()
|
|
staff.save()
|
|
|
|
|
|
@@ -57,10 +67,36 @@ def make_all_staffs_point_zero():
|
|
staffs = Staff.objects.all()
|
|
staffs = Staff.objects.all()
|
|
for s in staffs:
|
|
for s in staffs:
|
|
s.total_units = 0
|
|
s.total_units = 0
|
|
- s.name_mentioned = 0
|
|
|
|
|
|
+ s.name_mentioned_google = 0
|
|
|
|
+ s.name_mentioned_facebook = 0
|
|
|
|
+ s.name_mentioned_yelp = 0
|
|
s.save()
|
|
s.save()
|
|
|
|
|
|
|
|
|
|
-def extract_names_from_reviews(reviews):
|
|
|
|
- for review in reviews:
|
|
|
|
- add_point_to_staff_profile(review)
|
|
|
|
|
|
+def extract_names_from_reviews(start_date, end_date, location_id=None):
|
|
|
|
+ # For google reviews
|
|
|
|
+ google_reviews = Review.objects.filter(
|
|
|
|
+ location_id=location_id,
|
|
|
|
+ create_time__range=(start_date, end_date)
|
|
|
|
+ ).exclude(comment=None)
|
|
|
|
+
|
|
|
|
+ for review in google_reviews:
|
|
|
|
+ add_point_to_staff_profile(text=review.comment, location_id=location_id, platform='google')
|
|
|
|
+
|
|
|
|
+ # For facebook review
|
|
|
|
+ facebook_reviews = FacebookReview.objects.filter(
|
|
|
|
+ page__location__location_id=location_id,
|
|
|
|
+ create_time__range=(start_date, end_date)
|
|
|
|
+ ).exclude(review_text=None)
|
|
|
|
+
|
|
|
|
+ for review in facebook_reviews:
|
|
|
|
+ add_point_to_staff_profile(text=review.review_text, location_id=location_id, platform='facebook')
|
|
|
|
+
|
|
|
|
+ # For yelp reviews
|
|
|
|
+ yelp_reviews = YelpReview.objects.filter(
|
|
|
|
+ location__location_id=location_id,
|
|
|
|
+ date_posted__range=(start_date, end_date)
|
|
|
|
+ ).exclude(comment=None)
|
|
|
|
+
|
|
|
|
+ for review in yelp_reviews:
|
|
|
|
+ add_point_to_staff_profile(text=review.comment, location_id=location_id, platform='yelp')
|