-
Notifications
You must be signed in to change notification settings - Fork 5
/
adaptive_model.py
313 lines (239 loc) · 9.64 KB
/
adaptive_model.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
import re
from os.path import join
import pandas as pd
from configuration import DATA_PATH
from conventional_commits import build_cc_adaptive_regex
from labeling_util import get_false_positives, get_false_negatives
from language_utils import file_scheme, term_seperator, build_separated_terms, negation_terms, modals\
, regex_to_big_query, generate_bq_function, match, SCHEMA_NAME, documentation_entities, prefective_entities\
, software_terms, build_non_positive_linguistic, software_goals_modification, software_goals, unnedded_terms\
, code_review_fixes, no_message, NEAR_ENOUGH
from model_evaluation import classifiy_commits_df, evaluate_performance
core_adaptive_terms = [
'add(?:s|ed|ing)?',
'creat(?:e|es|ing)',
'disabl(?:e|es|ed|ing)',
'implement(?:ed|s|ing)?',
'import(?:s|ed|ing)?',
'introduc(?:e|es|ed|ing)',
'port(?:s|ed|ing)?',
'provid(?:e|es|ed|ing)',
'updat(?:e|es|ed|ing)',
'upgrad(?:e|es|ed|ing)'
]
adaptive_context = [
'(?:un)?hid(?:e|es|den)',
'allow(?:s|ed|ing)?',
'buil(?:t|ds|ing)',
'calibirat(?:e|es|ed|ing)',
'configure',
'deferr(?:ed|s|ing)?',
'enhanc(?:e|es|ed|ing)',
'extend(?:s|ed|ing)?',
'form(?:ed|s|ing)?',
'report(?:s|ed|ing)?',
'support(s|ed|ing)?',
# , 'mov(e|es|ed|ing)'
# , 'print(s|ed|ing)?'
] + core_adaptive_terms
adaptive_entities = ['ability', 'configuration', 'conversion', 'debug', 'new', 'possibility', 'support'
, 'test(s)?', 'tweak(s)?', 'mode', 'option']
adaptive_header_action = "|".join([
'upgrad(?:e|es|ed|ing)',
'configur(?:e|es|ed|ing)',
'chang(?:e|es|ed|ing)',
'(?:keep|change)\s+(?:the\s+)?default',
'new',
# '(?:make(?:s)?|made|making)',
'merg(?:e|es|ed|ing)',
'clear(?:s|ed|ing)?',
#'comment(?:s|ed|ing)?\sout'
'creat(?:e|es|ed|ing)',
'cast(?:s|et|ing)?' + NEAR_ENOUGH + '\sas',
# 'convert(?:s|ed|ing)?',
# 'check(?:s|ed|ing)?',
'add(?:s|ed|ing)?',
# 'buil(?:d|t|ds|ing)',
'initial revision',
'(?:im)?port(?:s|ed|ing)?',
'(?:un)?hid(?:e|es|den)',
'updat(?:e|es|ed|ing)',
'upload(?:s|ed|ing)?',
'disabl(?:e|es|ed|ing)',
'delet(?:e|es|ed|ing)',
'enabl(?:e|es|ed|ing)',
'quirk(?:s|ed|ing)?',
'skip(?:s|ed|ing)?',
'switch(?:s|ed|ing)?',
'allow(?:s|ed|ing)?',
'provid(e|es|ed|ing)',
###
# , 'build'
# , 'mark(?:s|ed|ing)?'
# , 'us(?:e|es|ed|ing)'
# , '(?:make|made|making)'
# , 'creat(?:e|es|ed|ing)'
# , 'handl(?:e|es|ed|ing)'
'remov(?:e|es|ed|ing)',
'refresh(?:s|ed|ing)?',
#'re(-)?enabl(?:e|es|ed|ing)',
] +no_message
)
adaptive_actions = [ # 'revert(?:s|ed|ing)?',
#'merg(?:e|es|ed|ing)[\s\S]{1,5}(pull request|pr|branch)',
'add(?:s|ed|ing)?[\s\S]{1,50}(?:version|v\d|ver\d)',
#'(cr(s)?(-)?|code\sreview)\sfix(?:s|ed|ing)?',
'(^|\s)implement(?:ed|s|ing)?\s',
'(?:make(?:s)?|made|making)[\s\S]{1,50}consitent',
'updat(?:e|es|ed|ing)[\s\S]{1,25}to[\s\S]{1,25}\d+.\d',
'updat(?:e|es|ed|ing)\s+(to\s+)?\d+\.\d',
'(?:add(s|ed|ing)?|delet(?:e|es|ed|ing)|updat(?:e|es|ed|ing))\s+' + file_scheme,
# '(add(s|ed|ing)?|delet(e|es|ed|ing)|updat(e|es|ed|ing))\s+([A-Z0-9_]*)', # TODO - run without lower
'(^|^[\s\S]{0,25}%s)(%s)%s' % (term_seperator, adaptive_header_action, term_seperator),
# '^(?:version|v\d+\.\d|ver\d+\.\d)',
'^\[(?:IMP|imp)\]', # TODO - take care of upper/lower case
'support(?:s|ed|ing)?\sfor\s',
'show(?:es|ed|ing)?[\s\S]instead',
'scal(?:e|es|ed|ing)?\s(up|down)'
] + code_review_fixes
def build_adaptive_action_regex():
return "(%s)" % ("|".join(
adaptive_actions))
def build_adaptive_regex(use_conventional_commits=True):
adaptive_context_re = build_separated_terms(adaptive_context, just_before=True)
base_re = "((%s)\s[\s\S]{0,50}(%s)%s)" % (adaptive_context_re
, "|".join(adaptive_entities + software_terms)
, term_seperator)
if use_conventional_commits:
agg_re = "((%s)|(%s))" % (base_re
, build_cc_adaptive_regex())
else:
agg_re = base_re
return agg_re
def build_non_adaptive_context():
non_adaptive_header_action = "|".join([
'transla(?:tion|et|eted|ets|ting)'
, 'readme(?:.md)?'
])
non_adaptive_header ='^[\s\S]{0,50}(%s)' % non_adaptive_header_action
entities = documentation_entities + ['bug',
'helper',
'miss(?:ing|ed)',
'to(?: |-)?do(?:s)?',
'warning(?:s)?'
]
adaptive_actions = ['remov(?:e|es|ed|ing)']
non_adaptive_entities = documentation_entities + software_terms + unnedded_terms + [file_scheme]
return '(%s)' % "|".join(['(?:%s)\s[\s\S]{0,50}(?:%s)' % (build_separated_terms(adaptive_context, just_before=True)
, "|".join(entities))
, non_adaptive_header
, '(?:%s)\s[\s\S]{0,50}(?:%s)' % (build_separated_terms(adaptive_actions, just_before=True)
, "|".join(non_adaptive_entities))
])
def build_non_adaptive_linguistic():
return build_non_positive_linguistic(build_adaptive_regex(use_conventional_commits=False))
def is_adaptive(text):
return (match(text, build_adaptive_regex())
+ match(text, build_adaptive_action_regex())
- match(text, build_non_adaptive_context())
- match(text, build_non_adaptive_linguistic()))
def build_core_adaptive_regex():
return '(%s)' % build_separated_terms(core_adaptive_terms)
def is_core_adaptive(text):
return match(text, build_core_adaptive_regex())
def core_adaptive_to_bq():
print("# Core Adaptive Term")
print( regex_to_big_query(build_core_adaptive_regex()))
print("#Core Adaptive Term - end")
def adaptive_to_bq():
print("# Adaptive")
print( "# Adaptive :build_adaptive_regex()")
#print( ",")
print( regex_to_big_query(build_adaptive_regex()))
print( "#Adaptive :build_adaptive_action_regex()")
print( "+")
print( regex_to_big_query(build_adaptive_action_regex()))
print( "# Adaptive :build_non_adaptive_context()")
print( "-")
print( regex_to_big_query(build_non_adaptive_context()))
print( "# Adaptive :build_non_adaptive_linguistic()")
print( "-")
print( regex_to_big_query(build_non_adaptive_linguistic()))
print("# Adaptive - end")
def print_adaptive_functions(commit: str = 'XXX'):
print()
generate_bq_function('{schema}.bq_adaptive'.format(schema=SCHEMA_NAME)
, adaptive_to_bq
, commit=commit)
print()
core_adaptive_to_bq
print()
generate_bq_function('{schema}.bq_core_adaptive'.format(schema=SCHEMA_NAME)
, core_adaptive_to_bq
, commit=commit)
print()
def evaluate_adaptive_classifier():
text_name = 'message'
classification_function = is_adaptive
classification_column = 'corrective_pred'
concept_column='Is_Adaptive'
df = pd.read_csv(join(DATA_PATH, 'commit_classification_batch2.csv'))
df = df[df.certain != 'FALSE']
df = df[~df.Is_Corrective.isna()]
"""
concept_column = 'is_adaptive'
df = pd.read_csv(join(DATA_PATH, "commit_classification_batch2.csv"))
df[concept_column] = df.expected.map(lambda x: not x)
"""
df = classifiy_commits_df(df
, classification_function=classification_function
, classification_column=classification_column
, text_name=text_name
)
cm = evaluate_performance(df
, classification_column
, concept_column
, text_name=text_name)
print("corrective_labels CM")
print(cm)
"""
fp = get_false_positives(df
, classifier_column=classification_column
, concept_column=concept_column)
print("False Positives")
pd.options.display.max_columns = 50
pd.options.display.max_rows = 2000
print(fp)
"""
fn = get_false_negatives(df
, classifier_column=classification_column
, concept_column=concept_column)
print("False Negatives")
pd.options.display.max_columns = 50
pd.options.display.max_rows = 2000
print(fn)
if __name__ == '__main__':
print_adaptive_functions(commit='4b76d8e76af938824f91f4b99247731c21e37ff9')
#evaluate_adaptive_classifier()
text = """
"Leverage pip to access installed packages
- Use `get_installed_distributions` from pip
- This cascades to `pkg_resources.working_set` which relies on
`sys.path` to uncover packages
- Consequently I implemented `temp_path()` as a contextmanager in the
style of `temp_environ()`
- This requires us to know the environment's `sys.path`, so
`load_path(python)` will do a `json.dumps` of `sys.path` to stdout
which then gets loaded and returned
- Allows us to avoid trying to hack around `pip freeze` output to parse
out names from comments etc
- Provides other potential uses
Signed-off-by: Dan Ryan <[email protected]>
"
""".lower()
print("is adaptive", is_adaptive(text))
valid_num = len(re.findall(build_adaptive_action_regex(), text))
valid_num = len(re.findall('updat(?:e|es|ed|ing)', text))
valid_num = len(re.findall(build_non_adaptive_context(), text))
print(valid_num)
print(build_non_adaptive_context())