Skip to content

Commit 07b70c2

Browse files
committed
feat(Speech to Text): New methods for custom grammars
1 parent 2d1257d commit 07b70c2

File tree

3 files changed

+465
-0
lines changed

3 files changed

+465
-0
lines changed

test/integration/test_speech_to_text_v1.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from unittest import TestCase
23
import os
34
from watson_developer_cloud.websocket import RecognizeCallback, AudioSource
@@ -107,3 +108,51 @@ def on_transcription(self, transcript):
107108
assert test_callback.error is None
108109
assert test_callback.transcript is not None
109110
assert test_callback.transcript[0]['transcript'] == 'thunderstorms could produce large hail isolated tornadoes and heavy rain '
111+
112+
def test_custom_grammars(self):
113+
customization_id = None
114+
for custom_model in self.custom_models['customizations']:
115+
if custom_model['name'] == 'integration_test_model_for_grammar':
116+
customization_id = custom_model['customization_id']
117+
break
118+
119+
if customization_id is None:
120+
print('Creating a new custom model')
121+
create_custom_model_for_grammar = self.speech_to_text.create_language_model(
122+
name="integration_test_model_for_grammar",
123+
base_model_name="en-US_BroadbandModel"
124+
).get_result()
125+
customization_id = create_custom_model_for_grammar['customization_id']
126+
127+
grammars = self.speech_to_text.list_grammars(
128+
customization_id
129+
).get_result()['grammars']
130+
131+
if not grammars:
132+
with open(os.path.join(os.path.dirname(__file__), '../../resources/confirm-grammar.xml'), 'rb') as grammar_file:
133+
add_grammar_result = self.speech_to_text.add_grammar(
134+
customization_id,
135+
grammar_name='test-add-grammar-python',
136+
grammar_file=grammar_file,
137+
content_type='application/srgs+xml',
138+
allow_overwrite=True
139+
).get_result()
140+
assert add_grammar_result is not None
141+
142+
get_grammar_result = self.speech_to_text.get_grammar(
143+
customization_id,
144+
grammar_name='test-add-grammar-python'
145+
).get_result()
146+
assert get_grammar_result is not None
147+
else:
148+
print('Deleting grammar')
149+
delete_grammar_result = self.speech_to_text.delete_grammar(
150+
customization_id,
151+
'test-add-grammar-python'
152+
).get_result()
153+
assert delete_grammar_result is not None
154+
155+
try:
156+
self.speech_to_text.delete_language_model(customization_id)
157+
except watson_developer_cloud.WatsonApiException as ex:
158+
print('Could not delete model: {0}'.format(ex.message))

test/unit/test_speech_to_text_v1.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,55 @@ def test_delete_user_data():
513513
response = speech_to_text.delete_user_data('id').get_result()
514514
assert response is None
515515
assert len(responses.calls) == 1
516+
517+
@responses.activate
518+
def test_custom_grammars():
519+
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/{0}/grammars/{1}'
520+
521+
responses.add(
522+
responses.POST,
523+
url.format('customization_id', 'grammar_name'),
524+
body='{}',
525+
status=200,
526+
content_type='application/json')
527+
528+
responses.add(
529+
responses.DELETE,
530+
url.format('customization_id', 'grammar_name'),
531+
status=200,
532+
content_type='application/json')
533+
534+
responses.add(
535+
responses.GET,
536+
url.format('customization_id', 'grammar_name'),
537+
body='{"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}',
538+
status=200,
539+
content_type='application/json')
540+
541+
responses.add(
542+
responses.GET,
543+
url='https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customization_id/grammars',
544+
body='{"grammars":[{"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}]}',
545+
status=200,
546+
content_type='application/json')
547+
548+
speech_to_text = watson_developer_cloud.SpeechToTextV1(
549+
username="username", password="password")
550+
551+
with open(os.path.join(os.path.dirname(__file__), '../../resources/confirm-grammar.xml'), 'rb') as grammar_file:
552+
speech_to_text.add_grammar(
553+
"customization_id",
554+
grammar_name='grammar_name',
555+
grammar_file=grammar_file,
556+
content_type='application/srgs+xml',
557+
allow_overwrite=True)
558+
assert responses.calls[0].response.json() == {}
559+
560+
speech_to_text.delete_grammar('customization_id', 'grammar_name')
561+
assert responses.calls[1].response.status_code == 200
562+
563+
speech_to_text.get_grammar('customization_id', 'grammar_name')
564+
assert responses.calls[2].response.json() == {"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}
565+
566+
speech_to_text.list_grammars('customization_id')
567+
assert responses.calls[3].response.json() == {"grammars":[{"status": "analyzed", "name": "test-add-grammar-python", "out_of_vocabulary_words": 0}]}

0 commit comments

Comments
 (0)