Created
August 31, 2022 11:09
-
-
Save mrtushartiwari/4b3afee38edab1552843d4ceb3e88193 to your computer and use it in GitHub Desktop.
Libre_translate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
API_URL = "http://127.0.0.1:5000/translate" | |
# Openning and reading a file with sample English Sentences. | |
try: | |
file1 = open('sentence.txt', 'r') | |
lines = file1.readlines() | |
except: | |
print("No such file exists") | |
params = { | |
'q': "India wins the world cup after 28 years", | |
'source': "en", | |
'target': "hi", | |
'format': "text", | |
'api_key': "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
} | |
# Creates a new files and writes the hindi Translation line by line | |
file2 = open('hindi_translation.txt','w') | |
for l in lines: | |
print(l) | |
params['q'] = str(l) | |
r=requests.post(url = API_URL, data = params) | |
translated_text = r.json(encoding='utf-8') | |
file2.write(translated_text['translatedText']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment