Skip to content

Commit

Permalink
Merge 4ad1714 into 7767df5
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Paz authored Oct 4, 2018
2 parents 7767df5 + 4ad1714 commit 02376d7
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 3 deletions.
4 changes: 4 additions & 0 deletions bothub/api/serializers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Meta:
'name',
'slug',
'language',
'use_language_model_featurizer',
'use_competing_intents',
'categories',
'description',
'is_private',
Expand Down Expand Up @@ -65,6 +67,8 @@ class Meta:
'slug',
'language',
'available_languages',
'use_language_model_featurizer',
'use_competing_intents',
'categories',
'categories_list',
'description',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.6 on 2018-10-02 17:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0022_repositoryupdate_training_log'),
]

operations = [
migrations.AddField(
model_name='repository',
name='use_language_model_featurizer',
field=models.BooleanField(default=True, help_text='You can use language featurizer to get words similarity. You need less examples to create a great bot.', verbose_name='Use language model featurizer'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.6 on 2018-10-02 17:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0023_repository_use_language_model_featurizer'),
]

operations = [
migrations.AddField(
model_name='repositoryupdate',
name='use_language_model_featurizer',
field=models.BooleanField(default=True),
),
]
23 changes: 23 additions & 0 deletions bothub/common/migrations/0025_auto_20181003_1911.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.0.6 on 2018-10-03 19:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0024_repositoryupdate_use_language_model_featurizer'),
]

operations = [
migrations.AddField(
model_name='repository',
name='use_competing_intents',
field=models.BooleanField(default=False, help_text='When using competing intents the confidence of the prediction is distributed in all the intents.', verbose_name='Use competing intents'),
),
migrations.AddField(
model_name='repositoryupdate',
name='use_competing_intents',
field=models.BooleanField(default=False),
),
]
31 changes: 30 additions & 1 deletion bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class Meta:
validators=[
languages.validate_language,
])
use_language_model_featurizer = models.BooleanField(
_('Use language model featurizer'),
help_text=_('You can use language featurizer to get words ' +
'similarity. You need less examples to create a great ' +
'bot.'),
default=True)
use_competing_intents = models.BooleanField(
_('Use competing intents'),
help_text=_('When using competing intents the confidence of the ' +
'prediction is distributed in all the intents.'),
default=False)
categories = models.ManyToManyField(
RepositoryCategory,
help_text=CATEGORIES_HELP_TEXT)
Expand Down Expand Up @@ -293,7 +304,9 @@ def current_update(self, language=None):
language = language or self.language
repository_update, created = self.updates.get_or_create(
language=language,
training_started_at=None)
training_started_at=None,
use_language_model_featurizer=self.use_language_model_featurizer,
use_competing_intents=self.use_competing_intents)
return repository_update

def last_trained_update(self, language=None):
Expand Down Expand Up @@ -337,6 +350,8 @@ class Meta:
validators=[
languages.validate_language,
])
use_language_model_featurizer = models.BooleanField(default=True)
use_competing_intents = models.BooleanField(default=False)
created_at = models.DateTimeField(
_('created at'),
auto_now_add=True)
Expand Down Expand Up @@ -434,6 +449,15 @@ def requirements_to_train(self):

@property
def ready_for_train(self):
last_trained_update = self.repository.last_trained_update(
language=self.language)
if last_trained_update:
if last_trained_update.use_language_model_featurizer is not \
self.repository.use_language_model_featurizer:
return True
if last_trained_update.use_competing_intents is not \
self.repository.use_competing_intents:
return True
return len(self.requirements_to_train) is 0

def validate_init_train(self, by=None):
Expand All @@ -450,10 +474,15 @@ def start_training(self, by):
self.validate_init_train(by)
self.by = by
self.training_started_at = timezone.now()
self.use_language_model_featurizer = self.repository \
.use_language_model_featurizer
self.use_competing_intents = self.repository.use_competing_intents
self.save(
update_fields=[
'by',
'training_started_at',
'use_language_model_featurizer',
'use_competing_intents',
])

def save_training(self, bot_data):
Expand Down
90 changes: 90 additions & 0 deletions bothub/common/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,93 @@ def test_set_label_to_none(self):
name_entity.set_label(None)

self.assertIsNone(name_entity.label)


class UseLanguageModelFeaturizerTestCase(TestCase):
def setUp(self):
self.language = languages.LANGUAGE_EN

self.owner = User.objects.create_user('owner@user.com', 'user')

self.repository = Repository.objects.create(
owner=self.owner,
name='Test',
slug='test',
language=self.language,
use_language_model_featurizer=True)

RepositoryExample.objects.create(
repository_update=self.repository.current_update(),
text='my name is Douglas',
intent='greet')
RepositoryExample.objects.create(
repository_update=self.repository.current_update(),
text='my name is John',
intent='greet')

def test_change_ready_for_train(self):
self.assertTrue(self.repository.ready_for_train)
current_update = self.repository.current_update()
current_update.start_training(self.owner)
current_update.save_training(b'')
self.assertFalse(self.repository.ready_for_train)
self.repository.use_language_model_featurizer = False
self.repository.save()
self.assertTrue(self.repository.ready_for_train)
self.repository.use_language_model_featurizer = True
self.repository.save()
self.assertFalse(self.repository.ready_for_train)

def test_equal_repository_value_after_train(self):
current_update = self.repository.current_update()
self.repository.use_language_model_featurizer = False
self.repository.save()
self.assertTrue(current_update.use_language_model_featurizer)
current_update.start_training(self.owner)
current_update.save_training(b'')
self.assertFalse(current_update.use_language_model_featurizer)


class UseCompetingIntentsTestCase(TestCase):
def setUp(self):
self.language = languages.LANGUAGE_EN

self.owner = User.objects.create_user('owner@user.com', 'user')

self.repository = Repository.objects.create(
owner=self.owner,
name='Test',
slug='test',
language=self.language,
use_competing_intents=True)

RepositoryExample.objects.create(
repository_update=self.repository.current_update(),
text='my name is Douglas',
intent='greet')
RepositoryExample.objects.create(
repository_update=self.repository.current_update(),
text='my name is John',
intent='greet')

def test_change_ready_for_train(self):
self.assertTrue(self.repository.ready_for_train)
current_update = self.repository.current_update()
current_update.start_training(self.owner)
current_update.save_training(b'')
self.assertFalse(self.repository.ready_for_train)
self.repository.use_competing_intents = False
self.repository.save()
self.assertTrue(self.repository.ready_for_train)
self.repository.use_competing_intents = True
self.repository.save()
self.assertFalse(self.repository.ready_for_train)

def test_equal_repository_value_after_train(self):
current_update = self.repository.current_update()
self.repository.use_competing_intents = False
self.repository.save()
self.assertTrue(current_update.use_competing_intents)
current_update.start_training(self.owner)
current_update.save_training(b'')
self.assertFalse(current_update.use_competing_intents)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


setup(
name='bothub',
name='bothub-engine',
version='1.16.0',
description='bothub',
description='Bothub Engine',
packages=find_packages(),
install_requires=[
'python-decouple',
Expand Down

0 comments on commit 02376d7

Please sign in to comment.