为什么Azure Content Moderator无法检测自定义术语?

时间:2019-05-06 09:09:10

标签: python azure azure-cognitive-services

我正在尝试使用Azure认知服务(更具体地说是azure-cognitiveservices-vision-contentmoderator==1.0.0)来检测文本块中的custom个标记的关键字。

代码运行如下:

from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
from msrest.authentication import CognitiveServicesCredentials

subscription_key = '<my_key>'
endpoint_url = 'https://westeurope.api.cognitive.microsoft.com/'

client = ContentModeratorClient(endpoint_url, CognitiveServicesCredentials(subscription_key))

之后,我还可以创建自定义字词列表(名为123)(使用方法client.list_management_term_lists.createclient.list_management_term.add_term

client.list_management_term_lists.create(
        content_type="application/json",
        body={
            "name": "My custom list",
            "description": "Monty Python related terms",
        }
)

client.list_management_term.add_term(
        list_id=123,
        term="eggs",
        language="eng"
)

并且我可以验证它是否按预期工作

terms_data = client.list_management_term.get_all_terms(list_id=123, language="eng").data
terms_data.as_dict()

收益

  

{'language':'eng','terms':[{'term':'eggs'},{'term':'spam'}],   '状态':{'代码':3000,'描述':'确定'},'tracking_id':   'some_id'}

但是,如果我尝试使用

进行检测
import io

TEXT = "Do you like spam and eggs ?"

text_screen = client.text_moderation.screen_text(
    text_content_type="text/plain",
    text_content=io.StringIO(TEXT),
    language="eng",
    list_id=123,
    classify=True
)

text_screen.as_dict()

没有'Terms'实体出现,因为我从上面得到的只是:

{'original_text': 'Do you like spam and eggs ?',
 'normalized_text': ' you like spam  eggs ?',
 'classification': {'category1': {'score': 0.028309470042586327},
  'category2': {'score': 0.14004242420196533},
  'category3': {'score': 0.12679287791252136},
  'review_recommended': False},
 'status': {'code': 3000, 'description': 'OK'},
 'language': 'eng'}

我做错了什么,应该如何正确做?

也(不确定是否相关),开火

client.list_management_term_lists.refresh_index_method(list_id=123, language="eng")

给我一​​个APIErrorException: Operation returned an invalid status code 'Not Found'

0 个答案:

没有答案