Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. def detect_language(*documents):
  2. '''Returns language detected for each string in documents (*args)'''
  3.  
  4. params = urllib.parse.urlencode({})
  5. headers = {
  6. 'Content-Type': 'application/json',
  7. 'Ocp-Apim-Subscription-Key': subscriptions['TextAnalytics']}
  8.  
  9. # redefine body for proper text format
  10. body={"documents": []}
  11. ID = 0
  12. for document in documents:
  13. doc = {
  14. # assign unique idea
  15. "id": ID,
  16. "text": "'{}'".format(document)
  17. }
  18. body['documents'].append(doc)
  19. ID+= 1
  20. body = json.dumps(body)
  21.  
  22. r = send_request('/text/analytics/v2.0/languages', params=params, headers=headers, body=body)
  23. return [i['detectedLanguages'][0]['name'] for i in r['documents']]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement