monkeylearapi的官方python客户端
monkeylearn的Python项目详细描述
用于python的monkeylearn api
MonkeyLearn API的官方Python客户端。从您的python应用程序构建并运行用于语言处理的机器学习模型。
安装
您可以使用pip安装库:
$ pip install monkeylearn
或者,您可以克隆存储库并运行setup.py脚本:
$ python setup.py install
用法
在向api发出请求之前,需要创建monkeylearn客户机的实例。您必须使用您的帐户api键:
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')
请求
从monkeylern客户机实例中,您可以调用任何端点(检查下面的可用端点)。例如,您可以使用公共的情感分析分类器对文本列表进行分类。
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])
响应
每个端点调用返回的响应对象是一个monkeylearnResponse
对象。body
属性具有来自api的解析响应:
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]
您还可以访问response对象中的其他属性,以获取有关已使用或可用查询的信息:
print(response.plan_queries_allowed)# => 300print(response.plan_queries_remaining)# => 240print(response.request_queries_used)# => 2
错误
端点调用可能引发异常。下面是一个如何处理它们的示例:
frommonkeylearn.exceptionsimportPlanQueryLimitError,MonkeyLearnExceptiontry:response=ml.classifiers.classify('[MODEL_ID]',data=['My text'])exceptPlanQueryLimitErrorase:# No monthly queries left# e.response contains the MonkeyLearnResponse objectprint(e.error_code,e.detail)exceptMonkeyLearnException:raise
可用例外:
<表><广告>monkeylearnexception
请求参数错误
身份验证错误
禁止错误
型号限制错误
未找到型号
型号ID
标记未找到
标记id
参数。planqueryliemiterror
平面速率限制错误
concurrencyRateLimitorRor
模型状态错误
自动批处理
classify和extract端点可能需要向MonkeyLearn API发出多个请求,以便处理数据中的每个文本。如果
auto_batch
参数为true
(这是默认值),则不必将数据的长度保持在最大允许值(200)以下。你可以把所有的信息都传过去t和库将处理批处理并提出必要的请求。如果
retry\u if\u throttled
参数为true
(这是默认值),则如果API限制了请求,它也将等待并重试。
假设您发送一个包含300个文本的数据
参数,并且启用了自动批处理
。该列表将在内部进行拆分,两个请求将分别发送给Monkeylearn 200和100条文本。如果所有请求都以200状态代码响应,则响应将被追加,您将在monkeylearnresponse.body
属性中获得300个分类
data=['Text to classify']*300response=ml.classifiers.classify('[MODEL_ID]',data)assertlen(response.body)==300# => True
现在,假设您在尝试上一个示例时只剩下200个查询,则第二个内部请求将失败,因为您在第一批之后不会留下查询,并且会引发planQueryMimiterror
异常。前200个(成功)分类将位于异常对象中。但是,如果不使用except
子句管理此异常,则前200个成功的分类将丢失。你应该如何处理这个案子:
frommonkeylearn.exceptionsimportPlanQueryLimitErrordata=['Text to classify']*300batch_size=200try:response=ml.classifiers.classify('[MODEL_ID]',data,batch_size=batch_size)exceptPlanQueryLimitErrorase:partial_predictions=e.response.body# The body of the successful responsesnon_2xx_raw_responses=r.response.failed_raw_responses# List of requests responses objectselse:predictions=response.body
这很方便,通常应该足够了。如果您需要更大的灵活性,您可以自行管理批处理和速率限制。
$ pip install monkeylearn0
这样,您就可以控制发送到MonkeyLearn API的每个请求。
可用端点
这些都是api的端点。有关每个端点的更多信息,请查看api文档
分类器
分类
$ pip install monkeylearn1
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
列表[str或dict]
文本
键和文本作为值的dict。您可以提供一个可选的外部id
键和一个包含在响应中的字符串。bool
int
bool
数据
列表拆分为较小的有效列表,将每个列表以单独的请求发送到monkeylern,并合并响应。bool
示例:
$ pip install monkeylearn2 < BR>
分类器详细信息
$ pip install monkeylearn3
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
bool
示例:
$ pip install monkeylearn4 < BR>
创建分类器
$ pip install monkeylearn5
参数:
<表><广告>str
str
str
str
int
元组(int,int)
bool
bool
bool
bool
bool或list
列表
bool
示例:
$ pip install monkeylearn6 < BR>
编辑分类器
$ pip install monkeylearn7
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
str
str
str
str
int
元组(int,int)
bool
bool
bool
bool
bool或list
列表
bool
示例:
$ pip install monkeylearn8 < BR>
删除分类器
$ pip install monkeylearn9
参数:
<表><广告>str
'cl_ojnmkt2v'
bool
示例:
$ python setup.py install0 < BR>
列出分类器
$ python setup.py install1
参数:
<表><广告>int
int
字符串或列表
-
(短划线)字符之前。一些有效的例子是:'is_public'
,'-name'
或['-is_public',name']
bool
示例:
$ python setup.py install2 < BR>
部署
$ python setup.py install3
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
bool
示例:
$ python setup.py install4 < BR>
列车
$ python setup.py install5
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
bool
示例:
$ python setup.py install6 < BR>
标记详细信息
$ python setup.py install7
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
int
bool
示例:
$ python setup.py install8 < BR>
创建标记
$ python setup.py install9
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
str
。int
bool
示例:
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')0 < BR>
编辑标记
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')1
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
int
str
int
bool
示例:
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')2 < BR>
删除标记
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')3
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
int
int
bool
示例:
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')4 < BR>
上传数据
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')5
参数:
<表><广告>str
'cl'
开头,例如,'cl_ojnmkt2v'
列表[dict]
str
merge
、keep_first
或keep_last
str
覆盖
或忽略
bool
数据
听写键:
示例:
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')6 < BR>
提取器
提取
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')7
参数:
<表><广告>str
'ex'
开头,例如,'ex_ojnmkt2v'
列表[str或dict]
文本
键和文本作为值的dict。您还可以提供一个可选的外部id
键和一个将包含在响应中的字符串。bool
int
bool
示例:
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')8 < BR>
抽壳机详图
frommonkeylearnimportMonkeyLearn# Instantiate the client Using your API keyml=MonkeyLearn('<YOUR API TOKEN HERE>')9
参数:
<表><广告>str
'ex'
开头,例如,'ex_ojnmkt2v'
bool
示例:
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])0 < BR>
列出提取器
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])1
参数:
<表><广告>int
int
字符串或列表
-
(短划线)字符之前。一些有效的例子是:'is_public'
,'-name'
或['-is_public',name']
bool
示例:
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])2
工作流程
工作流详细信息
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])
3
参数:
<表><广告>str
'wf'
开头,例如,'wf懔ojnmkt2v'
int
bool
示例:
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])4 < BR>
创建工作流
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])
5
参数:
<表><广告>str
str
列表[dict]
str
str
[]
文本
字段。{}
示例:
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])6 < BR>
删除工作流
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])
7
参数:
<表><广告>str
'wf'
开头,例如,'wf懔ojnmkt2v'
bool
示例:
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])8 < BR>
步骤详细信息
response=ml.classifiers.classify(model_id='cl_Jx8qzYJh',data=['Great hotel with excellent location','This is the worst hotel ever.'])
9
参数:
<表><广告>str
'wf'
开头,例如,'wf懔ojnmkt2v'
int
bool
示例:
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]0 < BR>
创建步骤
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]
1
参数:
<表><广告>str
'wf'
开头,例如,'wf懔ojnmkt2v'
str
str
str
输入数据
(默认),这意味着输入将是原始文本。列表[dict]
bool
示例:
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]2 < BR>
删除步骤
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]
3
参数:
<表><广告>str
'wf'
开头,例如,'wf懔ojnmkt2v'
int
bool
示例:
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]4 < BR>
上传工作流数据
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]
5
参数:
<表><广告>str
'wf'
开头,例如,'wf懔ojnmkt2v'
列表[dict]
bool
数据
听写键:
示例:
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]6 < BR>
列出工作流数据
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]
7
参数:
<表><广告>int
int
50
int
bool
str
str
示例:
print(response.body)# => [# => {# => 'text': 'Great hotel with excellent location',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Positive',# => 'tag_id': 1994,# => 'confidence': 0.922,# => }# => ]# => },# => {# => 'text': 'This is the worst hotel ever.',# => 'external_id': null,# => 'error': false,# => 'classifications': [# => {# => 'tag_name': 'Negative',# => 'tag_id': 1941,# => 'confidence': 0.911,# => }# => ]# => }# => ]8 < BR>
创建自定义字段 αααα59
参数:
<表><广告>str
'wf'
开头,例如,'wf懔ojnmkt2v'
str
str
字符串
,日期
,文本
,整数
,浮点
,布尔
bool
示例:
print(response.plan_queries_allowed)# => 300print(response.plan_queries_remaining)# => 240print(response.request_queries_used)# => 20