2017-01-30 3 views
0

Ich versuche Azure Textanalyse-Tool zu verwenden Themen zu extrahieren, aber die 400 Bad Anfrage Fehler bekommen: Mein Code unten:Azure Cognitive Textanalyse-Tool liefert 400 Bad Anfrage 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, ‚Bad Request ')

Was mache ich da falsch?

Danke!

Antwort

1

Ich habe versucht, Ihr Problem zu reproduzieren, und ich entdeckte, dass der 400 Bad Request Fehler für Ihren Code wie folgt verursacht wurde, wenn Sie print(r.text) verwenden.

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}}'

Es zeigt auch in der offiziellen Tutorial Task 3 - Detect topics in a corpus of text, wie unten.

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.

Bitte fügen Sie also genügend Textdatensätze für die Verwendung der API hinzu.

0

Sie sollten nicht str(tufani) verwenden, um Daten für die POST-Anforderung zu kodieren, anstelle requests wird automatically encode dict data für Sie:

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

P. S. Wenn der Server JSON-codierte POST/PATCH-Daten akzeptiert, können Sie json.dumps(payload) verwenden.

0

Die von Shane und Peter vorgeschlagene Lösung sind die Fehler, die ich vermisste, und diese Vorschläge lösten das Problem.

Danke!

Verwandte Themen