small translation test working

This commit is contained in:
Tomas Kucharik 2022-02-20 23:04:51 +01:00
parent 0e76056e0d
commit 5edc0bef7f
5 changed files with 4517 additions and 4 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
google_api_key.json
google-cloud-sdk/

File diff suppressed because it is too large Load Diff

View File

@ -1,2 +1,3 @@
python-dotenv==0.19.1
google-cloud-translate==2.0.1
tqdm==4.62.3

View File

@ -1,7 +1,9 @@
import json
from dotenv import load_dotenv
from tqdm import tqdm
from squad_utils import print_squad
from translate_utils import translate_text
def sort_qas_by_answer_index(squad):
@ -69,6 +71,13 @@ def add_special_chars_to_paragraph(paragraph):
other['answer_end'] += len(special_char) +1
def translate_paragraphs(squad):
for article in tqdm(squad["data"]):
for paragraph in article["paragraphs"]:
translated = translate_text(paragraph["context"])
paragraph['translatedContext'] = translated
if __name__ == "__main__":
load_dotenv()
@ -77,7 +86,7 @@ if __name__ == "__main__":
sort_qas_by_answer_index(squad)
transform_squad(squad)
print_squad(squad)
translate_paragraphs(squad)
with open("./data/squad-v2-dev-small-translated.json", "w") as f:
json.dump(squad, f, indent=2)

View File

@ -21,7 +21,26 @@ def translate_text(text):
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language="sk")
print(u"Text: {}".format(result["input"]))
print(u"Translation: {}".format(result["translatedText"]))
print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))
# print(u"Text: {}".format(result["input"]))
# print(u"Translation: {}".format(result["translatedText"]))
# print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))
return result['translatedText']
def translate_test():
try:
translate_text("Hello")
return True
except Exception as e:
print(e)
return False
if __name__ == "__main__":
load_dotenv()
if translate_test():
print("Translate keys work")