Dear forum,
My psychopy experiment has a function which sends text to a machine translation API and procures a result. The python code uses requests.post and an authorization key. The response comes as a json file.
I understand that the function needs to be translated correctly into JS. The Psychopy converter gave it a good try but it still doesn’t work. I am not good with JS and it’s hard to ask someone to help because this is not just a JS issue, it’s a specific psychoJS issue as far as I can see.
Could someone please help me understand what I need to use instead of requests. How do I import json?
Thank you very much in advance!
Anastassia
import requests
import json
def use_mt(inputstring):
source_lang = "fr"
target_lang = "de"
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
params = (
('auth_key', 'XXX'),
)
textdata = '{ "segments": [ { "text": "'+str(inputstring)+'" } ], "source_language": "'+str(source_lang)+'", "target_language": "de", "split_sentences": false, "politeness": "auto" }'
response = requests.post('https://try.XXX.ai/api/v2/translate', headers=headers, params=params, data=textdata.encode('utf8'))
if response.ok:
dict = json.loads(my_response)
raw_output = dict['segments'][0]['text']