2017-01-30 39 views
0

我想使用Azure的文本分析工具來提取主題,但得到的400錯誤請求錯誤:我下面的代碼:Azure的認知文本分析工具返回400錯誤的請求的Python

account_key = '546e6162da424e6f991d03b7f6acxxx' 
headers = { 
'Ocp-Apim-Subscription-Key': account_key, 
'Content-Type': 'application/json', 
'Accept': 'application/json'} 

import requests 

tufani={ 
    "documents": [ 
     { 
      "language": "en", 
      "id": "1", 
      "text": "First document" 
     }, 
     { 
      "language": "en", 
      "id": "100", 
      "text": "Final document" 
     } 
    ] 
} 

print('Starting topic detection.') 
uri = base_url + 'text/analytics/v2.0/topics' 
r=requests.post('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/topics',data=str(tufani),headers =headers) 

print(r.status_code, r.reason) 

(400,「壞請求')

我在這做錯了什麼?

謝謝!

回答

1

我試圖重現您的問題,並且如果使用print(r.text),我發現您的代碼的400 Bad Request錯誤是由以下原因造成的。

u'{"code":"BadRequest","message":"Invalid request","innerError":{"code":"InvalidRequestContent","message":"Requests to this API should contain at least 100 documents, where each document is not null or empty","minimumNumberOfDocuments":100}}'

它也顯示在官方教程Task 3 - Detect topics in a corpus of text,如下所示。

This API requires a minimum of 100 text records to be submitted, but is designed to detect topics across hundreds to thousands of records. Any non-English records or records with less than 3 words will be discarded and therefore will not be assigned to topics. For topic detection, the maximum size of a single document that can be submitted is 30KB, and the total maximum size of submitted input is 30MB. Topic detection is rate limited to 5 submissions every 5 minutes.

所以請爲使用API​​添加足夠的文本記錄。

0

你不應該使用str(tufani)爲您編碼數據的POST請求,而不是requestsautomatically encode dict data

r=requests.post('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/topics',data=tufani,headers =headers) 

附:如果服務器接受JSON編碼的POST/PATCH數據,則可以使用json.dumps(payload)來完成這項工作。

0

Shane和Peter提出的解決方案都是我錯過的錯誤,這些建議解決了這個問題。

謝謝!

相關問題