utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import re
  2. from django.conf import settings
  3. from .models import Staff
  4. nlp = settings.MODEL
  5. STOP_WORDS = ['signature', 'care', 'emergency', 'er', 'center', 'nurse', 'dr', 'dr.', 'signaturecare', 'tech',
  6. 'doc', 'urgent', 'the', 'nures', 'nurses', 'registration']
  7. def clean_text(text):
  8. # replace some letter in text for getting better performance
  9. text = re.sub(r':\s*', ' ', text)
  10. text = re.sub(r'&', ', ', text)
  11. text = re.sub(r'/', ', ', text)
  12. text = re.sub(r'\.*\n\.*', '.', text)
  13. text = re.sub(r'[dD][rR](\.|\s*)*', 'Dr. ', text)
  14. return text
  15. def cleaning_name(names):
  16. cleaned_names = []
  17. for name in names:
  18. for n in name.split():
  19. cleaned_names.append(n) if len(n) > 2 and n.lower() not in STOP_WORDS else None
  20. return cleaned_names
  21. def extract_names(text):
  22. text = clean_text(text)
  23. doc = nlp(text)
  24. names = {ent.text for ent in doc.ents if ent.label_ in ['PERSON', 'ORG']}
  25. names = list(names)
  26. cleaned_names = cleaning_name(names)
  27. return cleaned_names
  28. def add_point_to_staff_profile(review):
  29. staffs = Staff.objects.all()
  30. names = extract_names(review.comment)
  31. point_unit = 1/len(names)
  32. # TODO: store points to the staff profile