Переглянути джерело

extract names for all platform

Mohidul Islam 4 роки тому
батько
коміт
b272506e40

+ 4 - 0
dashboard/static/user-dashboard.css

@@ -175,4 +175,8 @@ body {
 }
 #id_nick_names {
     height: 4rem;
+}
+
+.card-body span {
+    font-size: 1rem;
 }

+ 24 - 9
facebook_app/review_utils.py

@@ -69,15 +69,30 @@ def store_reviews_into_db(page_id):
         start_time = graph_story.get('start_time')
         recommendation_type = RECOMMENDATION_STR2NUM[graph_story.get('data')['recommendation_type']]
 
-        review, created = FacebookReview.objects.update_or_create(
-            id=review_id,
-            recommendation_type=recommendation_type,
-            review_text=message,
-            create_time=start_time,
-            page_id=page_id
-        )
-        if created:
-            print(f'New Facebook review has been created at {page_id}!')
+        # review, created = FacebookReview.objects.update_or_create(
+        #     id=review_id,
+        #     recommendation_type=recommendation_type,
+        #     review_text=message,
+        #     create_time=start_time,
+        #     page_id=page_id
+        # )
+        try:
+            review = FacebookReview.objects.get(id=review_id)
+            review.recommendation_type = recommendation_type
+            review.review_text = message
+            review.create_time = start_time
+            review.page_id = page_id
+            review.save()
+
+        except FacebookReview.DoesNotExist:
+            review = FacebookReview.objects.create(
+                id=review_id,
+                recommendation_type=recommendation_type,
+                review_text=message,
+                create_time=start_time,
+                page_id=page_id
+            )
+            print(f'New Facebook review has been created at {review.page_id}!')
 
         # store the review reply that is done by signature care
         store_review_reply(review_id=review_id, page_id=page_id)

+ 1 - 1
name_extractor/admin.py

@@ -3,7 +3,7 @@ from .models import Staff
 
 
 class StaffAdmin(admin.ModelAdmin):
-    list_display = ('name', 'department', 'location', 'name_mentioned', 'total_units')
+    list_display = ('name', 'department', 'location', 'name_mentioned_google', 'name_mentioned_facebook', 'name_mentioned_yelp', 'total_units')
     list_filter = ('location', 'department',)
     ordering = ['-total_units']
     search_fields = ['department', 'name']

+ 28 - 0
name_extractor/migrations/0006_auto_20201019_0725.py

@@ -0,0 +1,28 @@
+# Generated by Django 3.0.4 on 2020-10-19 07:25
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('name_extractor', '0005_auto_20200219_1157'),
+    ]
+
+    operations = [
+        migrations.RenameField(
+            model_name='staff',
+            old_name='name_mentioned',
+            new_name='name_mentioned_facebook',
+        ),
+        migrations.AddField(
+            model_name='staff',
+            name='name_mentioned_google',
+            field=models.IntegerField(default=0),
+        ),
+        migrations.AddField(
+            model_name='staff',
+            name='name_mentioned_yelp',
+            field=models.IntegerField(default=0),
+        ),
+    ]

+ 3 - 1
name_extractor/models.py

@@ -6,7 +6,9 @@ from gauth.models import Location
 class Staff(models.Model):
     name = models.CharField(max_length=255)
     total_units = models.DecimalField(default=0.0, max_digits=5, decimal_places=2)
-    name_mentioned = models.IntegerField(default=0)
+    name_mentioned_google = models.IntegerField(default=0)
+    name_mentioned_facebook = models.IntegerField(default=0)
+    name_mentioned_yelp = models.IntegerField(default=0)
     location = models.ForeignKey(Location, on_delete=models.CASCADE)
     department = models.CharField(max_length=255, default='staff')
     nick_names = models.TextField(null=True, blank=True)

+ 49 - 13
name_extractor/utils.py

@@ -3,6 +3,10 @@ from decimal import Decimal
 from django.conf import settings
 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')
 
 
@@ -12,9 +16,9 @@ def get_all_names(text):
     return names
 
 
-def all_staffs(location):
+def all_staffs(location_id):
     names = []
-    staffs = Staff.objects.filter(location=location)
+    staffs = Staff.objects.filter(location_id=location_id)
     for s in staffs:
         names.extend(s.get_nick_names)
     return set(names)
@@ -30,25 +34,31 @@ def clean_text(text):
     return text
 
 
-def extract_names(review):
-    text = clean_text(review.comment)
+def extract_names(text, location_id):
     names = get_all_names(text)
     cleaned_names = []
     for name in names:
         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
 
 
-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
 
     for name in names:
         for staff in staffs:
             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.save()
 
@@ -57,10 +67,36 @@ def make_all_staffs_point_zero():
     staffs = Staff.objects.all()
     for s in staffs:
         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()
 
 
-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')

+ 4 - 2
user/templates/staff_list.html

@@ -3,7 +3,7 @@
 {% block content %}
 
 <!--adding a new staff member.-->
-<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button>
+<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Add a new staff</button>
   <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
     <div class="modal-dialog modal-lg">
       <div class="modal-content">
@@ -32,7 +32,9 @@
           <span style="float: right;">{{ staff.department }}</span>
         </div>
         <div class="card-body">
-          <span >Name Mentioned: {{ staff.name_mentioned }}</span>
+          <span >Name Mentioned: <i class="fa fa-google" aria-hidden="true" style="color: #ff7700; margin-left: 1rem;"></i> {{ staff.name_mentioned_google }}</span>
+          <span > <i class="fa fa-facebook" aria-hidden="true" style="color: blue; margin-left: 2rem;"></i> {{ staff.name_mentioned_facebook }} </span>
+          <span ><i class="fa fa-yelp" aria-hidden="true" style="color: red; margin-left: 2rem;"></i> {{ staff.name_mentioned_yelp }}</span>
           <span style="float: right;">Bonus: ${{ staff.total_units }}</span>
         </div>
       </div>