Skip to content

Commit f4a218e

Browse files
author
Joshua B. Smith
committed
adds ability to add custom corpora
1 parent 93c2fae commit f4a218e

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

test/test_speech_to_text_v1.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding=utf-8
2-
import os,json
2+
import os,json,pytest
33
import responses
44
import watson_developer_cloud
55

@@ -98,3 +98,45 @@ def test_custom_model():
9898

9999
assert len(responses.calls) == 5
100100

101+
@responses.activate
102+
def test_custom_corpora():
103+
104+
corpora_url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/{0}/corpora'
105+
get_corpora_url = '{0}/{1}'.format(corpora_url.format('customid'),'corpus')
106+
107+
responses.add(responses.GET, corpora_url.format('customid'),
108+
body='{"get response": "yep"}', status=200,
109+
content_type='application/json')
110+
111+
responses.add(responses.POST, get_corpora_url,
112+
body='{"get response": "yep"}',
113+
status=200,
114+
content_type='application/json')
115+
116+
responses.add(responses.GET, get_corpora_url,
117+
body='{"get response": "yep"}',
118+
status=200,
119+
content_type='application/json')
120+
121+
responses.add(responses.DELETE, get_corpora_url,
122+
body='{"get response": "yep"}',
123+
status=200,
124+
content_type='application/json')
125+
126+
speech_to_text = watson_developer_cloud.SpeechToTextV1(
127+
username="username", password="password")
128+
129+
speech_to_text.list_corpora(customization_id='customid')
130+
131+
file_path = './resources/speech_to_text/corpus-short-1.txt'
132+
with open(file_path) as f:
133+
speech_to_text.add_corpora(customization_id='customid',
134+
corpus_name="corpus",file=f)
135+
136+
speech_to_text.get_corpus(customization_id='customid',
137+
corpus_name='corpus')
138+
139+
speech_to_text.delete_corpus(customization_id='customid',
140+
corpus_name='corpus')
141+
142+
assert len(responses.calls) == 4

watson_developer_cloud/speech_to_text_v1.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from .watson_developer_cloud_service import WatsonDeveloperCloudService
2020
import json
21+
from collections import namedtuple
2122

2223

2324
class SpeechToTextV1(WatsonDeveloperCloudService):
@@ -96,3 +97,44 @@ def delete_custom_model(self, modelid):
9697
return self.request(method='DELETE',
9798
url='/v1/customizations/{0}'.format(modelid),
9899
accept_json=True)
100+
101+
def list_corpora(self, customization_id):
102+
url = '/v1/customizations/{0}/corpora'
103+
return self.request(method='GET',
104+
url=url.format(customization_id),
105+
accept_json=True)
106+
107+
def add_corpora(self,
108+
customization_id,
109+
corpus_name,
110+
file,
111+
allow_overwrite=None):
112+
113+
url = '/v1/customizations/{0}/corpora/{1}'
114+
115+
if allow_overwrite is None:
116+
allow_overwrite = False
117+
118+
headers = {'Content-Type': 'application/octet-stream'}
119+
120+
return self.request(method='GET',
121+
url=url.format(customization_id,
122+
corpus_name),
123+
headers=headers,
124+
data=file,
125+
params={'allow_overwrite': allow_overwrite},
126+
accept_json=True)
127+
128+
def get_corpus(self, customization_id, corpus_name):
129+
url = '/v1/customizations/{0}/corpora/{1}'
130+
return self.request(method='GET',
131+
url=url.format(customization_id,
132+
corpus_name),
133+
accept_json=True)
134+
135+
def delete_corpus(self, customization_id, corpus_name):
136+
url = '/v1/customizations/{0}/corpora/{1}'
137+
return self.request(method='DELETE',
138+
url=url.format(customization_id,
139+
corpus_name),
140+
accept_json=True)

0 commit comments

Comments
 (0)