From f3ce98b18377b2f82155ea9624c379d3175d8980 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 25 Jan 2024 14:40:05 +0100 Subject: [PATCH 01/21] add test and change parameters --- tests/test_translation_level/test_translation_level_01.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_translation_level/test_translation_level_01.py b/tests/test_translation_level/test_translation_level_01.py index 81b7adf0f00..f1a44ec9c47 100644 --- a/tests/test_translation_level/test_translation_level_01.py +++ b/tests/test_translation_level/test_translation_level_01.py @@ -25,6 +25,14 @@ def test_print_english_dutch(self): self.assertEqual(expected, result) + def test_print_english_dutch_including_string(self): + code = 'print Hello welcome to Hedy!' + + result = hedy_translation.translate_keywords(code, from_lang="en", to_lang="nl", level=self.level, translate_strings=True) + expected = 'print Hallo welkom bij Hedy!' + + self.assertEqual(expected, result) + def test_ask_english_dutch(self): code = "ask Hallo welkom bij Hedy!" From 8b8c8559999f323690926c1b8320f203f9b9cdee Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 25 Jan 2024 14:40:10 +0100 Subject: [PATCH 02/21] add rule --- hedy_translation.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index f446feb2be8..ef45392f552 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -73,7 +73,7 @@ def get_target_keyword(keyword_dict, keyword): return keyword -def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1): +def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, translate_strings=False): """ "Return code with keywords translated to language of choice in level of choice""" if input_string == "": @@ -216,6 +216,13 @@ def returns(self, tree): def print(self, tree): self.add_rule("_PRINT", "print", tree) + # in addition to keywords, we atr now also adding plain text strings + # like print arguments to the list of things that need to be translated + + argument = str(tree.children[1].children[0]) + self.add_rule("text1", argument, tree) # this of course only support 1 string + # you will have to introduce a counter in the class that increases when we see a string + def print_empty_brackets(self, tree): self.print(tree) @@ -360,9 +367,15 @@ def add_rule(self, token_name, token_keyword, tree): token_keyword, token.line, token.column - 1, token.end_column - 2, token.value ) self.rules.append(rule) + elif token_name[:4] == "text": # this is not superduper pretty but for now it works! + + rule = Rule( + token_name, tree.line, tree.column - 1, tree.end_column - 2, "value" + ) + self.rules.append(rule) - def get_keyword_token(self, token_type, node): - for c in node.children: + def get_keyword_token(self, token_type, tree): + for c in tree.children: if type(c) is Token and c.type == token_type: return c return None From 8f2af2a9a414f58296b4eb6f91bbf042d3c78569 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:48:47 +0000 Subject: [PATCH 03/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hedy_translation.py | 4 ++-- tests/test_translation_level/test_translation_level_01.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index ef45392f552..5bc5a5ad640 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -220,7 +220,7 @@ def print(self, tree): # like print arguments to the list of things that need to be translated argument = str(tree.children[1].children[0]) - self.add_rule("text1", argument, tree) # this of course only support 1 string + self.add_rule("text1", argument, tree) # this of course only support 1 string # you will have to introduce a counter in the class that increases when we see a string def print_empty_brackets(self, tree): @@ -367,7 +367,7 @@ def add_rule(self, token_name, token_keyword, tree): token_keyword, token.line, token.column - 1, token.end_column - 2, token.value ) self.rules.append(rule) - elif token_name[:4] == "text": # this is not superduper pretty but for now it works! + elif token_name[:4] == "text": # this is not superduper pretty but for now it works! rule = Rule( token_name, tree.line, tree.column - 1, tree.end_column - 2, "value" diff --git a/tests/test_translation_level/test_translation_level_01.py b/tests/test_translation_level/test_translation_level_01.py index f1a44ec9c47..44f0293f19f 100644 --- a/tests/test_translation_level/test_translation_level_01.py +++ b/tests/test_translation_level/test_translation_level_01.py @@ -28,7 +28,8 @@ def test_print_english_dutch(self): def test_print_english_dutch_including_string(self): code = 'print Hello welcome to Hedy!' - result = hedy_translation.translate_keywords(code, from_lang="en", to_lang="nl", level=self.level, translate_strings=True) + result = hedy_translation.translate_keywords( + code, from_lang="en", to_lang="nl", level=self.level, translate_strings=True) expected = 'print Hallo welkom bij Hedy!' self.assertEqual(expected, result) From 0eaa5c6d3cecac9555b5da2b710c01a992fbbde9 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 25 Jan 2024 14:56:44 +0100 Subject: [PATCH 04/21] add rule and text token --- hedy_translation.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index ef45392f552..b2b9aa19776 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -1,5 +1,5 @@ from collections import namedtuple -from lark import Token, Visitor +from lark import Token, Visitor, Tree from lark.exceptions import VisitError import hedy import operator @@ -361,23 +361,27 @@ def pressed(self, tree): self.add_rule("_PRESSED", "pressed", tree) def add_rule(self, token_name, token_keyword, tree): - token = self.get_keyword_token(token_name, tree) - if token: + if token_name[:4] == "text": # this is not superduper pretty but for now it works! + token = self.get_keyword_token('text', tree) rule = Rule( - token_keyword, token.line, token.column - 1, token.end_column - 2, token.value + token_name, tree.line, tree.column - 1, tree.end_column - 2, token.value ) self.rules.append(rule) - elif token_name[:4] == "text": # this is not superduper pretty but for now it works! + else: + token = self.get_keyword_token(token_name, tree) + if token: + rule = Rule( + token_keyword, token.line, token.column - 1, token.end_column - 2, token.value + ) + self.rules.append(rule) - rule = Rule( - token_name, tree.line, tree.column - 1, tree.end_column - 2, "value" - ) - self.rules.append(rule) def get_keyword_token(self, token_type, tree): for c in tree.children: if type(c) is Token and c.type == token_type: return c + if type(c) is Tree and c.data == token_type: + return c.children[0] return None def get_keyword_tokens(self, token_type, node): From 53cc8861979fc2d4b4716f36a02495378a56d864 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:58:24 +0000 Subject: [PATCH 05/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hedy_translation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index b2b9aa19776..5c258576c05 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -220,7 +220,7 @@ def print(self, tree): # like print arguments to the list of things that need to be translated argument = str(tree.children[1].children[0]) - self.add_rule("text1", argument, tree) # this of course only support 1 string + self.add_rule("text1", argument, tree) # this of course only support 1 string # you will have to introduce a counter in the class that increases when we see a string def print_empty_brackets(self, tree): @@ -375,7 +375,6 @@ def add_rule(self, token_name, token_keyword, tree): ) self.rules.append(rule) - def get_keyword_token(self, token_type, tree): for c in tree.children: if type(c) is Token and c.type == token_type: From 2f7aeff8d607078942a748f6a85b78ef094c2b35 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 25 Jan 2024 14:59:30 +0100 Subject: [PATCH 06/21] update token --- hedy_translation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hedy_translation.py b/hedy_translation.py index b2b9aa19776..dae78a9e51b 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -112,6 +112,8 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran target = get_target_keyword(keyword_dict_to, rule.keyword) replaced_line = replace_token_in_line(line, rule, original, target) result = replace_line(lines, rule.line - 1, replaced_line) + else: + # this is a text string that needs to be translated # For now the needed post processing is only removing the 'end-block's added during pre-processing result = "\n".join([line for line in result.splitlines()]) @@ -364,7 +366,7 @@ def add_rule(self, token_name, token_keyword, tree): if token_name[:4] == "text": # this is not superduper pretty but for now it works! token = self.get_keyword_token('text', tree) rule = Rule( - token_name, tree.line, tree.column - 1, tree.end_column - 2, token.value + token_name, token.line, token.column - 1, token.end_column, token.value ) self.rules.append(rule) else: From e2b5b713f2bbc6c7181622440a6a3b9752f9cb24 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:00:24 +0000 Subject: [PATCH 07/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hedy_translation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hedy_translation.py b/hedy_translation.py index 510b856eb5b..3a4f5d4d13a 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -115,7 +115,7 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran else: # this is a text string that needs to be translated - # For now the needed post processing is only removing the 'end-block's added during pre-processing + # For now the needed post processing is only removing the 'end-block's added during pre-processing result = "\n".join([line for line in result.splitlines()]) result = result.replace("#ENDBLOCK", "") From 68db33f6d362972fc3b32e806614a27640986918 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 25 Jan 2024 15:02:52 +0100 Subject: [PATCH 08/21] call fake api --- hedy_translation.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hedy_translation.py b/hedy_translation.py index 510b856eb5b..cfe99da780d 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -72,6 +72,8 @@ def get_target_keyword(keyword_dict, keyword): else: return keyword +def translate(string): + return 'Hallo welkom bij Hedy!' # clearly we need to call a real translation api here! def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, translate_strings=False): """ "Return code with keywords translated to language of choice in level of choice""" @@ -114,6 +116,12 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran result = replace_line(lines, rule.line - 1, replaced_line) else: # this is a text string that needs to be translated + lines = result.splitlines() # do we need to do this for each rule?? + line = lines[rule.line - 1] + original = rule.value + target = translate(original) + replaced_line = replace_token_in_line(line, rule, original, target) + result = replace_line(lines, rule.line - 1, replaced_line) # For now the needed post processing is only removing the 'end-block's added during pre-processing result = "\n".join([line for line in result.splitlines()]) From d30581dd7d8a2adc8fb56fadf19b66625ca8a140 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:04:00 +0000 Subject: [PATCH 09/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hedy_translation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hedy_translation.py b/hedy_translation.py index 97c81b58334..e7b63e73736 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -72,9 +72,11 @@ def get_target_keyword(keyword_dict, keyword): else: return keyword + def translate(string): return 'Hallo welkom bij Hedy!' # clearly we need to call a real translation api here! + def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, translate_strings=False): """ "Return code with keywords translated to language of choice in level of choice""" @@ -116,7 +118,7 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran result = replace_line(lines, rule.line - 1, replaced_line) else: # this is a text string that needs to be translated - lines = result.splitlines() # do we need to do this for each rule?? + lines = result.splitlines() # do we need to do this for each rule?? line = lines[rule.line - 1] original = rule.value target = translate(original) From 7e054c4db65b55855952e0105c85ff411355b4dc Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 25 Jan 2024 15:05:26 +0100 Subject: [PATCH 10/21] use parameter --- hedy_translation.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index 97c81b58334..9ef2337227c 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -115,13 +115,14 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran replaced_line = replace_token_in_line(line, rule, original, target) result = replace_line(lines, rule.line - 1, replaced_line) else: - # this is a text string that needs to be translated - lines = result.splitlines() # do we need to do this for each rule?? - line = lines[rule.line - 1] - original = rule.value - target = translate(original) - replaced_line = replace_token_in_line(line, rule, original, target) - result = replace_line(lines, rule.line - 1, replaced_line) + if translate_strings: + # this is a text string that needs to be translated + lines = result.splitlines() # do we need to do this for each rule?? + line = lines[rule.line - 1] + original = rule.value + target = translate(original) + replaced_line = replace_token_in_line(line, rule, original, target) + result = replace_line(lines, rule.line - 1, replaced_line) # For now the needed post processing is only removing the 'end-block's added during pre-processing result = "\n".join([line for line in result.splitlines()]) From 051e55e9d642b2d174608f12f019cc1419e84543 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:07:19 +0000 Subject: [PATCH 11/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hedy_translation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hedy_translation.py b/hedy_translation.py index fa758eece53..cda139f7ea5 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -119,7 +119,7 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran else: if translate_strings: # this is a text string that needs to be translated - lines = result.splitlines() # do we need to do this for each rule?? + lines = result.splitlines() # do we need to do this for each rule?? line = lines[rule.line - 1] original = rule.value target = translate(original) From e6bd89d5833881ac99cf483570e72e7740df19e5 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 25 Jan 2024 15:51:14 +0100 Subject: [PATCH 12/21] skip Nones --- hedy_translation.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index fa758eece53..c07ea9c6e4a 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -376,10 +376,11 @@ def pressed(self, tree): def add_rule(self, token_name, token_keyword, tree): if token_name[:4] == "text": # this is not superduper pretty but for now it works! token = self.get_keyword_token('text', tree) - rule = Rule( - token_name, token.line, token.column - 1, token.end_column, token.value - ) - self.rules.append(rule) + if token: + rule = Rule( + token_name, token.line, token.column - 1, token.end_column, token.value + ) + self.rules.append(rule) else: token = self.get_keyword_token(token_name, tree) if token: From b8ce7b97d3b14f7670838f0f9b0fe510b358c7b5 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Fri, 26 Jan 2024 11:38:01 +0100 Subject: [PATCH 13/21] only add strings if parameter is set --- hedy_translation.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index f78b03cd0b2..00ce6cfc926 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -100,7 +100,7 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran program_root = parser.parse(processed_input + "\n").children[0] - translator = Translator(processed_input) + translator = Translator(processed_input, translate_strings) translator.visit(program_root) ordered_rules = reversed(sorted(translator.rules, key=operator.attrgetter("line", "start"))) @@ -207,9 +207,10 @@ class Translator(Visitor): in the user input string and original value. The information is later used to replace the token in the original user input with the translated token value.""" - def __init__(self, input_string): + def __init__(self, input_string, translate_strings=False): self.input_string = input_string self.rules = [] + self.translate_strings = translate_strings def define(self, tree): self.add_rule("_DEFINE", "define", tree) @@ -229,12 +230,13 @@ def returns(self, tree): def print(self, tree): self.add_rule("_PRINT", "print", tree) - # in addition to keywords, we atr now also adding plain text strings - # like print arguments to the list of things that need to be translated - - argument = str(tree.children[1].children[0]) - self.add_rule("text1", argument, tree) # this of course only support 1 string - # you will have to introduce a counter in the class that increases when we see a string + if self.translate_strings: + # in addition to keywords, we atr now also adding plain text strings + # like print arguments to the list of things that need to be translated + if len(tree.children) > 1: + argument = str(tree.children[1].children[0]) + self.add_rule("text1", argument, tree) # this of course only support 1 string + # you will have to introduce a counter in the class that increases when we see a string def print_empty_brackets(self, tree): self.print(tree) From 8a15c636b3e65d400b34a6c2f9d8e96ce9733220 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Fri, 3 May 2024 17:44:59 +0200 Subject: [PATCH 14/21] call google api --- hedy_translation.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hedy_translation.py b/hedy_translation.py index 00ce6cfc926..de28068de68 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -74,7 +74,11 @@ def get_target_keyword(keyword_dict, keyword): def translate(string): - return 'Hallo welkom bij Hedy!' # clearly we need to call a real translation api here! + from googletrans import Translator + translator = Translator() + result = translator.translate(string, src='en', dest='nl') + return result.text + #return 'Hallo welkom bij Hedy!' # clearly we need to call a real translation api here! def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, translate_strings=False): From 2fcf524b9cbb0eacc2109f77d6c0902f08fb2ed4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 May 2024 15:45:25 +0000 Subject: [PATCH 15/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hedy_translation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hedy_translation.py b/hedy_translation.py index de28068de68..10fde3b47bf 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -78,7 +78,7 @@ def translate(string): translator = Translator() result = translator.translate(string, src='en', dest='nl') return result.text - #return 'Hallo welkom bij Hedy!' # clearly we need to call a real translation api here! + # return 'Hallo welkom bij Hedy!' # clearly we need to call a real translation api here! def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, translate_strings=False): From b4eaace869d1833d9b6e418f97c1c864fb4b7de4 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Wed, 8 May 2024 19:01:15 +0200 Subject: [PATCH 16/21] add api --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 9f4a0de2ddd..041c97695fd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -35,3 +35,4 @@ doit_watch>=0.1.0 uflash>=2.0.0 pyinstaller==6.3.0 commonmark==0.9.1 +googletrans==3.1.0a0 From 81424befd6b2c23fa9f9906258ec79af1a0c9b89 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Wed, 8 May 2024 21:00:23 +0200 Subject: [PATCH 17/21] better organize api calls, also translate in ASK --- hedy_translation.py | 55 ++++++++++++------- .../test_translation_level_01.py | 9 +++ .../test_translation_level_02.py | 18 ++++-- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index 10fde3b47bf..ca523551ff8 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -7,12 +7,16 @@ import hedy_content from website.yaml_file import YamlFile import copy +from googletrans import Translator + + # Holds the token that needs to be translated, its line number, start and # end indexes and its value (e.g. ", "). Rule = namedtuple("Rule", "keyword line start end value") - +# stores the connection to Google Translate +translator = Translator() def keywords_to_dict(lang="nl"): """ "Return a dictionary of keywords from language of choice. Key is english value is lang of choice""" base = path.abspath(path.dirname(__file__)) @@ -73,13 +77,9 @@ def get_target_keyword(keyword_dict, keyword): return keyword -def translate(string): - from googletrans import Translator - translator = Translator() - result = translator.translate(string, src='en', dest='nl') +def translate_string(string, from_lang, to_lang): + result = translator.translate(string, src=from_lang, dest=to_lang) return result.text - # return 'Hallo welkom bij Hedy!' # clearly we need to call a real translation api here! - def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, translate_strings=False): """ "Return code with keywords translated to language of choice in level of choice""" @@ -122,11 +122,11 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, tran result = replace_line(lines, rule.line - 1, replaced_line) else: if translate_strings: - # this is a text string that needs to be translated + # this is not a keyword, so (for now) that means a text string that needs to be translated lines = result.splitlines() # do we need to do this for each rule?? line = lines[rule.line - 1] original = rule.value - target = translate(original) + target = translate_string(original, from_lang, to_lang) replaced_line = replace_token_in_line(line, rule, original, target) result = replace_line(lines, rule.line - 1, replaced_line) @@ -163,11 +163,15 @@ def replace_line(lines, index, line): def replace_token_in_line(line, rule, original, target): """Replaces a token in a line from the user input with its translated equivalent""" - before = "" if rule.start == 0 else line[0: rule.start] - after = "" if rule.end == len(line) - 1 else line[rule.end + 1:] - # Note that we need to replace the target value in the original value because some - # grammar rules have ambiguous length and value, e.g. _COMMA: _SPACES* - # (latin_comma | arabic_comma) _SPACES* + if rule.keyword == 'text': + before = line[:rule.start] + after = line[rule.end-1:] + else: + before = "" if rule.start == 0 else line[0: rule.start] + after = "" if rule.end == len(line) - 1 else line[rule.end + 1:] + # Note that we need to replace the target value in the original value because some + # grammar rules have ambiguous length and value, e.g. _COMMA: _SPACES* + # (latin_comma | arabic_comma) _SPACES* return before + rule.value.replace(original, target) + after @@ -235,12 +239,14 @@ def print(self, tree): self.add_rule("_PRINT", "print", tree) if self.translate_strings: - # in addition to keywords, we atr now also adding plain text strings + # in addition to keywords, we are now also adding plain text strings # like print arguments to the list of things that need to be translated if len(tree.children) > 1: - argument = str(tree.children[1].children[0]) - self.add_rule("text1", argument, tree) # this of course only support 1 string - # you will have to introduce a counter in the class that increases when we see a string + # argument = str(tree.children[1].children[0]) + for argument in tree.children: + if type(argument) is Tree and argument.data == 'text': + self.add_rule("text", "text", argument) # this of course only support 1 string + def print_empty_brackets(self, tree): self.print(tree) @@ -249,6 +255,15 @@ def ask(self, tree): self.add_rule("_IS", "is", tree) self.add_rule("_ASK", "ask", tree) + if self.translate_strings: #it'd be nicer of course if this was not copy-paste from PRINT! + # in addition to keywords, we are now also adding plain text strings + # like ask arguments to the list of things that need to be translated + if len(tree.children) > 1: + # argument = str(tree.children[1].children[0]) + for argument in tree.children: + if type(argument) is Tree and argument.data == 'text': + self.add_rule("text", "text", argument) # this of course only support 1 string + def echo(self, tree): self.add_rule("_ECHO", "echo", tree) @@ -380,8 +395,8 @@ def pressed(self, tree): self.add_rule("_PRESSED", "pressed", tree) def add_rule(self, token_name, token_keyword, tree): - if token_name[:4] == "text": # this is not superduper pretty but for now it works! - token = self.get_keyword_token('text', tree) + if token_name == "text": # this is not superduper pretty but for now it works! + token = tree.children[0] if token: rule = Rule( token_name, token.line, token.column - 1, token.end_column, token.value diff --git a/tests/test_translation_level/test_translation_level_01.py b/tests/test_translation_level/test_translation_level_01.py index 44f0293f19f..ee1f0a4a86e 100644 --- a/tests/test_translation_level/test_translation_level_01.py +++ b/tests/test_translation_level/test_translation_level_01.py @@ -42,6 +42,15 @@ def test_ask_english_dutch(self): self.assertEqual(expected, result) + def test_ask_english_dutch_including_string(self): + code = "ask Hello welcome to Hedy!!" + + result = hedy_translation.translate_keywords(code, from_lang="en", to_lang="nl", + level=self.level, translate_strings=True) + expected = "vraag Hallo welkom bij Hedy!!" + + self.assertEqual(expected, result) + def test_echo_english_dutch(self): code = "ask Hallo welkom bij Hedy!\necho" diff --git a/tests/test_translation_level/test_translation_level_02.py b/tests/test_translation_level/test_translation_level_02.py index 0747d08fdce..f9b09c29512 100644 --- a/tests/test_translation_level/test_translation_level_02.py +++ b/tests/test_translation_level/test_translation_level_02.py @@ -15,12 +15,12 @@ class TestsTranslationLevel2(HedyTester): level = 2 all_keywords = hedy_translation.all_keywords_to_dict() - def test_print(self): - code = "print Hallo welkom bij Hedy!" + def test_print_including_string(self): + code = "print Hallo, welkom Hedy!" result = hedy_translation.translate_keywords( - code, "nl", "en", self.level) - expected = "print Hallo welkom bij Hedy!" + code, "nl", "en", self.level, translate_strings=True) + expected = "print Hello, welcome Hedy!" self.assertEqual(expected, result) @@ -69,6 +69,16 @@ def test_print_var_text(self): self.assertEqual(expected, result) + # def test_print_var_text_including_string(self): + # code = "welcome is Hi welcome to Hedy\nprint welcome Enjoy!" + # + # result = hedy_translation.translate_keywords( + # code, "en", "nl", self.level, translate_strings=True) + # expected = "welkom is Hallo welkom bij Hedy\nprint welkom Veel plezier" + + self.assertEqual(expected, result) + + def test_ask_kewords(self): code = "hedy is vraag print ask echo" From 5e9b7a3cda0321cf30e768f3726fddfb4e835b5a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 May 2024 19:00:40 +0000 Subject: [PATCH 18/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hedy_translation.py | 7 ++++--- tests/test_translation_level/test_translation_level_02.py | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/hedy_translation.py b/hedy_translation.py index ca523551ff8..b38e4ea05e3 100644 --- a/hedy_translation.py +++ b/hedy_translation.py @@ -10,13 +10,14 @@ from googletrans import Translator - # Holds the token that needs to be translated, its line number, start and # end indexes and its value (e.g. ", "). Rule = namedtuple("Rule", "keyword line start end value") # stores the connection to Google Translate translator = Translator() + + def keywords_to_dict(lang="nl"): """ "Return a dictionary of keywords from language of choice. Key is english value is lang of choice""" base = path.abspath(path.dirname(__file__)) @@ -81,6 +82,7 @@ def translate_string(string, from_lang, to_lang): result = translator.translate(string, src=from_lang, dest=to_lang) return result.text + def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1, translate_strings=False): """ "Return code with keywords translated to language of choice in level of choice""" @@ -247,7 +249,6 @@ def print(self, tree): if type(argument) is Tree and argument.data == 'text': self.add_rule("text", "text", argument) # this of course only support 1 string - def print_empty_brackets(self, tree): self.print(tree) @@ -255,7 +256,7 @@ def ask(self, tree): self.add_rule("_IS", "is", tree) self.add_rule("_ASK", "ask", tree) - if self.translate_strings: #it'd be nicer of course if this was not copy-paste from PRINT! + if self.translate_strings: # it'd be nicer of course if this was not copy-paste from PRINT! # in addition to keywords, we are now also adding plain text strings # like ask arguments to the list of things that need to be translated if len(tree.children) > 1: diff --git a/tests/test_translation_level/test_translation_level_02.py b/tests/test_translation_level/test_translation_level_02.py index f9b09c29512..6f9b5454b2b 100644 --- a/tests/test_translation_level/test_translation_level_02.py +++ b/tests/test_translation_level/test_translation_level_02.py @@ -78,7 +78,6 @@ def test_print_var_text(self): self.assertEqual(expected, result) - def test_ask_kewords(self): code = "hedy is vraag print ask echo" From ffb5fef109c284868e2e4094dca923f3e7a06015 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Wed, 8 May 2024 21:21:58 +0200 Subject: [PATCH 19/21] update on working of ask --- .../test_translation_level_02.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_translation_level/test_translation_level_02.py b/tests/test_translation_level/test_translation_level_02.py index f9b09c29512..4ef42200957 100644 --- a/tests/test_translation_level/test_translation_level_02.py +++ b/tests/test_translation_level/test_translation_level_02.py @@ -106,6 +106,28 @@ def test_ask_assign_dutch_english(self): self.assertEqual(expected, result) + def test_ask_assign_dutch_english_including_string(self): + code = textwrap.dedent("""\ + naam is vraag Hoe heet jij? + print Dus het is naam""") + + result = hedy_translation.translate_keywords( + code, "nl", "en", self.level, translate_strings=True) + + expected = textwrap.dedent("""\ + naam is ask How is called you? + print So It is name""") + + # the result sounds silly because all words are translated separately + # in levels 2 and 3, this is needed because words in between can be vars + # in level 4 it will magically be better + + # we should, of course, changed but that' not easy, and for now it is better than what we have + # also: result is naam because ask vars are not yet translated! + + self.assertEqual(expected, result) + + def test_translate_back(self): code = "print welkom bij Hedy\nnaam is ask what is your name\nprint naam" From d15983f96dd2341bf47f56f29b40fd7bd6637ff6 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Thu, 9 May 2024 13:02:04 +0200 Subject: [PATCH 20/21] trial in the front end! --- app.py | 2 +- tests/test_translation_level/test_translation_level_02.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index c6c5ac571b8..a8820380d72 100644 --- a/app.py +++ b/app.py @@ -2289,7 +2289,7 @@ def translate_keywords(): body = request.json try: translated_code = hedy_translation.translate_keywords(body.get('code'), body.get( - 'start_lang'), body.get('goal_lang'), level=int(body.get('level', 1))) + 'start_lang'), body.get('goal_lang'), level=int(body.get('level', 1)), translate_strings=True) if translated_code or translated_code == '': # empty string is False, so explicitly allow it session["previous_keyword_lang"] = body.get("start_lang") session["keyword_lang"] = body.get("goal_lang") diff --git a/tests/test_translation_level/test_translation_level_02.py b/tests/test_translation_level/test_translation_level_02.py index 320049b4d9a..a36edd23b39 100644 --- a/tests/test_translation_level/test_translation_level_02.py +++ b/tests/test_translation_level/test_translation_level_02.py @@ -121,7 +121,7 @@ def test_ask_assign_dutch_english_including_string(self): # in levels 2 and 3, this is needed because words in between can be vars # in level 4 it will magically be better - # we should, of course, changed but that' not easy, and for now it is better than what we have + # we should, of course, changed but that's not easy, and for now it is better than what we have # also: result is naam because ask vars are not yet translated! self.assertEqual(expected, result) From 35309f0be101800329f9ed2480f44e83785ead67 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 May 2024 11:02:36 +0000 Subject: [PATCH 21/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_translation_level/test_translation_level_02.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_translation_level/test_translation_level_02.py b/tests/test_translation_level/test_translation_level_02.py index a36edd23b39..5b9f5e2a944 100644 --- a/tests/test_translation_level/test_translation_level_02.py +++ b/tests/test_translation_level/test_translation_level_02.py @@ -126,7 +126,6 @@ def test_ask_assign_dutch_english_including_string(self): self.assertEqual(expected, result) - def test_translate_back(self): code = "print welkom bij Hedy\nnaam is ask what is your name\nprint naam"