Skip to content

Commit d78b1b0

Browse files
committed
feat(discovery): CPD-only functionality, Autocomplete method, spellingSuggestions in Query)
1 parent a24ff4e commit d78b1b0

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

ibm_watson/discovery_v1.py

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,7 @@ def query(self,
15111511
similar_document_ids=None,
15121512
similar_fields=None,
15131513
bias=None,
1514+
spelling_suggestions=None,
15141515
x_watson_logging_opt_out=None,
15151516
**kwargs):
15161517
"""
@@ -1588,6 +1589,12 @@ def query(self,
15881589
field is specified, returned results are biased towards higher field
15891590
values. This parameter cannot be used in the same query as the **sort**
15901591
parameter.
1592+
:param bool spelling_suggestions: (optional) When `true` and the
1593+
**natural_language_query** parameter is used, the **natural_languge_query**
1594+
parameter is spell checked. The most likely correction is retunred in the
1595+
**suggested_query** field of the response (if one exists).
1596+
**Important:** this parameter is only valid when using the Cloud Pak
1597+
version of Discovery.
15911598
:param bool x_watson_logging_opt_out: (optional) If `true`, queries are not
15921599
stored in the Discovery **Logs** endpoint.
15931600
:param dict headers: A `dict` containing the request headers
@@ -1627,7 +1634,8 @@ def query(self,
16271634
'similar': similar,
16281635
'similar.document_ids': similar_document_ids,
16291636
'similar.fields': similar_fields,
1630-
'bias': bias
1637+
'bias': bias,
1638+
'spelling_suggestions': spelling_suggestions
16311639
}
16321640

16331641
url = '/v1/environments/{0}/collections/{1}/query'.format(
@@ -2047,6 +2055,63 @@ def federated_query_notices(self,
20472055
response = self.send(request)
20482056
return response
20492057

2058+
def get_autocompletion(self,
2059+
environment_id,
2060+
collection_id,
2061+
*,
2062+
field=None,
2063+
prefix=None,
2064+
count=None,
2065+
**kwargs):
2066+
"""
2067+
Get Autocomplete Suggestions.
2068+
2069+
Returns completion query suggestions for the specified prefix. /n/n
2070+
**Important:** this method is only valid when using the Cloud Pak version of
2071+
Discovery.
2072+
2073+
:param str environment_id: The ID of the environment.
2074+
:param str collection_id: The ID of the collection.
2075+
:param str field: (optional) The field in the result documents that
2076+
autocompletion suggestions are identified from.
2077+
:param str prefix: (optional) The prefix to use for autocompletion. For
2078+
example, the prefix `Ho` could autocomplete to `Hot`, `Housing`, or `How do
2079+
I upgrade`. Possible completions are.
2080+
:param int count: (optional) The number of autocompletion suggestions to
2081+
return.
2082+
:param dict headers: A `dict` containing the request headers
2083+
:return: A `DetailedResponse` containing the result, headers and HTTP status code.
2084+
:rtype: DetailedResponse
2085+
"""
2086+
2087+
if environment_id is None:
2088+
raise ValueError('environment_id must be provided')
2089+
if collection_id is None:
2090+
raise ValueError('collection_id must be provided')
2091+
2092+
headers = {}
2093+
if 'headers' in kwargs:
2094+
headers.update(kwargs.get('headers'))
2095+
sdk_headers = get_sdk_headers('discovery', 'V1', 'get_autocompletion')
2096+
headers.update(sdk_headers)
2097+
2098+
params = {
2099+
'version': self.version,
2100+
'field': field,
2101+
'prefix': prefix,
2102+
'count': count
2103+
}
2104+
2105+
url = '/v1/environments/{0}/collections/{1}/autocompletion'.format(
2106+
*self._encode_path_vars(environment_id, collection_id))
2107+
request = self.prepare_request(method='GET',
2108+
url=url,
2109+
headers=headers,
2110+
params=params,
2111+
accept_json=True)
2112+
response = self.send(request)
2113+
return response
2114+
20502115
#########################
20512116
# Training data
20522117
#########################
@@ -3857,6 +3922,59 @@ def __ne__(self, other):
38573922
return not self == other
38583923

38593924

3925+
class Completions():
3926+
"""
3927+
An object containing an array of autocompletion suggestions.
3928+
3929+
:attr list[str] completions: (optional) Array of autcomplete suggestion based on
3930+
the provided prefix.
3931+
"""
3932+
3933+
def __init__(self, *, completions=None):
3934+
"""
3935+
Initialize a Completions object.
3936+
3937+
:param list[str] completions: (optional) Array of autcomplete suggestion
3938+
based on the provided prefix.
3939+
"""
3940+
self.completions = completions
3941+
3942+
@classmethod
3943+
def _from_dict(cls, _dict):
3944+
"""Initialize a Completions object from a json dictionary."""
3945+
args = {}
3946+
valid_keys = ['completions']
3947+
bad_keys = set(_dict.keys()) - set(valid_keys)
3948+
if bad_keys:
3949+
raise ValueError(
3950+
'Unrecognized keys detected in dictionary for class Completions: '
3951+
+ ', '.join(bad_keys))
3952+
if 'completions' in _dict:
3953+
args['completions'] = _dict.get('completions')
3954+
return cls(**args)
3955+
3956+
def _to_dict(self):
3957+
"""Return a json dictionary representing this model."""
3958+
_dict = {}
3959+
if hasattr(self, 'completions') and self.completions is not None:
3960+
_dict['completions'] = self.completions
3961+
return _dict
3962+
3963+
def __str__(self):
3964+
"""Return a `str` version of this Completions object."""
3965+
return json.dumps(self._to_dict(), indent=2)
3966+
3967+
def __eq__(self, other):
3968+
"""Return `true` when self and other are equal, false otherwise."""
3969+
if not isinstance(other, self.__class__):
3970+
return False
3971+
return self.__dict__ == other.__dict__
3972+
3973+
def __ne__(self, other):
3974+
"""Return `true` when self and other are not equal, false otherwise."""
3975+
return not self == other
3976+
3977+
38603978
class Configuration():
38613979
"""
38623980
A custom configuration for the environment.

test/unit/test_discovery_v1.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,3 +1272,30 @@ def test_gateway_configuration(cls):
12721272
discovery.get_gateway('envid', 'gateway_id')
12731273
discovery.delete_gateway(environment_id='envid', gateway_id='gateway_id')
12741274
assert len(responses.calls) == 8
1275+
1276+
1277+
@responses.activate
1278+
def test_get_autocompletion(self):
1279+
endpoint = 'environments/{0}/collections/{1}/autocompletion?version=2018-08-13&field=field&prefix=prefix&count=count'.format('environment_id', 'collection_id').format('collection_id')
1280+
url = '{0}{1}'.format(base_discovery_url, endpoint)
1281+
print('hello')
1282+
print(url)
1283+
response = {
1284+
"completions" : [ "completions", "completions" ]
1285+
}
1286+
responses.add(responses.GET,
1287+
url,
1288+
body=json.dumps(response),
1289+
status=200,
1290+
content_type='application/json')
1291+
1292+
authenticator = IAMAuthenticator('iam_apikey')
1293+
discovery = ibm_watson.DiscoveryV1('2018-08-13', authenticator=authenticator)
1294+
1295+
detailed_response = discovery.get_autocompletion(environment_id='environment_id',
1296+
collection_id='collection_id',
1297+
field='field',
1298+
prefix='prefix',
1299+
count='count')
1300+
result = detailed_response.get_result()
1301+
assert len(responses.calls) == 2

0 commit comments

Comments
 (0)