|
19 | 19 | from .watson_developer_cloud_service import WatsonDeveloperCloudService |
20 | 20 | import json |
21 | 21 |
|
| 22 | + |
22 | 23 | class SpeechToTextV1(WatsonDeveloperCloudService): |
23 | 24 | default_url = "https://stream.watsonplatform.net/speech-to-text/api" |
24 | 25 |
|
@@ -136,3 +137,92 @@ def delete_corpus(self, customization_id, corpus_name): |
136 | 137 | url=url.format(customization_id, |
137 | 138 | corpus_name), |
138 | 139 | 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