Skip to content

Commit 3554bde

Browse files
committed
Merge branch 'master' of github.com:watson-developer-cloud/python-sdk
2 parents 5b50a1a + 87086c1 commit 3554bde

2 files changed

Lines changed: 163 additions & 0 deletions

File tree

test/test_speech_to_text_v1.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,76 @@ def test_custom_corpora():
141141
corpus_name='corpus')
142142

143143
assert len(responses.calls) == 4
144+
145+
@responses.activate
146+
def test_custom_words():
147+
words_url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/{0}/words'
148+
word_url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/{0}/words/{1}'
149+
150+
responses.add(responses.POST, word_url.format('custid','IEEE'),
151+
body='{"get response": "yep"}',
152+
status=200,
153+
content_type='application/json')
154+
155+
responses.add(responses.DELETE, word_url.format('custid', 'IEEE'),
156+
body='{"get response": "yep"}',
157+
status=200,
158+
content_type='application/json')
159+
160+
responses.add(responses.GET, word_url.format('custid', 'IEEE'),
161+
body='{"get response": "yep"}',
162+
status=200,
163+
content_type='application/json')
164+
165+
responses.add(responses.POST, words_url.format('custid'),
166+
body='{"get response": "yep"}',
167+
status=200,
168+
content_type='application/json')
169+
170+
responses.add(responses.GET, words_url.format('custid'),
171+
body='{"get response": "yep"}',
172+
status=200,
173+
content_type='application/json')
174+
175+
speech_to_text = watson_developer_cloud.SpeechToTextV1(
176+
username="username", password="password")
177+
178+
custom_word = speech_to_text.CustomWord(word="IEEE",
179+
sounds_like=["i triple e"],
180+
display_as="IEEE")
181+
182+
183+
speech_to_text.add_custom_word(customization_id='custid',
184+
custom_word=custom_word)
185+
186+
speech_to_text.delete_custom_word(customization_id='custid',
187+
custom_word=custom_word)
188+
189+
speech_to_text.delete_custom_word(customization_id='custid',
190+
custom_word='IEEE')
191+
192+
custom_words = [custom_word, custom_word, custom_word]
193+
speech_to_text.add_custom_words(customization_id='custid',
194+
custom_words=custom_words)
195+
196+
speech_to_text.get_custom_word(customization_id='custid',
197+
custom_word="IEEE")
198+
199+
speech_to_text.get_custom_word(customization_id='custid',
200+
custom_word=custom_word)
201+
202+
speech_to_text.list_custom_words(customization_id='custid')
203+
speech_to_text.list_custom_words(customization_id='custid', sort='alphabetical')
204+
with pytest.raises(KeyError) as keyerror:
205+
speech_to_text.list_custom_words(customization_id='custid', sort='badsort')
206+
assert 'sort must be alphabetical or count' in str(keyerror.value)
207+
208+
speech_to_text.list_custom_words(customization_id='custid', word_type='all')
209+
210+
with pytest.raises(KeyError) as keyerror:
211+
speech_to_text.list_custom_words(customization_id='custid', word_type='badwordtype')
212+
assert 'word type must be all, user, or corpora' in str(keyerror.value)
213+
214+
assert len(responses.calls) == 9
215+
216+

watson_developer_cloud/speech_to_text_v1.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .watson_developer_cloud_service import WatsonDeveloperCloudService
2020
import json
2121

22+
2223
class SpeechToTextV1(WatsonDeveloperCloudService):
2324
default_url = "https://stream.watsonplatform.net/speech-to-text/api"
2425

@@ -136,3 +137,92 @@ def delete_corpus(self, customization_id, corpus_name):
136137
url=url.format(customization_id,
137138
corpus_name),
138139
accept_json=True)
140+
141+
class CustomWord(object):
142+
def __init__(self, word=None, sounds_like=None, display_as=None):
143+
self._word = word
144+
self._sounds_like = sounds_like
145+
self._display_as = display_as
146+
147+
@property
148+
def word(self):
149+
return self._word
150+
151+
@property
152+
def sounds_like(self):
153+
return self._sounds_like
154+
155+
@property
156+
def display_as(self):
157+
return self._display_as
158+
159+
def __dict__(self):
160+
return {'word': self.word,
161+
'sounds_like': self.sounds_like,
162+
'display_as': self.display_as}
163+
164+
def add_custom_words(self, customization_id, custom_words):
165+
url = '/v1/customizations/{0}/words'
166+
payload = {'words': [x.__dict__() for x in custom_words]}
167+
return self.request(method='POST',
168+
url=url.format(customization_id),
169+
data=json.dumps(payload),
170+
headers={'content-type': 'application/json'},
171+
accept_json=True)
172+
173+
def add_custom_word(self, customization_id, custom_word):
174+
url = '/v1/customizations/{0}/words/{1}'
175+
176+
custom_word_fragment = {'sounds_like': custom_word.sounds_like,
177+
'display_as': custom_word.display_as}
178+
return self.request(method='POST',
179+
url=url.format(customization_id,
180+
custom_word.word),
181+
data=json.dumps(custom_word_fragment),
182+
headers={'content-type': 'application/json'},
183+
accept_json=True)
184+
185+
def list_custom_words(self, customization_id, word_type=None, sort=None):
186+
url = '/v1/customizations/{0}/words'
187+
qs = {}
188+
189+
if word_type:
190+
if word_type in ['all', 'user', 'corpora']:
191+
qs['word_type'] = word_type
192+
else:
193+
raise KeyError('word type must be all, user, or corpora')
194+
195+
if sort:
196+
if sort in ['alphabetical', 'count']:
197+
qs['sort'] = sort
198+
else:
199+
raise KeyError('sort must be alphabetical or count')
200+
201+
return self.request(method='GET',
202+
url=url.format(customization_id),
203+
params=qs,
204+
accept_json=True)
205+
206+
def get_custom_word(self, customization_id, custom_word):
207+
url = '/v1/customizations/{0}/words/{1}'
208+
word = None
209+
if isinstance(custom_word, str):
210+
word = custom_word
211+
else:
212+
word = custom_word.word
213+
214+
return self.request(method='GET',
215+
url=url.format(customization_id, word),
216+
accept_json=True)
217+
218+
def delete_custom_word(self, customization_id, custom_word):
219+
url = '/v1/customizations/{0}/words/{1}'
220+
word = None
221+
if isinstance(custom_word, str):
222+
word = custom_word
223+
else:
224+
word = custom_word.word
225+
226+
return self.request(method='DELETE',
227+
url=url.format(customization_id, word),
228+
accept_json=True)

0 commit comments

Comments
 (0)