Преглед изворни кода

Change staff model and add some function to extract names from review

Mohidul Islam пре 5 година
родитељ
комит
82eb4146f3

+ 2 - 2
name_extractor/admin.py

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

+ 18 - 0
name_extractor/migrations/0004_staff_name_mentioned.py

@@ -0,0 +1,18 @@
+# Generated by Django 3.0 on 2020-02-19 10:49
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('name_extractor', '0003_staff_department'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='staff',
+            name='name_mentioned',
+            field=models.IntegerField(default=0),
+        ),
+    ]

+ 18 - 0
name_extractor/migrations/0005_auto_20200219_1157.py

@@ -0,0 +1,18 @@
+# Generated by Django 3.0 on 2020-02-19 11:57
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('name_extractor', '0004_staff_name_mentioned'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='staff',
+            name='total_units',
+            field=models.DecimalField(decimal_places=2, default=0.0, max_digits=5),
+        ),
+    ]

+ 2 - 1
name_extractor/models.py

@@ -5,7 +5,8 @@ from gauth.models import Location
 
 class Staff(models.Model):
     name = models.CharField(max_length=255)
-    total_units = models.FloatField(default=0.0)
+    total_units = models.DecimalField(default=0.0, max_digits=5, decimal_places=2)
+    name_mentioned = 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)

+ 47 - 15
name_extractor/utils.py

@@ -1,10 +1,27 @@
 import re
+from decimal import Decimal
 from django.conf import settings
 from .models import Staff
+from nlu_job.nlu_utils import model_inference
 
 nlp = settings.MODEL
-STOP_WORDS = ['signature', 'care', 'emergency', 'er', 'center', 'nurse', 'dr', 'dr.', 'signaturecare', 'tech',
-              'doc', 'urgent', 'the', 'nures', 'nurses', 'registration']
+
+nlu_url = settings.NLU_SERVER_URI
+
+
+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']}
+    return names
+
+
+def all_staffs(location):
+    names = []
+    staffs = Staff.objects.filter(location=location)
+    for s in staffs:
+        names.extend(s.get_nick_names)
+    return set(names)
 
 
 def clean_text(text):
@@ -17,25 +34,40 @@ def clean_text(text):
     return text
 
 
-def cleaning_name(names):
+def extract_names(review):
+    text = clean_text(review.comment)
+    # doc = nlp(text)
+    # names = {ent.text for ent in doc.ents if ent.label_ in ['PERSON', 'ORG']}
+
+    names = get_all_names(text)
     cleaned_names = []
     for name in names:
         for n in name.split():
-            cleaned_names.append(n) if len(n) > 2 and n.lower() not in STOP_WORDS else None
+            cleaned_names.append(n) if n.lower() in all_staffs(review.location) else None
     return cleaned_names
 
 
-def extract_names(text):
-    text = clean_text(text)
-    doc = nlp(text)
-    names = {ent.text for ent in doc.ents if ent.label_ in ['PERSON', 'ORG']}
-    names = list(names)
-    cleaned_names = cleaning_name(names)
-    return cleaned_names
+def add_point_to_staff_profile(review):
+    staffs = Staff.objects.filter(location=review.location)
+    names = extract_names(review)
+    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
+                staff.total_units += point_unit
+                staff.save()
 
-def add_point_to_staff_profile(review):
+
+def make_all_staffs_point_zero():
     staffs = Staff.objects.all()
-    names = extract_names(review.comment)
-    point_unit = 1/len(names)
-    # TODO: store points to the staff profile
+    for s in staffs:
+        s.total_units = 0
+        s.name_mentioned = 0
+        s.save()
+
+
+def extract_names_from_reviews(reviews):
+    for review in reviews:
+        add_point_to_staff_profile(review)

+ 1 - 1
review_automation/settings.py

@@ -75,7 +75,7 @@ DATABASES = {
         'NAME': 'review_automation',
         'USER': 'root',
         'PASSWORD': 'sad2002S1',
-        'HOST': 'localhost',
+        'HOST': '10.0.0.17',
         'PORT': '3306',
     }
 }