翻译(Language Translator)
简介
本部分提供了基本的信息,并且解释了当使用不同编程语言时所需要方法的参数。我这里以python为例。
认证
在bluemix上获得该服务,并且获得用户和密码信息,详见Watson使用指南。
from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator
language_translator = LanguageTranslator(
username='{username}',
password='{password}')
方法
翻译(Translate)
将输入文本翻译成指定语言
参数:
model_id (string型,用于翻译的模板,启用该参数时其余参数可忽略)
source (string型,输入文本的语言,如不设定,系统将默认)
target (string型,目标语言)
text (string型,必须,UTF-8格式,待翻译文本)
import json
from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator
language_translator = LanguageTranslator(
username='{username}',
password='{password}')
translation = language_translator.translate(
text='hello',
source='en',
target='es')
print(json.dumps(translation, indent=2, ensure_ascii=False))
返回结果:
参数:
translations (包含返回信息的数组)
translation (翻译结果,UTF-8格式)
word_count (待翻译文本单词数)
character_count (待翻译文本的字母数)
{
"translations": [{
"translation": "Hola"
}],
"word_count": 1,
"character_count": 5
}
识别语言(Identifiable Language)
返回能够识别的语言列表
参数:
无参数
import json
from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator
language_translator = LanguageTranslator(
username='{username}',
password='{password}')
languages = language_translator.get_identifiable_languages()
print(json.dumps(languages, indent=2))
返回结果
参数:
languages (结果数组)
language (检测语言缩写)
name (语言名称)
{
"languages": [
{
"language": "af",
"name": "Afrikaans"
},
{
"language": "ar",
"name": "Arabic"
},
{
"language": "az",
"name": "Azerbaijani"
},
{
"language": "ba",
"name": "Bashkir"
},
//...
]
}
识别语言(Identity Language)
识别文本语言
参数:
text (string型,UTF-8格式输入文本)
import json
from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator
language_translator = LanguageTranslator(
username='{username}',
password='{password}')
language = language_translator.identify('this is a test')
print(json.dumps(language, indent=2))
返回结果:
参数:
languages (结果数组)
language (文本语言-翻译语言)
confidence (成功翻译概率)
{
"languages": [
{
"confidence": 0.9143,
"language": "en-US"
},
{
"confidence": 0.0396,
"language": "hu-HU"
},
{
"confidence": 0.0093,
"language": "ro-RO"
},
{
"confidence": 0.005,
"language": "nl-NL"
},
//...
]
}
模型列表(List Models)
根据源语言或目标语言筛选出可靠的翻译模型
参数:
default (string型,设置为true返回默认模型,false返回非默认模型,不设置,都返回)
source (string型,按照源语言筛选)
target (string型,按照目标语言筛选)
import json
from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator
language_translator = LanguageTranslator(
username='{username}',
password='{password}')
models = language_translator.get_models()
print(json.dumps(models, indent=2))
返回结果:
参数:
model_id (使用模型)
source (源语言)
target (目标语言)
base_model_id (使用模型的基本模型)
customizable (该模型是否可以被用于训练其他模型,已定制过的将不被允许再次定义)
owner (模型拥有者,IBM或用户)
status (模型可用性)
default_model (该模型是否可以默认)
domain (该模型域名)
name (如果模型由用户训练,可以给它命名以便区分)
{
models: [{
"model_id ": "3e7dfdbe-f757-4150-afee-458e71eb93fb",
"source": "en",
"target": "es",
"base_model_id": "en-es",
"customizable": "false",
"default": "false",
"domain": "travel",
"owner": "74a239fa-f2db-42ef-8737-7bf74f49e35f",
"status": "available",
"name": "mymodel"
}]
}
文档原文地址:http://www.ibm.com/watson/dev...
自然语言分类(Natural Language Classifier)
该服务使用机器学习算法返回对于输入文本而言最为匹配的预定义类,你也可以创建训练一个分类器用以连接样本文本和预定义类,这样就可以将服务应用于新的输入中
认证
在bluemix上创建该服务实例,并且获得用户名和密码,详见Watson使用指南。
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')
方法
创建分类器(Create Classifier)
发送数据以获得和训练一个分类器,同时返回关于该分类器的信息。注意,操作成功时返回标志“Training”,只有当标志为“Available”时才可以使用
参数:
name (string型, 该分类器名字)
language (string型, 必须, 训练数据语言)
training_data (文件型, 必须, 训练数据)
import json
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')
with open('../resources/weather_data_train.csv', 'rb') as training_data:
classifier = natural_language_classifier.create(
training_data=training_data,
name='My Classfier',
language='en'
)
print(json.dumps(classifier, indent=2))
返回结果
参数:
classifier_id (独一的ID序号)
name (用户定义分类器名称)
language (训练分类器使用语言)
created (创建时间)
url (分类器地址)
status (当前分类器状态)
status_description (当前状态描述)
{
"classifier_id": "10D41B-nlc-1",
"name": "My Classifier",
"language": "en"
"created": "2015-05-28T18:01:57.393Z",
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/10D41B-nlc-1",
"status": "Training",
"status_description": "The classifier instance is in its training phase, not yet ready to accept classify requests"
}
分类器列表(List Classifier)
检索适用于服务实例的分类器,如没有可用的,返回空值
参数:
无参数
import json
from watson_developer_cloud import NaturalLanguageClassifierV1 as NaturalLanguageClassifier
natural_language_classifier = NaturalLanguageClassifier(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')
classifiers = natural_language_classifier.list()
print(json.dumps(classifiers, indent=2))
返回结果
参数:
classifier (结果数组)
classifier_id (分类器ID)
url (分类器地址)
name (分类器名称)
language (分类器使用语言)
created (分类器创建时间)
{
"classifiers": [
{
"classifier_id": "10D41B-nlc-1",
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/10D41B-nlc-1",
"name": "My Classifier",
"language": "en",
"created": "2015-05-28T18:01:57.393Z"
}
]
}
*获取更多信息(Get information about a classifier)
返回关于分类器的更多信息
参数:
classifier_id (string型,必须,查询分类器ID)
import json
from watson_developer_cloud import NaturalLanguageClassifierV1 as NaturalLanguageClassifier
natural_language_classifier = NaturalLanguageClassifier(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')
status = natural_language_classifier.status('10D41B-nlc-1')
print (json.dumps(status, indent=2))
返回结果:
参数:
classifier_id (独一的ID序号)
name (用户定义分类器名称)
language (训练分类器使用语言)
created (创建时间)
url (分类器地址)
status (当前分类器状态)
status_description (当前状态描述)
{
"classifier_id": "10D41B-nlc-1",
"name": "My Classifier",
"language": "en",
"created": "2015-05-28T18:01:57.393Z",
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/10D41B-nlc-1",
"status": "Available",
"status_description": "The classifier instance is now available and is ready to take classifier requests.",
}
删除(Delete Classifier)
参数:
classifier_id (string型, 必须,要删除分类器)
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')
classes = natural_language_classifier.remove('10D41B-nlc-1')
print(json.dumps(classes, indent=2))
返回结果
{}
分类(Classify)
返回输入文本的标签
参数:
classifier_id (string型, 必须,所使用分类器)
text (string型,必须,待分类文本)
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')
classes = natural_language_classifier.classify('10D41B-nlc-1', 'How hot will it be today?')
print(json.dumps(classes, indent=2))
返回结果
参数:
classifier_id (分类器ID)
url (分类器地址)
text (待分类文本)
top_class (匹配度最高类别)
-
classes (类别信息)
class_name (类别标签)
confidence (得分,分数越高匹配程度越大)
{
"classifier_id": "10D41B-nlc-1",
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/10D41B-nlc-1/classify?text=How%20hot%20wil/10D41B-nlc-1",
"text": "How hot will it be today?",
"top_class": "temperature",
"classes": [
{
"class_name": "temperature",
"confidence": 0.9998201258549781
},
{
"class_name": "conditions",
"confidence": 0.00017987414502176904
}
]
}