@@ -93,14 +93,21 @@ def translate(self,
93
93
"""
94
94
Translate.
95
95
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.
97
99
98
100
:param List[str] text: Input text in UTF-8 encoding. Multiple entries will
99
101
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.
104
111
:param dict headers: A `dict` containing the request headers
105
112
:return: A `DetailedResponse` containing the result, headers and HTTP status code.
106
113
:rtype: DetailedResponse
@@ -469,16 +476,19 @@ def translate_document(self,
469
476
470
477
:param TextIO file: The contents of the source file to translate.
471
478
[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)
473
480
Maximum file size: **20 MB**.
474
481
:param str filename: (optional) The filename for file.
475
482
: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.
478
488
:param str source: (optional) Language code that specifies the language of
479
489
the source document.
480
490
: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.
482
492
:param str document_id: (optional) To use a previously submitted document
483
493
as the source for a new translation, enter the `document_id` of the
484
494
document.
@@ -879,6 +889,10 @@ class DocumentStatus():
879
889
customize the model. If the model is not a custom model, this will be absent or
880
890
an empty string.
881
891
: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.
882
896
:attr str target: Translation target language code.
883
897
:attr datetime created: The time when the document was submitted.
884
898
:attr datetime completed: (optional) The time when the translation completed.
@@ -898,6 +912,7 @@ def __init__(self,
898
912
created : datetime ,
899
913
* ,
900
914
base_model_id : str = None ,
915
+ detected_language_confidence : float = None ,
901
916
completed : datetime = None ,
902
917
word_count : int = None ,
903
918
character_count : int = None ) -> None :
@@ -918,6 +933,10 @@ def __init__(self,
918
933
:param str base_model_id: (optional) Model ID of the base model that was
919
934
used to customize the model. If the model is not a custom model, this will
920
935
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.
921
940
:param datetime completed: (optional) The time when the translation
922
941
completed.
923
942
:param int word_count: (optional) An estimate of the number of words in the
@@ -931,6 +950,7 @@ def __init__(self,
931
950
self .model_id = model_id
932
951
self .base_model_id = base_model_id
933
952
self .source = source
953
+ self .detected_language_confidence = detected_language_confidence
934
954
self .target = target
935
955
self .created = created
936
956
self .completed = completed
@@ -943,8 +963,8 @@ def from_dict(cls, _dict: Dict) -> 'DocumentStatus':
943
963
args = {}
944
964
valid_keys = [
945
965
'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'
948
968
]
949
969
bad_keys = set (_dict .keys ()) - set (valid_keys )
950
970
if bad_keys :
@@ -983,6 +1003,9 @@ def from_dict(cls, _dict: Dict) -> 'DocumentStatus':
983
1003
raise ValueError (
984
1004
'Required property \' source\' not present in DocumentStatus JSON'
985
1005
)
1006
+ if 'detected_language_confidence' in _dict :
1007
+ args ['detected_language_confidence' ] = _dict .get (
1008
+ 'detected_language_confidence' )
986
1009
if 'target' in _dict :
987
1010
args ['target' ] = _dict .get ('target' )
988
1011
else :
@@ -1023,6 +1046,10 @@ def to_dict(self) -> Dict:
1023
1046
_dict ['base_model_id' ] = self .base_model_id
1024
1047
if hasattr (self , 'source' ) and self .source is not None :
1025
1048
_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
1026
1053
if hasattr (self , 'target' ) and self .target is not None :
1027
1054
_dict ['target' ] = self .target
1028
1055
if hasattr (self , 'created' ) and self .created is not None :
@@ -1663,12 +1690,23 @@ class TranslationResult():
1663
1690
1664
1691
:attr int word_count: An estimate of the number of words in the input text.
1665
1692
: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.
1666
1699
:attr List[Translation] translations: List of translation output in UTF-8,
1667
1700
corresponding to the input text entries.
1668
1701
"""
1669
1702
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 :
1672
1710
"""
1673
1711
Initialize a TranslationResult object.
1674
1712
@@ -1677,16 +1715,27 @@ def __init__(self, word_count: int, character_count: int,
1677
1715
:param int character_count: Number of characters in the input text.
1678
1716
:param List[Translation] translations: List of translation output in UTF-8,
1679
1717
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.
1680
1724
"""
1681
1725
self .word_count = word_count
1682
1726
self .character_count = character_count
1727
+ self .detected_language = detected_language
1728
+ self .detected_language_confidence = detected_language_confidence
1683
1729
self .translations = translations
1684
1730
1685
1731
@classmethod
1686
1732
def from_dict (cls , _dict : Dict ) -> 'TranslationResult' :
1687
1733
"""Initialize a TranslationResult object from a json dictionary."""
1688
1734
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
+ ]
1690
1739
bad_keys = set (_dict .keys ()) - set (valid_keys )
1691
1740
if bad_keys :
1692
1741
raise ValueError (
@@ -1704,6 +1753,11 @@ def from_dict(cls, _dict: Dict) -> 'TranslationResult':
1704
1753
raise ValueError (
1705
1754
'Required property \' character_count\' not present in TranslationResult JSON'
1706
1755
)
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' )
1707
1761
if 'translations' in _dict :
1708
1762
args ['translations' ] = [
1709
1763
Translation ._from_dict (x ) for x in (_dict .get ('translations' ))
@@ -1727,6 +1781,13 @@ def to_dict(self) -> Dict:
1727
1781
if hasattr (self ,
1728
1782
'character_count' ) and self .character_count is not None :
1729
1783
_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
1730
1791
if hasattr (self , 'translations' ) and self .translations is not None :
1731
1792
_dict ['translations' ] = [x ._to_dict () for x in self .translations ]
1732
1793
return _dict
0 commit comments