Skip to content

Commit 230878a

Browse files
committed
feat(LanguageTranslator): add support for auto correct
1 parent 4e57026 commit 230878a

File tree

1 file changed

+75
-14
lines changed

1 file changed

+75
-14
lines changed

ibm_watson/language_translator_v3.py

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,21 @@ def translate(self,
9393
"""
9494
Translate.
9595
96-
Translates the input text from the source language to the target language.
96+
Translates the input text from the source language to the target language. A
97+
target language or translation model ID is required. The service attempts to
98+
detect the language of the source text if it is not specified.
9799
98100
:param List[str] text: Input text in UTF-8 encoding. Multiple entries will
99101
result in multiple translations in the response.
100-
:param str model_id: (optional) A globally unique string that identifies
101-
the underlying model that is used for translation.
102-
:param str source: (optional) Translation source language code.
103-
:param str target: (optional) Translation target language code.
102+
:param str model_id: (optional) The model to use for translation. For
103+
example, `en-de` selects the IBM provided base model for English to German
104+
translation. A model ID overrides the source and target parameters and is
105+
required if you use a custom model. If no model ID is specified, you must
106+
specify a target language.
107+
:param str source: (optional) Language code that specifies the language of
108+
the source document.
109+
:param str target: (optional) Language code that specifies the target
110+
language for translation. Required if model ID is not specified.
104111
:param dict headers: A `dict` containing the request headers
105112
:return: A `DetailedResponse` containing the result, headers and HTTP status code.
106113
:rtype: DetailedResponse
@@ -469,16 +476,19 @@ def translate_document(self,
469476
470477
:param TextIO file: The contents of the source file to translate.
471478
[Supported file
472-
types](https://cloud.ibm.com/docs/services/language-translator?topic=language-translator-document-translator-tutorial#supported-file-formats)
479+
types](https://cloud.ibm.com/docs/language-translator?topic=language-translator-document-translator-tutorial#supported-file-formats)
473480
Maximum file size: **20 MB**.
474481
:param str filename: (optional) The filename for file.
475482
:param str file_content_type: (optional) The content type of file.
476-
:param str model_id: (optional) The model to use for translation.
477-
`model_id` or both `source` and `target` are required.
483+
:param str model_id: (optional) The model to use for translation. For
484+
example, `en-de` selects the IBM provided base model for English to German
485+
translation. A model ID overrides the source and target parameters and is
486+
required if you use a custom model. If no model ID is specified, you must
487+
specify a target language.
478488
:param str source: (optional) Language code that specifies the language of
479489
the source document.
480490
:param str target: (optional) Language code that specifies the target
481-
language for translation.
491+
language for translation. Required if model ID is not specified.
482492
:param str document_id: (optional) To use a previously submitted document
483493
as the source for a new translation, enter the `document_id` of the
484494
document.
@@ -879,6 +889,10 @@ class DocumentStatus():
879889
customize the model. If the model is not a custom model, this will be absent or
880890
an empty string.
881891
:attr str source: Translation source language code.
892+
:attr float detected_language_confidence: (optional) A score between 0 and 1
893+
indicating the confidence of source language detection. A higher value indicates
894+
greater confidence. This is returned only when the service automatically detects
895+
the source language.
882896
:attr str target: Translation target language code.
883897
:attr datetime created: The time when the document was submitted.
884898
:attr datetime completed: (optional) The time when the translation completed.
@@ -898,6 +912,7 @@ def __init__(self,
898912
created: datetime,
899913
*,
900914
base_model_id: str = None,
915+
detected_language_confidence: float = None,
901916
completed: datetime = None,
902917
word_count: int = None,
903918
character_count: int = None) -> None:
@@ -918,6 +933,10 @@ def __init__(self,
918933
:param str base_model_id: (optional) Model ID of the base model that was
919934
used to customize the model. If the model is not a custom model, this will
920935
be absent or an empty string.
936+
:param float detected_language_confidence: (optional) A score between 0 and
937+
1 indicating the confidence of source language detection. A higher value
938+
indicates greater confidence. This is returned only when the service
939+
automatically detects the source language.
921940
:param datetime completed: (optional) The time when the translation
922941
completed.
923942
:param int word_count: (optional) An estimate of the number of words in the
@@ -931,6 +950,7 @@ def __init__(self,
931950
self.model_id = model_id
932951
self.base_model_id = base_model_id
933952
self.source = source
953+
self.detected_language_confidence = detected_language_confidence
934954
self.target = target
935955
self.created = created
936956
self.completed = completed
@@ -943,8 +963,8 @@ def from_dict(cls, _dict: Dict) -> 'DocumentStatus':
943963
args = {}
944964
valid_keys = [
945965
'document_id', 'filename', 'status', 'model_id', 'base_model_id',
946-
'source', 'target', 'created', 'completed', 'word_count',
947-
'character_count'
966+
'source', 'detected_language_confidence', 'target', 'created',
967+
'completed', 'word_count', 'character_count'
948968
]
949969
bad_keys = set(_dict.keys()) - set(valid_keys)
950970
if bad_keys:
@@ -983,6 +1003,9 @@ def from_dict(cls, _dict: Dict) -> 'DocumentStatus':
9831003
raise ValueError(
9841004
'Required property \'source\' not present in DocumentStatus JSON'
9851005
)
1006+
if 'detected_language_confidence' in _dict:
1007+
args['detected_language_confidence'] = _dict.get(
1008+
'detected_language_confidence')
9861009
if 'target' in _dict:
9871010
args['target'] = _dict.get('target')
9881011
else:
@@ -1023,6 +1046,10 @@ def to_dict(self) -> Dict:
10231046
_dict['base_model_id'] = self.base_model_id
10241047
if hasattr(self, 'source') and self.source is not None:
10251048
_dict['source'] = self.source
1049+
if hasattr(self, 'detected_language_confidence'
1050+
) and self.detected_language_confidence is not None:
1051+
_dict[
1052+
'detected_language_confidence'] = self.detected_language_confidence
10261053
if hasattr(self, 'target') and self.target is not None:
10271054
_dict['target'] = self.target
10281055
if hasattr(self, 'created') and self.created is not None:
@@ -1663,12 +1690,23 @@ class TranslationResult():
16631690
16641691
:attr int word_count: An estimate of the number of words in the input text.
16651692
:attr int character_count: Number of characters in the input text.
1693+
:attr str detected_language: (optional) The language code of the source text if
1694+
the source language was automatically detected.
1695+
:attr float detected_language_confidence: (optional) A score between 0 and 1
1696+
indicating the confidence of source language detection. A higher value indicates
1697+
greater confidence. This is returned only when the service automatically detects
1698+
the source language.
16661699
:attr List[Translation] translations: List of translation output in UTF-8,
16671700
corresponding to the input text entries.
16681701
"""
16691702

1670-
def __init__(self, word_count: int, character_count: int,
1671-
translations: List['Translation']) -> None:
1703+
def __init__(self,
1704+
word_count: int,
1705+
character_count: int,
1706+
translations: List['Translation'],
1707+
*,
1708+
detected_language: str = None,
1709+
detected_language_confidence: float = None) -> None:
16721710
"""
16731711
Initialize a TranslationResult object.
16741712
@@ -1677,16 +1715,27 @@ def __init__(self, word_count: int, character_count: int,
16771715
:param int character_count: Number of characters in the input text.
16781716
:param List[Translation] translations: List of translation output in UTF-8,
16791717
corresponding to the input text entries.
1718+
:param str detected_language: (optional) The language code of the source
1719+
text if the source language was automatically detected.
1720+
:param float detected_language_confidence: (optional) A score between 0 and
1721+
1 indicating the confidence of source language detection. A higher value
1722+
indicates greater confidence. This is returned only when the service
1723+
automatically detects the source language.
16801724
"""
16811725
self.word_count = word_count
16821726
self.character_count = character_count
1727+
self.detected_language = detected_language
1728+
self.detected_language_confidence = detected_language_confidence
16831729
self.translations = translations
16841730

16851731
@classmethod
16861732
def from_dict(cls, _dict: Dict) -> 'TranslationResult':
16871733
"""Initialize a TranslationResult object from a json dictionary."""
16881734
args = {}
1689-
valid_keys = ['word_count', 'character_count', 'translations']
1735+
valid_keys = [
1736+
'word_count', 'character_count', 'detected_language',
1737+
'detected_language_confidence', 'translations'
1738+
]
16901739
bad_keys = set(_dict.keys()) - set(valid_keys)
16911740
if bad_keys:
16921741
raise ValueError(
@@ -1704,6 +1753,11 @@ def from_dict(cls, _dict: Dict) -> 'TranslationResult':
17041753
raise ValueError(
17051754
'Required property \'character_count\' not present in TranslationResult JSON'
17061755
)
1756+
if 'detected_language' in _dict:
1757+
args['detected_language'] = _dict.get('detected_language')
1758+
if 'detected_language_confidence' in _dict:
1759+
args['detected_language_confidence'] = _dict.get(
1760+
'detected_language_confidence')
17071761
if 'translations' in _dict:
17081762
args['translations'] = [
17091763
Translation._from_dict(x) for x in (_dict.get('translations'))
@@ -1727,6 +1781,13 @@ def to_dict(self) -> Dict:
17271781
if hasattr(self,
17281782
'character_count') and self.character_count is not None:
17291783
_dict['character_count'] = self.character_count
1784+
if hasattr(self,
1785+
'detected_language') and self.detected_language is not None:
1786+
_dict['detected_language'] = self.detected_language
1787+
if hasattr(self, 'detected_language_confidence'
1788+
) and self.detected_language_confidence is not None:
1789+
_dict[
1790+
'detected_language_confidence'] = self.detected_language_confidence
17301791
if hasattr(self, 'translations') and self.translations is not None:
17311792
_dict['translations'] = [x._to_dict() for x in self.translations]
17321793
return _dict

0 commit comments

Comments
 (0)