Skip to content

Commit

Permalink
feat(Speech to Text): New methods for custom grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdsouza committed Jan 16, 2019
1 parent 2d1257d commit 07b70c2
Show file tree
Hide file tree
Showing 3 changed files with 465 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/integration/test_speech_to_text_v1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from unittest import TestCase
import os
from watson_developer_cloud.websocket import RecognizeCallback, AudioSource
Expand Down Expand Up @@ -107,3 +108,51 @@ def on_transcription(self, transcript):
assert test_callback.error is None
assert test_callback.transcript is not None
assert test_callback.transcript[0]['transcript'] == 'thunderstorms could produce large hail isolated tornadoes and heavy rain '

def test_custom_grammars(self):
customization_id = None
for custom_model in self.custom_models['customizations']:
if custom_model['name'] == 'integration_test_model_for_grammar':
customization_id = custom_model['customization_id']
break

if customization_id is None:
print('Creating a new custom model')
create_custom_model_for_grammar = self.speech_to_text.create_language_model(
name="integration_test_model_for_grammar",
base_model_name="en-US_BroadbandModel"
).get_result()
customization_id = create_custom_model_for_grammar['customization_id']

grammars = self.speech_to_text.list_grammars(
customization_id
).get_result()['grammars']

if not grammars:
with open(os.path.join(os.path.dirname(__file__), '../../resources/confirm-grammar.xml'), 'rb') as grammar_file:
add_grammar_result = self.speech_to_text.add_grammar(
customization_id,
grammar_name='test-add-grammar-python',
grammar_file=grammar_file,
content_type='application/srgs+xml',
allow_overwrite=True
).get_result()
assert add_grammar_result is not None

get_grammar_result = self.speech_to_text.get_grammar(
customization_id,
grammar_name='test-add-grammar-python'
).get_result()
assert get_grammar_result is not None
else:
print('Deleting grammar')
delete_grammar_result = self.speech_to_text.delete_grammar(
customization_id,
'test-add-grammar-python'
).get_result()
assert delete_grammar_result is not None

try:
self.speech_to_text.delete_language_model(customization_id)
except watson_developer_cloud.WatsonApiException as ex:
print('Could not delete model: {0}'.format(ex.message))
52 changes: 52 additions & 0 deletions test/unit/test_speech_to_text_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,55 @@ def test_delete_user_data():
response = speech_to_text.delete_user_data('id').get_result()
assert response is None
assert len(responses.calls) == 1

@responses.activate
def test_custom_grammars():
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/{0}/grammars/{1}'

responses.add(
responses.POST,
url.format('customization_id', 'grammar_name'),
body='{}',
status=200,
content_type='application/json')

responses.add(
responses.DELETE,
url.format('customization_id', 'grammar_name'),
status=200,
content_type='application/json')

responses.add(
responses.GET,
url.format('customization_id', 'grammar_name'),
body='{"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}',
status=200,
content_type='application/json')

responses.add(
responses.GET,
url='https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customization_id/grammars',
body='{"grammars":[{"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}]}',
status=200,
content_type='application/json')

speech_to_text = watson_developer_cloud.SpeechToTextV1(
username="username", password="password")

with open(os.path.join(os.path.dirname(__file__), '../../resources/confirm-grammar.xml'), 'rb') as grammar_file:
speech_to_text.add_grammar(
"customization_id",
grammar_name='grammar_name',
grammar_file=grammar_file,
content_type='application/srgs+xml',
allow_overwrite=True)
assert responses.calls[0].response.json() == {}

speech_to_text.delete_grammar('customization_id', 'grammar_name')
assert responses.calls[1].response.status_code == 200

speech_to_text.get_grammar('customization_id', 'grammar_name')
assert responses.calls[2].response.json() == {"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}

speech_to_text.list_grammars('customization_id')
assert responses.calls[3].response.json() == {"grammars":[{"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}]}
Loading

0 comments on commit 07b70c2

Please sign in to comment.