forked from amazon-science/recode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_rename.py
184 lines (166 loc) · 7.42 KB
/
func_rename.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
from nlaugmenter.transformations.butter_fingers_perturbation.transformation import ButterFingersPerturbation
from nlaugmenter.transformations.swap_characters.transformation import SwapCharactersPerturbation
from nlaugmenter.transformations.change_char_case.transformation import ChangeCharCase
from nlaugmenter.transformations.english_inflectional_variation.transformation import EnglishInflectionalVariation
from nlaugmenter.transformations.synonym_substitution.transformation import SynonymSubstitution
def post_process_name(s):
""" clean up the perturbed function name
return None if no simple way to make it a valid name
"""
if len(s) == 0:
return ""
res = ""
start = False
for ch in s:
if ch.isalpha:
# make sure the first character isalpha()
start = True
if start:
if ch.isalnum() or ch == '_':
res += ch
return res
def replace_func_name(code, entry_point, new_func_name):
""" A general function to replace function name with the new name
"""
new_func_name = post_process_name(new_func_name)
if new_func_name == "":
return code, entry_point
return code.replace(entry_point, new_func_name), new_func_name
def FuncRenameSynonymSub(code, entry_point, seed=0):
""" Using synonum substitution to perturb function name
>>> example1: has_close_elements => receive_close_element
>>> example2: hasCloseElements => receiveCloseElements
"""
t = SynonymSubstitution(seed=seed)
new_code = str(code)
if not isinstance(entry_point, list):
entry_points = [entry_point]
else:
entry_points = entry_point
for entry_point in entry_points:
if "_" in entry_point:
# has_close_elements => receive_close_element
new_func_name = " ".join(entry_point.split("_"))
new_func_name = t.generate(new_func_name)[0]
new_func_name = "_".join(new_func_name.split(" "))
else:
# hasCloseElements => receiveCloseElements
_, new_func_name = FuncRenameCamelCase(new_code, entry_point)
new_func_name = " ".join(new_func_name.split("_"))
new_func_name = t.generate(new_func_name)[0]
new_func_name = "_".join(new_func_name.split(" "))
_, new_func_name = FuncRenameCamelCase(new_code, new_func_name)
new_code, new_func_name = replace_func_name(new_code, entry_point, new_func_name)
# print(f"function name from \"{entry_point}\" to \"{new_func_name}\"")
# import pdb; pdb.set_trace()
return new_code, new_func_name
def FuncRenameInflectionalVariation(code, entry_point, seed):
""" Using english inflectional variation to perturb function name
"""
t = EnglishInflectionalVariation(seed=seed)
new_code = str(code)
if not isinstance(entry_point, list):
entry_points = [entry_point]
else:
entry_points = entry_point
for entry_point in entry_points:
# new_func_name = t.generate(entry_point)[0]
if "_" in entry_point:
# has_close_elements => receive_close_element
new_func_name = " ".join(entry_point.split("_"))
new_func_name = t.generate(new_func_name)[0]
new_func_name = "_".join(new_func_name.split(" "))
else:
# hasCloseElements => receiveCloseElements
_, new_func_name = FuncRenameCamelCase(new_code, entry_point)
new_func_name = " ".join(new_func_name.split("_"))
new_func_name = t.generate(new_func_name)[0]
new_func_name = "_".join(new_func_name.split(" "))
_, new_func_name = FuncRenameCamelCase(new_code, new_func_name)
new_code, new_func_name = replace_func_name(new_code, entry_point, new_func_name)
# print(f"function name from \"{entry_point}\" to \"{new_func_name}\"")
# import pdb; pdb.set_trace()
return new_code, new_func_name
def FuncRenameChangeChar(code, entry_point, seed):
""" Using random character case changes to perturb function name
"""
t = ChangeCharCase(seed=seed, prob=0.35)
# default 0.1 would be too small since func name is short
new_code = str(code)
if not isinstance(entry_point, list):
entry_points = [entry_point]
else:
entry_points = entry_point
for entry_point in entry_points:
new_func_name = t.generate(entry_point)[0]
new_code, new_func_name = replace_func_name(new_code, entry_point, new_func_name)
# print(f"function name from \"{entry_point}\" to \"{new_func_name}\"")
# import pdb; pdb.set_trace()
return new_code, new_func_name
def FuncRenameSwapChar(code, entry_point, seed):
""" Using character swap to perturb function name
"""
t = SwapCharactersPerturbation(seed=seed)
new_code = str(code)
if not isinstance(entry_point, list):
entry_points = [entry_point]
else:
entry_points = entry_point
for entry_point in entry_points:
new_func_name = t.generate(entry_point)[0]
# new_func_name = t.generate(entry_point, prob=0.1)[0]
new_code, new_func_name = replace_func_name(new_code, entry_point, new_func_name)
# print(f"function name from \"{entry_point}\" to \"{new_func_name}\"")
# import pdb; pdb.set_trace()
return new_code, new_func_name
def FuncRenameButterFinger(code, entry_point, seed):
""" Using butterfinger to perturb function name
"""
t = ButterFingersPerturbation(seed=seed)
new_code = str(code)
if not isinstance(entry_point, list):
entry_points = [entry_point]
else:
entry_points = entry_point
for entry_point in entry_points:
new_func_name = t.generate(entry_point)[0]
new_code, new_func_name = replace_func_name(new_code, entry_point, new_func_name)
# print(f"function name from \"{entry_point}\" to \"{new_func_name}\"")
# import pdb; pdb.set_trace()
return new_code, new_func_name
def FuncRenameCamelCase(code, entry_point, seed=0):
""" Perturb between two versions of function name
We might have input function names as list which means we need to perturb all these names
>>> example1: has_close_elements => hasCloseElements
>>> example2: hasCloseElements => has_close_elements
"""
new_code = str(code)
if not isinstance(entry_point, list):
entry_points = [entry_point]
else:
entry_points = entry_point
for entry_point in entry_points:
if entry_point == "": continue
if "_" in entry_point:
# has_close_elements => hasCloseElements
new_func_name = str(entry_point)
words = new_func_name.split("_")
new_words = [words[0]]
for word in words[1:]:
chars = list(word)
if len(chars) == 0: continue
chars[0] = chars[0].upper()
new_words.append("".join(chars))
new_func_name = "".join(new_words)
else:
# hasCloseElements => has_close_elements
new_func_name = entry_point[0]
for ch in entry_point[1:]:
if ch.isupper():
new_func_name += "_" + ch.lower()
else:
new_func_name += ch
new_code, new_func_name = replace_func_name(new_code, entry_point, new_func_name)
# print(f"function name from \"{entry_point}\" to \"{new_func_name}\"")
# import pdb; pdb.set_trace()
return new_code, new_func_name