Skip to content

Commit

Permalink
feat(assistant_v2,disco_v1): add answers property to response model, …
Browse files Browse the repository at this point in the history
…fix typo
  • Loading branch information
apaparazzi0329 committed Sep 14, 2021
1 parent ab880f0 commit 611b7c9
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 26 deletions.
95 changes: 94 additions & 1 deletion ibm_watson/assistant_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4843,6 +4843,12 @@ class SearchResult():
:attr SearchResultHighlight highlight: (optional) An object containing segments
of text from search results with query-matching text highlighted using HTML
`<em>` tags.
:attr List[SearchResultAnswer] answers: (optional) An array specifying segments
of text within the result that were identified as direct answers to the search
query. Currently, only the single answer with the highest confidence (if any) is
returned.
**Note:** This property uses the answer finding beta feature, and is available
only if the search skill is connected to a Discovery v2 service instance.
"""

def __init__(self,
Expand All @@ -4852,7 +4858,8 @@ def __init__(self,
body: str = None,
title: str = None,
url: str = None,
highlight: 'SearchResultHighlight' = None) -> None:
highlight: 'SearchResultHighlight' = None,
answers: List['SearchResultAnswer'] = None) -> None:
"""
Initialize a SearchResult object.
Expand All @@ -4873,13 +4880,21 @@ def __init__(self,
:param SearchResultHighlight highlight: (optional) An object containing
segments of text from search results with query-matching text highlighted
using HTML `<em>` tags.
:param List[SearchResultAnswer] answers: (optional) An array specifying
segments of text within the result that were identified as direct answers
to the search query. Currently, only the single answer with the highest
confidence (if any) is returned.
**Note:** This property uses the answer finding beta feature, and is
available only if the search skill is connected to a Discovery v2 service
instance.
"""
self.id = id
self.result_metadata = result_metadata
self.body = body
self.title = title
self.url = url
self.highlight = highlight
self.answers = answers

@classmethod
def from_dict(cls, _dict: Dict) -> 'SearchResult':
Expand All @@ -4906,6 +4921,10 @@ def from_dict(cls, _dict: Dict) -> 'SearchResult':
if 'highlight' in _dict:
args['highlight'] = SearchResultHighlight.from_dict(
_dict.get('highlight'))
if 'answers' in _dict:
args['answers'] = [
SearchResultAnswer.from_dict(x) for x in _dict.get('answers')
]
return cls(**args)

@classmethod
Expand All @@ -4929,6 +4948,8 @@ def to_dict(self) -> Dict:
_dict['url'] = self.url
if hasattr(self, 'highlight') and self.highlight is not None:
_dict['highlight'] = self.highlight.to_dict()
if hasattr(self, 'answers') and self.answers is not None:
_dict['answers'] = [x.to_dict() for x in self.answers]
return _dict

def _to_dict(self):
Expand All @@ -4950,6 +4971,78 @@ def __ne__(self, other: 'SearchResult') -> bool:
return not self == other


class SearchResultAnswer():
"""
An object specifing a segment of text that was identified as a direct answer to the
search query.
:attr str text: The text of the answer.
:attr float confidence: The confidence score for the answer, as returned by the
Discovery service.
"""

def __init__(self, text: str, confidence: float) -> None:
"""
Initialize a SearchResultAnswer object.
:param str text: The text of the answer.
:param float confidence: The confidence score for the answer, as returned
by the Discovery service.
"""
self.text = text
self.confidence = confidence

@classmethod
def from_dict(cls, _dict: Dict) -> 'SearchResultAnswer':
"""Initialize a SearchResultAnswer object from a json dictionary."""
args = {}
if 'text' in _dict:
args['text'] = _dict.get('text')
else:
raise ValueError(
'Required property \'text\' not present in SearchResultAnswer JSON'
)
if 'confidence' in _dict:
args['confidence'] = _dict.get('confidence')
else:
raise ValueError(
'Required property \'confidence\' not present in SearchResultAnswer JSON'
)
return cls(**args)

@classmethod
def _from_dict(cls, _dict):
"""Initialize a SearchResultAnswer object from a json dictionary."""
return cls.from_dict(_dict)

def to_dict(self) -> Dict:
"""Return a json dictionary representing this model."""
_dict = {}
if hasattr(self, 'text') and self.text is not None:
_dict['text'] = self.text
if hasattr(self, 'confidence') and self.confidence is not None:
_dict['confidence'] = self.confidence
return _dict

def _to_dict(self):
"""Return a json dictionary representing this model."""
return self.to_dict()

def __str__(self) -> str:
"""Return a `str` version of this SearchResultAnswer object."""
return json.dumps(self.to_dict(), indent=2)

def __eq__(self, other: 'SearchResultAnswer') -> bool:
"""Return `true` when self and other are equal, false otherwise."""
if not isinstance(other, self.__class__):
return False
return self.__dict__ == other.__dict__

def __ne__(self, other: 'SearchResultAnswer') -> bool:
"""Return `true` when self and other are not equal, false otherwise."""
return not self == other


class SearchResultHighlight():
"""
An object containing segments of text from search results with query-matching text
Expand Down
20 changes: 10 additions & 10 deletions ibm_watson/discovery_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11967,33 +11967,33 @@ class StatusDetails():
"""
Object that contains details about the status of the authentication process.

:attr bool authentication: (optional) Indicates whether the credential is
:attr bool authenticated: (optional) Indicates whether the credential is
accepted by the target data source.
:attr str error_message: (optional) If `authentication` is `false`, a message
:attr str error_message: (optional) If `authenticated` is `false`, a message
describes why the authentication was unsuccessful.
"""

def __init__(self,
*,
authentication: bool = None,
authenticated: bool = None,
error_message: str = None) -> None:
"""
Initialize a StatusDetails object.

:param bool authentication: (optional) Indicates whether the credential is
:param bool authenticated: (optional) Indicates whether the credential is
accepted by the target data source.
:param str error_message: (optional) If `authentication` is `false`, a
:param str error_message: (optional) If `authenticated` is `false`, a
message describes why the authentication was unsuccessful.
"""
self.authentication = authentication
self.authenticated = authenticated
self.error_message = error_message

@classmethod
def from_dict(cls, _dict: Dict) -> 'StatusDetails':
"""Initialize a StatusDetails object from a json dictionary."""
args = {}
if 'authentication' in _dict:
args['authentication'] = _dict.get('authentication')
if 'authenticated' in _dict:
args['authenticated'] = _dict.get('authenticated')
if 'error_message' in _dict:
args['error_message'] = _dict.get('error_message')
return cls(**args)
Expand All @@ -12006,8 +12006,8 @@ def _from_dict(cls, _dict):
def to_dict(self) -> Dict:
"""Return a json dictionary representing this model."""
_dict = {}
if hasattr(self, 'authentication') and self.authentication is not None:
_dict['authentication'] = self.authentication
if hasattr(self, 'authenticated') and self.authenticated is not None:
_dict['authenticated'] = self.authenticated
if hasattr(self, 'error_message') and self.error_message is not None:
_dict['error_message'] = self.error_message
return _dict
Expand Down
40 changes: 40 additions & 0 deletions test/unit/test_assistant_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4009,6 +4009,10 @@ def test_search_result_serialization(self):
search_result_highlight_model['url'] = ['testString']
search_result_highlight_model['foo'] = ['testString']

search_result_answer_model = {} # SearchResultAnswer
search_result_answer_model['text'] = 'testString'
search_result_answer_model['confidence'] = 0

# Construct a json representation of a SearchResult model
search_result_model_json = {}
search_result_model_json['id'] = 'testString'
Expand All @@ -4017,6 +4021,7 @@ def test_search_result_serialization(self):
search_result_model_json['title'] = 'testString'
search_result_model_json['url'] = 'testString'
search_result_model_json['highlight'] = search_result_highlight_model
search_result_model_json['answers'] = [search_result_answer_model]

# Construct a model instance of SearchResult by calling from_dict on the json representation
search_result_model = SearchResult.from_dict(search_result_model_json)
Expand All @@ -4033,6 +4038,36 @@ def test_search_result_serialization(self):
search_result_model_json2 = search_result_model.to_dict()
assert search_result_model_json2 == search_result_model_json

class TestModel_SearchResultAnswer():
"""
Test Class for SearchResultAnswer
"""

def test_search_result_answer_serialization(self):
"""
Test serialization/deserialization for SearchResultAnswer
"""

# Construct a json representation of a SearchResultAnswer model
search_result_answer_model_json = {}
search_result_answer_model_json['text'] = 'testString'
search_result_answer_model_json['confidence'] = 0

# Construct a model instance of SearchResultAnswer by calling from_dict on the json representation
search_result_answer_model = SearchResultAnswer.from_dict(search_result_answer_model_json)
assert search_result_answer_model != False

# Construct a model instance of SearchResultAnswer by calling from_dict on the json representation
search_result_answer_model_dict = SearchResultAnswer.from_dict(search_result_answer_model_json).__dict__
search_result_answer_model2 = SearchResultAnswer(**search_result_answer_model_dict)

# Verify the model instances are equivalent
assert search_result_answer_model == search_result_answer_model2

# Convert model instance back to dict and verify no loss of data
search_result_answer_model_json2 = search_result_answer_model.to_dict()
assert search_result_answer_model_json2 == search_result_answer_model_json

class TestModel_SearchResultHighlight():
"""
Test Class for SearchResultHighlight
Expand Down Expand Up @@ -4567,13 +4602,18 @@ def test_runtime_response_generic_runtime_response_type_search_serialization(sel
search_result_highlight_model['url'] = ['testString']
search_result_highlight_model['foo'] = ['testString']

search_result_answer_model = {} # SearchResultAnswer
search_result_answer_model['text'] = 'testString'
search_result_answer_model['confidence'] = 0

search_result_model = {} # SearchResult
search_result_model['id'] = 'testString'
search_result_model['result_metadata'] = search_result_metadata_model
search_result_model['body'] = 'testString'
search_result_model['title'] = 'testString'
search_result_model['url'] = 'testString'
search_result_model['highlight'] = search_result_highlight_model
search_result_model['answers'] = [search_result_answer_model]

response_generic_channel_model = {} # ResponseGenericChannel
response_generic_channel_model['channel'] = 'testString'
Expand Down
Loading

0 comments on commit 611b7c9

Please sign in to comment.