Skip to content

Commit

Permalink
Merge branch 'develop' into issue/60
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Paz committed Sep 5, 2018
2 parents 9c0a78b + b531945 commit 2307ac1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
21 changes: 21 additions & 0 deletions bothub_nlp/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from tornado.web import Application, url

from bothub.common import languages

from .. import settings
from .handlers.parse import ParseHandler
from .handlers.train import TrainHandler
Expand All @@ -13,6 +15,25 @@

logger = logging.getLogger('bothub_nlp.server')

NEXT_LANGS = {
'english': [
languages.LANGUAGE_EN,
],
'portuguese': [
languages.LANGUAGE_PT,
languages.LANGUAGE_PT_BR,
],
languages.LANGUAGE_PT: [
languages.LANGUAGE_PT_BR,
],
'pt-br': [
languages.LANGUAGE_PT_BR,
],
'br': [
languages.LANGUAGE_PT_BR,
],
}


def make_app():
return Application([
Expand Down
32 changes: 25 additions & 7 deletions bothub_nlp/server/handlers/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ def get(self):
self.set_header('Content-Type', 'text/plain')
self.finish('OK')

repository_authorization = self.repository_authorization()
if not repository_authorization:
raise AuthorizationIsRequired()

self._parse(text, language, rasa_format)

@asynchronous
Expand All @@ -41,16 +37,38 @@ def post(self):
self._parse(text, language, rasa_format)

def _parse(self, text, language, rasa_format=False):
print(text, language, rasa_format)
if language and language not in settings.SUPPORTED_LANGUAGES.keys():
from .. import logger
from .. import NEXT_LANGS

if language and (
language not in settings.SUPPORTED_LANGUAGES.keys() and
language not in NEXT_LANGS.keys()
):
raise ValidationError(
'Language \'{}\' not supported by now.'.format(language),
field='language')

repository_authorization = self.repository_authorization()
if not repository_authorization:
raise AuthorizationIsRequired()

logger.info(
'parse request',
repository_authorization,
text,
language,
rasa_format)

repository = repository_authorization.repository
update = repository.last_trained_update(language)

if not update:
next_languages = NEXT_LANGS.get(language, [])
for next_language in next_languages:
update = repository.last_trained_update(next_language)
if update:
break

if not update:
raise ValidationError(
'This repository has never been trained',
Expand All @@ -60,7 +78,7 @@ def _parse(self, text, language, rasa_format=False):
answer.update({
'text': text,
'update_id': update.id,
'language': language,
'language': update.language,
})

self.finish(answer)
33 changes: 33 additions & 0 deletions bothub_nlp/server/handlers/tests/test_parse_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,39 @@ def test_valid_request(self):
'update_id',
content_data.keys())

def test_valid_request_with_next_lang(self):
fill_examples(EXAMPLES_MOCKUP, self.repository)
train_update(self.repository.current_update(), self.user)

text = 'hi, my name is Douglas'

response = self.fetch(
'/parse/',
method='POST',
body=json.dumps({
'text': text,
'language': 'english'
}),
headers={
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(
self.authorization.uuid),
},
)

self.assertEqual(
response.code,
status.HTTP_200_OK)

content_data = json.loads(response.body)

self.assertIn(
'language',
content_data.keys())
self.assertEqual(
content_data.get('language'),
languages.LANGUAGE_EN)

def test_valid_request_method_get(self):
fill_examples(EXAMPLES_MOCKUP, self.repository)
train_update(self.repository.current_update(), self.user)
Expand Down

0 comments on commit 2307ac1

Please sign in to comment.