From 7274a3223399ff46d0f11c07127b581c54a1dcf3 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 6 Jan 2015 15:47:34 +0100 Subject: [PATCH] Add similar competence finding to Competence --- booking/models.py | 16 ++++++++++++++++ booking/tests.py | 35 +++++++++++++++++++++++++++++++++++ duckbook/settings.py | 1 + requirements.txt | 1 + 4 files changed, 53 insertions(+) diff --git a/booking/models.py b/booking/models.py index f03e5e7..d17d29c 100644 --- a/booking/models.py +++ b/booking/models.py @@ -3,6 +3,8 @@ from django.contrib.auth.models import User from django.utils import timezone from django.conf import settings +from fuzzywuzzy import fuzz + from .ducklevel import minutes_to_level class Species(models.Model): @@ -31,6 +33,20 @@ class Competence(models.Model): def __str__(self): return self.name + @staticmethod + def get_similar_comps(name): + comps = __class__.objects.values_list('name', flat = True) + ret = () + + for c in comps: + r = fuzz.ratio(name.lower(), c.lower()) + + # This ratio is subject to change + if r > settings.MIN_FUZZY_SIMILARITY: + ret = ret + (c,) + + return ret + class Duck(models.Model): """Model to hold Duck data""" diff --git a/booking/tests.py b/booking/tests.py index cdbaab2..eff4f09 100644 --- a/booking/tests.py +++ b/booking/tests.py @@ -13,6 +13,10 @@ class FrontTest(TestCase): def setUp(self): self.client = Client() + def test_index_page(self): + response = self.client.get('/') + self.assertEqual(response.status_code, 200) + def test_vocabulary_page(self): response = self.client.get('/vocabulary.html') self.assertEqual(response.status_code, 200) @@ -214,3 +218,34 @@ class TestListing(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['duck_list']), 1) + +class SimilarCompTest(TestCase): + def setUp(self): + admin = User(username = 'admin') + admin.save() + + competence_list = ( + 'Creativity', + 'Administration', + 'Perl', + 'Python', + 'TCSH', + ) + + for c in competence_list: + comp = Competence(name = c, added_by = admin) + comp.save() + + def test_good_similar_competences(self): + l = Competence.get_similar_comps('perl') + self.assertEquals(len(l), 1) + + l = Competence.get_similar_comps('pzthon') + self.assertEquals(len(l), 1) + + l = Competence.get_similar_comps('kreativitás') + self.assertEqual(len(l), 1) + + def test_bad_similar_competence(self): + l = Competence.get_similar_comps('development') + self.assertEqual(len(l), 0) diff --git a/duckbook/settings.py b/duckbook/settings.py index 7216f42..cad2774 100644 --- a/duckbook/settings.py +++ b/duckbook/settings.py @@ -87,3 +87,4 @@ USE_TZ = True STATIC_URL = '/static/' MAX_DUCK_LEVEL = 5 +MIN_FUZZY_SIMILARITY = 75 diff --git a/requirements.txt b/requirements.txt index 9c0dddd..e91e9ab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Django>=1.7,<1.8 WebTest==2.0.17 django-webtest==1.7.7 django-js-reverse==0.3.3 +fuzzywuzzy==0.4.0