Kaynağa Gözat

Added new spacy model and change dependent functions, And show staff names in frontend

Mohidul Islam 4 yıl önce
ebeveyn
işleme
f020986488

+ 3 - 5
name_extractor/utils.py

@@ -2,15 +2,13 @@ import re
 from decimal import Decimal
 from django.conf import settings
 from .models import Staff
-from nlu_job.nlu_utils import model_inference
 
-nlu_url = settings.NLU_SERVER_URI
+ner_model = getattr(settings, 'SPACY_NER_MODEL')
 
 
 def get_all_names(text):
-    res = model_inference(text)
-    entities = res.get('entities')
-    names = {ent.get('value') for ent in entities if ent.get('entity') in ['PERSON', 'ORG']}
+    doc = ner_model(text)
+    names = {ent.text for ent in doc.ents if ent.label_ in ['PERSON', 'ORG', 'PRODUCT']}
     return names
 
 

+ 7 - 10
nlu_job/nlu_utils.py

@@ -6,7 +6,8 @@ import json
 from difflib import SequenceMatcher
 from review.models import Review
 
-nlu_server_url = settings.NLU_SERVER_URI
+nlu_server_url = getattr(settings, 'NLU_SERVER_URI')
+ner_model = getattr(settings, 'SPACY_NER_MODEL')
 
 
 def filter_with_last_ten_reviews(location_id, replies):
@@ -61,15 +62,11 @@ def is_a_name(name):
     :return -> a boolean True/False:
     '''
 
-    response = model_inference(name.title())
-    entities = response.get('entities')
-    if not entities:
-        return False
-    entity = entities[0]
-    if entity.get('entity') == 'PERSON':
-        return True
-    else:
-        return False
+    doc = ner_model(name)
+    for ent in doc.ents:
+        if ent.label_ in ['PERSON']:
+            return True
+    return False
 
 
 # This function will not use anymore

+ 13 - 0
requirements.txt

@@ -1,8 +1,11 @@
 asgiref==3.2.3
 beautifulsoup4==4.9.1
+blis==0.4.1
 cachetools==4.0.0
+catalogue==1.0.0
 certifi==2019.11.28
 chardet==3.0.4
+cymem==2.0.3
 Django==3.0.4
 django-crispy-forms==1.9.0
 django-crontab==0.7.1
@@ -11,10 +14,14 @@ facebook-sdk==3.1.0
 google-auth==1.11.2
 google-auth-oauthlib==0.4.1
 idna==2.9
+importlib-metadata==2.0.0
+murmurhash==1.0.2
 mysqlclient==1.4.6
 nameparser==1.0.6
 numpy==1.19.1
 oauthlib==3.1.0
+plac==1.1.3
+preshed==3.0.2
 pyasn1==0.4.8
 pyasn1-modules==0.2.8
 pytz==2019.3
@@ -24,5 +31,11 @@ rsa==4.0
 selenium==3.141.0
 six==1.14.0
 soupsieve==2.0.1
+spacy==2.3.2
 sqlparse==0.3.1
+srsly==1.0.2
+thinc==7.4.1
+tqdm==4.50.0
 urllib3==1.25.8
+wasabi==0.8.0
+zipp==3.3.0

+ 6 - 0
review_automation/settings/dev.py

@@ -1,4 +1,10 @@
+import spacy
 from .base import *
+try:
+    SPACY_NER_MODEL = spacy.load('en_core_web_sm')
+    print('\033[92mSpacy model small size has loaded!\033[0m')
+except OSError:
+    print('\033[93mNo spacy model has found. Please install a small sized spacy model.\033[0m')
 
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = True

+ 7 - 0
review_automation/settings/prod.py

@@ -1,5 +1,12 @@
+import spacy
 from .base import *
 
+try:
+    SPACY_NER_MODEL = spacy.load('en_core_web_lg')
+    print('\033[93mSpacy model large size has loaded!\033[0m')
+except OSError:
+    print('\033[93mNo spacy model has found. Please install a large sized spacy model.\033[0m')
+
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = False
 

+ 19 - 0
user/templates/staff_list.html

@@ -0,0 +1,19 @@
+{% extends 'user-base.html' %}
+{% block content %}
+  <div class="row">
+    {% for staff in staffs %}
+    <div class="col-sm-6">
+      <div class="card mt-2 mb-2">
+        <div class="card-header">
+          <span style="font-size: larger; font-family: sans-serif; font-weight: bold;">{{staff.name}}</span>
+          <span style="float: right;">{{ staff.department }}</span>
+        </div>
+        <div class="card-body">
+          <span >Name Mentioned: {{ staff.name_mentioned }}</span>
+          <span style="float: right;">Bonus: ${{ staff.total_units }}</span>
+        </div>
+      </div>
+    </div>
+    {% endfor %}
+  </div>
+{% endblock %}

+ 2 - 0
user/urls.py

@@ -7,6 +7,7 @@ from .views import (
     ReviewListLocationWise,
     ReviewAnalyticsGraph,
     ChartDataAllPlatform,
+    StaffLeaderBoard,
 )
 
 urlpatterns = [
@@ -18,4 +19,5 @@ urlpatterns = [
     path('api/analytics', ChartDataThisMonth.as_view(), name='location-api-analytics'),
     path('api/analytics/all-platform', ChartDataAllPlatform.as_view(), name='location-api-analytics-all-platform'),
     path('graph', ReviewAnalyticsGraph.as_view(), name='location-analytics-graph'),
+    path('staff', StaffLeaderBoard.as_view(), name='staff-leaderboard'),
 ]

+ 10 - 0
user/views.py

@@ -8,6 +8,7 @@ from gauth.models import Location
 from review.models import Review
 from facebook_app.models import FacebookReview
 from yelp.models import YelpReview
+from name_extractor.models import Staff
 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 
 from django.contrib.auth.mixins import LoginRequiredMixin
@@ -126,3 +127,12 @@ class ReviewAnalyticsGraph(View):
     def get(self, requests, *args, **kwargs):
         return render(requests, 'location-wise-reviews.html')
 
+
+class StaffLeaderBoard(View):
+
+    def get(self, requests, *args, **kwargs):
+        staffs = Staff.objects.filter(location=requests.user.useraccount.location).order_by('-total_units')
+        context = {
+            'staffs': staffs
+        }
+        return render(requests, 'staff_list.html', context)