-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathabstract_numerals.py
39 lines (33 loc) · 1.39 KB
/
abstract_numerals.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
import numpy.random as npr
def generate_number_replacements(line, config, hl2ll):
min_abstracted_number = config.getint("NumberAbstraction", "MinAbstractedValue")
max_abstracted_number = config.getint("NumberAbstraction", "MaxAbstractedValue")
max_constants = config.getint("NumberAbstraction", "NumbersPerStatement")
replacements = {}
parts = line.strip().split(" ")
for i in range(len(parts)):
if hl2ll.is_number(parts[i]):
number = hl2ll.get_number(parts[i])
if (abs(int(number)) >= min_abstracted_number) and (abs(int(number)) <= max_abstracted_number):
if number in replacements.keys():
constant = replacements[number]
else:
free_constants = filter(lambda n: n not in replacements.values(), ['N'+str(j) for j in range(max_constants)])
if len(free_constants) == 0:
constant = 'N' + str(npr.randint(0, max_constants))
else:
constant = free_constants[npr.randint(0,len(free_constants))]
replacements[number] = constant
parts[i] = constant
return (" ".join(parts), replacements)
def apply_number_replacements(line, replacements):
parts = line.strip().split(" ")
for i in range(len(parts)):
if parts[i] in replacements.keys():
parts[i] = replacements[parts[i]]
return " ".join(parts)
def reverse_mapping(replacements):
reverse_replacement = {}
for k in replacements.keys():
reverse_replacement[replacements[k]] = k
return reverse_replacement