forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedy_translation.py
338 lines (253 loc) · 11 KB
/
hedy_translation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from collections import namedtuple
from lark import Visitor, Token
import hedy
import operator
import yaml
from os import path
import hedy_content
# 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")
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__))
keywords_path = "content/keywords/"
yaml_filesname_with_path = path.join(base, keywords_path, lang + ".yaml")
with open(yaml_filesname_with_path, "r", encoding="UTF-8") as stream:
command_combinations = yaml.safe_load(stream)
for k, v in command_combinations.items():
command_combinations[k] = v.split("|")
return command_combinations
def keywords_to_dict_single_choice(lang):
command_combinations = keywords_to_dict(lang)
return {k: v[0] for (k, v) in command_combinations.items()}
def all_keywords_to_dict():
"""Return a dictionary where each value is a list of the translations of that keyword (key). Used for testing"""
keyword_dict = {}
for lang in hedy_content.ALL_KEYWORD_LANGUAGES:
commands = keywords_to_dict_single_choice(lang)
keyword_dict[lang] = commands
all_translations = {k: [v.get(k, k) for v in keyword_dict.values()] for k in keyword_dict["en"]}
return all_translations
def translate_keyword_from_en(keyword, lang="en"):
# translated the keyword to a local lang
local_keywords = keywords_to_dict(lang)
if keyword in local_keywords.keys():
local_keyword = local_keywords[keyword][0]
else:
local_keyword = keyword
return local_keyword
def translate_keyword_to_en(keyword, lang):
# translated the keyword to from a local lang
original_keywords = keywords_to_dict(lang)
for k, v in original_keywords.items():
if keyword in v:
return k
return keyword
def get_target_keyword(keyword_dict, keyword):
if keyword in keyword_dict.keys():
return keyword_dict[keyword][0]
else:
return keyword
def translate_keywords(input_string_, from_lang="en", to_lang="nl", level=1):
""" "Return code with keywords translated to language of choice in level of choice"""
try:
processed_input = hedy.process_input_string(
input_string_, level, from_lang, escape_backslashes=False, preprocess_ifs_enabled=False
)
hedy.source_map.clear()
hedy.source_map.set_skip_faulty(False)
parser = hedy.get_parser(level, from_lang, True)
keyword_dict_from = keywords_to_dict(from_lang)
keyword_dict_to = keywords_to_dict(to_lang)
program_root = parser.parse(processed_input + "\n").children[0]
translator = Translator(processed_input)
translator.visit(program_root)
ordered_rules = reversed(sorted(translator.rules, key=operator.attrgetter("line", "start")))
# FH Feb 2022 TODO trees containing invalid nodes are happily translated,
# should be stopped here!
result = processed_input
for rule in ordered_rules:
if rule.keyword in keyword_dict_from and rule.keyword in keyword_dict_to:
lines = result.splitlines()
line = lines[rule.line - 1]
original = get_original_keyword(keyword_dict_from, rule.keyword, line)
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)
# 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", "")
return result
except Exception:
return input_string_
def replace_line(lines, index, line):
before = "\n".join(lines[0:index])
after = "\n".join(lines[index + 1:])
if len(before) > 0:
before = before + "\n"
if len(after) > 0:
after = "\n" + after
return "".join([before, line, after])
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*
return before + rule.value.replace(original, target) + after
def find_command_keywords(
input_string, lang, level, keywords, start_line, end_line, start_column, end_column
):
parser = hedy.get_parser(level, lang, True)
program_root = parser.parse(input_string).children[0]
translator = Translator(input_string)
translator.visit(program_root)
return {
k: find_keyword_in_rules(
translator.rules, k, start_line, end_line, start_column, end_column
)
for k in keywords
}
def find_keyword_in_rules(rules, keyword, start_line, end_line, start_column, end_column):
for rule in rules:
if rule.keyword == keyword and rule.line == start_line and rule.start >= start_column:
if rule.line < end_line or (rule.line == end_line and rule.end <= end_column):
return rule.value
return None
def get_original_keyword(keyword_dict, keyword, line):
found = False
for word in keyword_dict[keyword]:
if word in line:
original = word
found = True
# If we can't find the keyword, it means that it isn't part of the valid keywords for this language
# so return original instead
if found:
return original
else:
return keyword
class Translator(Visitor):
"""The visitor finds tokens that must be translated and stores information about their exact position
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):
self.input_string = input_string
self.rules = []
def define(self, tree):
self.add_rule("_DEFINE", "define", tree)
def defs(self, tree):
self.add_rule("_DEF", "def", tree)
def call(self, tree):
self.add_rule("_CALL", "call", tree)
def withs(self, tree):
self.add_rule("_WITH", "with", tree)
def returns(self, tree):
self.add_rule("_RETURN", "return", tree)
def print(self, tree):
self.add_rule("_PRINT", "print", tree)
def print_empty_brackets(self, tree):
self.print(tree)
def ask(self, tree):
self.add_rule("_IS", "is", tree)
self.add_rule("_ASK", "ask", tree)
def echo(self, tree):
self.add_rule("_ECHO", "echo", tree)
def color(self, tree):
self.add_rule("_COLOR", "color", tree)
def forward(self, tree):
self.add_rule("_FORWARD", "forward", tree)
def turn(self, tree):
self.add_rule("_TURN", "turn", tree)
def left(self, tree):
token = tree.children[0]
rule = Rule("left", token.line, token.column - 1, token.end_column - 2, token.value)
self.rules.append(rule)
def right(self, tree):
token = tree.children[0]
rule = Rule("right", token.line, token.column - 1, token.end_column - 2, token.value)
self.rules.append(rule)
def assign_list(self, tree):
self.add_rule("_IS", "is", tree)
commas = self.get_keyword_tokens("_COMMA", tree)
for comma in commas:
rule = Rule("comma", comma.line, comma.column - 1, comma.end_column - 2, comma.value)
self.rules.append(rule)
def assign(self, tree):
self.add_rule("_IS", "is", tree)
def sleep(self, tree):
self.add_rule("_SLEEP", "sleep", tree)
def add(self, tree):
self.add_rule("_ADD_LIST", "add", tree)
self.add_rule("_TO_LIST", "to_list", tree)
def remove(self, tree):
self.add_rule("_REMOVE", "remove", tree)
self.add_rule("_FROM", "from", tree)
def random(self, tree):
token = tree.children[0]
rule = Rule("random", token.line, token.column - 1, token.end_column - 2, token.value)
self.rules.append(rule)
def ifs(self, tree):
self.add_rule("_IF", "if", tree)
def ifelse(self, tree):
self.add_rule("_IF", "if", tree)
self.add_rule("_ELSE", "else", tree)
def elifs(self, tree):
self.add_rule("_ELIF", "elif", tree)
def elses(self, tree):
self.add_rule("_ELSE", "else", tree)
def condition_spaces(self, tree):
self.add_rule("_IS", "is", tree)
def equality_check_is(self, tree):
self.equality_check(tree)
def equality_check(self, tree):
self.add_rule("_IS", "is", tree)
self.add_rule("_EQUALS", "=", tree)
self.add_rule("_DOUBLE_EQUALS", "==", tree)
def in_list_check(self, tree):
self.add_rule("_IN", "in", tree)
def list_access(self, tree):
self.add_rule("_AT", "at", tree)
def list_access_var(self, tree):
self.add_rule("_IS", "is", tree)
self.add_rule("_AT", "at", tree)
def repeat(self, tree):
self.add_rule("_REPEAT", "repeat", tree)
self.add_rule("_TIMES", "times", tree)
def for_list(self, tree):
self.add_rule("_FOR", "for", tree)
self.add_rule("_IN", "in", tree)
def for_loop(self, tree):
self.add_rule("_FOR", "for", tree)
self.add_rule("_IN", "in", tree)
self.add_rule("_RANGE", "range", tree)
self.add_rule("_TO", "to", tree)
def while_loop(self, tree):
self.add_rule("_WHILE", "while", tree)
def and_condition(self, tree):
self.add_rule("_AND", "and", tree)
def or_condition(self, tree):
self.add_rule("_OR", "or", tree)
def input(self, tree):
self.add_rule("_IS", "is", tree)
self.add_rule("_INPUT", "input", tree)
def input_empty_brackets(self, tree):
self.add_rule("_IS", "is", tree)
self.add_rule("_INPUT", "input", tree)
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:
rule = Rule(
token_keyword, token.line, token.column - 1, token.end_column - 2, token.value
)
self.rules.append(rule)
def get_keyword_token(self, token_type, node):
for c in node.children:
if type(c) is Token and c.type == token_type:
return c
return None
def get_keyword_tokens(self, token_type, node):
return [c for c in node.children if type(c) is Token and c.type == token_type]