-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring_script.py
109 lines (85 loc) · 2.98 KB
/
scoring_script.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
# coding: utf-8
# In[1]:
import textual_similarity
import tags_similarity
import API_methods_similarity
from datetime import datetime
import numpy as np
import pickle
import math
# In[2]:
'''
Scoring script is aggregating all scoring functions and applies weights to the different scoring componenets
'''
## An example query containing quicksort algorithm
d = {'imports' : {'pandas', 'numpy', 'scipy'},
'methods' : ['quickSort', 'quickSortHelper', 'partition'],
'code' : '''function quicksort(array)
if length(array) > 1
pivot := select any element of array
left := first index of array
right := last index of array
while left ≤ right
while array[left] < pivot
left := left + 1
while array[right] > pivot
right := right - 1
if left ≤ right
swap array[left] with array[right]
left := left + 1
right := right - 1
quicksort(array from first index to right)
quicksort(array from left to last index)'''}
# In[3]:
def sigmoid(x ,avg):
return 1 / (1 + math.exp(avg-x))
# In[4]:
def get_scores(d):
'''
Get scores from all scripts, Time the execution, Apply weights and return results.
'''
# Time the function
startTime = datetime.now()
txt_score = textual_similarity.tf_score(d)
print ("Textual finished after: " + str(datetime.now() - startTime))
startTime = datetime.now()
tgs_score = tags_similarity.tags_score(d)
print ("Tags finished after: " + str(datetime.now() - startTime))
startTime = datetime.now()
mth_score = API_methods_similarity.score_methods(d)
print ("Methods finished after: " + str(datetime.now() - startTime))
qtn_score = textual_similarity.df.Score
startTime = datetime.now()
mth_score = [float(i) for i in mth_score]
tgs_score = [float(i) for i in tgs_score]
txt_score = [float(i) for i in txt_score]
avg = np.mean(qtn_score)
qtn_score = [sigmoid(i, avg) for i in qtn_score]
txt_score = list(map(lambda x: x * 0.32, txt_score))
tgs_score = list(map(lambda x: x * 0.18, tgs_score))
mth_score = list(map(lambda x: x * 0.30, mth_score))
qtn_score = list(map(lambda x: x * 0.07, qtn_score))
res = [x + y + z + w for x, y, z, w in zip(txt_score, tgs_score, mth_score, qtn_score)]
#top 5
top = np.argsort(res)[-5:][::-1]
print ("All the rest finished after: " + str(datetime.now() - startTime))
results = []
for r in top:
results.append('http://stackoverflow.com/questions/' + str(textual_similarity.df.iloc[r]['Id']))
return results
# In[8]:
if __name__ == '__main__':
with open('sample_inputs/tensorflow_mutilayer_nn_sample.py.pkl', 'rb') as f:
q = pickle.load(f)
top = get_scores(q)
# # In[9]:
#
# top
#
#
# # In[10]:
#
# print('tensorflow_mutilayer_nn_sample results:')
# for r in top:
# print('http://stackoverflow.com/questions/' + str(textual_similarity.df.iloc[r]['Id']))
# In[ ]: