Skip to content

Commit 7a915f5

Browse files
committed
feat(NLU): NLU Confidence field in EntitesResult and EntityMention
1 parent a3cf2ad commit 7a915f5

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

ibm_watson/natural_language_understanding_v1.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,10 @@ class EntitiesResult(object):
16571657
:attr str text: (optional) The name of the entity.
16581658
:attr float relevance: (optional) Relevance score from 0 to 1. Higher values indicate
16591659
greater relevance.
1660+
:attr float confidence: (optional) Confidence in the entity identification from 0 to
1661+
1. Higher values indicate higher confidence. In standard entities requests, confidence
1662+
is returned only for English text. All entities requests that use custom models return
1663+
the confidence score.
16601664
:attr list[EntityMention] mentions: (optional) Entity mentions and locations.
16611665
:attr int count: (optional) How many times the entity was mentioned in the text.
16621666
:attr EmotionScores emotion: (optional) Emotion analysis results for the entity,
@@ -1671,6 +1675,7 @@ def __init__(self,
16711675
type=None,
16721676
text=None,
16731677
relevance=None,
1678+
confidence=None,
16741679
mentions=None,
16751680
count=None,
16761681
emotion=None,
@@ -1683,6 +1688,10 @@ def __init__(self,
16831688
:param str text: (optional) The name of the entity.
16841689
:param float relevance: (optional) Relevance score from 0 to 1. Higher values
16851690
indicate greater relevance.
1691+
:param float confidence: (optional) Confidence in the entity identification from 0
1692+
to 1. Higher values indicate higher confidence. In standard entities requests,
1693+
confidence is returned only for English text. All entities requests that use
1694+
custom models return the confidence score.
16861695
:param list[EntityMention] mentions: (optional) Entity mentions and locations.
16871696
:param int count: (optional) How many times the entity was mentioned in the text.
16881697
:param EmotionScores emotion: (optional) Emotion analysis results for the entity,
@@ -1695,6 +1704,7 @@ def __init__(self,
16951704
self.type = type
16961705
self.text = text
16971706
self.relevance = relevance
1707+
self.confidence = confidence
16981708
self.mentions = mentions
16991709
self.count = count
17001710
self.emotion = emotion
@@ -1706,8 +1716,8 @@ def _from_dict(cls, _dict):
17061716
"""Initialize a EntitiesResult object from a json dictionary."""
17071717
args = {}
17081718
validKeys = [
1709-
'type', 'text', 'relevance', 'mentions', 'count', 'emotion',
1710-
'sentiment', 'disambiguation'
1719+
'type', 'text', 'relevance', 'confidence', 'mentions', 'count',
1720+
'emotion', 'sentiment', 'disambiguation'
17111721
]
17121722
badKeys = set(_dict.keys()) - set(validKeys)
17131723
if badKeys:
@@ -1720,6 +1730,8 @@ def _from_dict(cls, _dict):
17201730
args['text'] = _dict.get('text')
17211731
if 'relevance' in _dict:
17221732
args['relevance'] = _dict.get('relevance')
1733+
if 'confidence' in _dict:
1734+
args['confidence'] = _dict.get('confidence')
17231735
if 'mentions' in _dict:
17241736
args['mentions'] = [
17251737
EntityMention._from_dict(x) for x in (_dict.get('mentions'))
@@ -1745,6 +1757,8 @@ def _to_dict(self):
17451757
_dict['text'] = self.text
17461758
if hasattr(self, 'relevance') and self.relevance is not None:
17471759
_dict['relevance'] = self.relevance
1760+
if hasattr(self, 'confidence') and self.confidence is not None:
1761+
_dict['confidence'] = self.confidence
17481762
if hasattr(self, 'mentions') and self.mentions is not None:
17491763
_dict['mentions'] = [x._to_dict() for x in self.mentions]
17501764
if hasattr(self, 'count') and self.count is not None:
@@ -1779,24 +1793,33 @@ class EntityMention(object):
17791793
:attr str text: (optional) Entity mention text.
17801794
:attr list[int] location: (optional) Character offsets indicating the beginning and
17811795
end of the mention in the analyzed text.
1796+
:attr float confidence: (optional) Confidence in the entity identification from 0 to
1797+
1. Higher values indicate higher confidence. In standard entities requests, confidence
1798+
is returned only for English text. All entities requests that use custom models return
1799+
the confidence score.
17821800
"""
17831801

1784-
def __init__(self, text=None, location=None):
1802+
def __init__(self, text=None, location=None, confidence=None):
17851803
"""
17861804
Initialize a EntityMention object.
17871805
17881806
:param str text: (optional) Entity mention text.
17891807
:param list[int] location: (optional) Character offsets indicating the beginning
17901808
and end of the mention in the analyzed text.
1809+
:param float confidence: (optional) Confidence in the entity identification from 0
1810+
to 1. Higher values indicate higher confidence. In standard entities requests,
1811+
confidence is returned only for English text. All entities requests that use
1812+
custom models return the confidence score.
17911813
"""
17921814
self.text = text
17931815
self.location = location
1816+
self.confidence = confidence
17941817

17951818
@classmethod
17961819
def _from_dict(cls, _dict):
17971820
"""Initialize a EntityMention object from a json dictionary."""
17981821
args = {}
1799-
validKeys = ['text', 'location']
1822+
validKeys = ['text', 'location', 'confidence']
18001823
badKeys = set(_dict.keys()) - set(validKeys)
18011824
if badKeys:
18021825
raise ValueError(
@@ -1806,6 +1829,8 @@ def _from_dict(cls, _dict):
18061829
args['text'] = _dict.get('text')
18071830
if 'location' in _dict:
18081831
args['location'] = _dict.get('location')
1832+
if 'confidence' in _dict:
1833+
args['confidence'] = _dict.get('confidence')
18091834
return cls(**args)
18101835

18111836
def _to_dict(self):
@@ -1815,6 +1840,8 @@ def _to_dict(self):
18151840
_dict['text'] = self.text
18161841
if hasattr(self, 'location') and self.location is not None:
18171842
_dict['location'] = self.location
1843+
if hasattr(self, 'confidence') and self.confidence is not None:
1844+
_dict['confidence'] = self.confidence
18181845
return _dict
18191846

18201847
def __str__(self):

0 commit comments

Comments
 (0)