-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlang_orig.py
More file actions
171 lines (145 loc) · 5.21 KB
/
Copy pathlang_orig.py
File metadata and controls
171 lines (145 loc) · 5.21 KB
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
import os
import os.path
import pickle
import pandas as pd
import nltk
import re
from textblob import TextBlob as tb
from sklearn import metrics
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.naive_bayes import BernoulliNB
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
def read_files(dir_path):
file_list = []
for root, dirs, files in os.walk(dir_path):
for name in files:
file_list.append(os.path.join(root, name))
for name in dirs:
file_list.append((os.path.join(root, name)))
return file_list
def get_extension(file):
return os.path.splitext(file)[1]
def get_language(ext):
if ext in ['.clj', '.cljs', '.edn', '.clojure']:
return 'Clojure'
elif ext in ['.hs', '.lhs', 'ghc']:
return 'Haskell'
elif ext in ['.java', '.class', '.jar']:
return 'Java'
elif ext in ['.js', '.javascript']:
return 'Javascript'
elif ext in ['.pl', '.pm', '.t', '.pod', '.perl']:
return 'Perl'
elif ext in ['.php', '.phtml', '.php4', '.php3', '.php5', '.phps']:
return 'PHP'
elif ext in ['.ocaml', '.ml']:
return 'Ocaml'
elif ext in ['.py', '.pyw', '.pyc', '.pyo', '.pyd', '.python3']:
return "Python"
elif ext in ['.rb', '.rbw', '.jruby']:
return "Ruby"
elif ext in ['.scala']:
return 'Scala'
elif ext in ['.scm', '.ss', '.racket']:
return "Scheme"
elif ext in ['.tcl']:
return "Tcl"
return None
def read_train_data():
files = read_files("./rosetta")
main_list = []
for file in files:
ext = get_extension(file)
lang = get_language(ext)
if lang != None:
file_lang = []
with open(file, errors="surrogateescape") as in_file:
texto = in_file.read()
main_list.append([texto, lang])
datadf = pd.DataFrame(main_list, columns = ['Code', 'Language'])
return datadf
def join_all_code(content):
all_content = [row["Code"] for ind, row in content.iterrows()]
return ' '.join(all_content)
def tokenize(content):
tokens = nltk.word_tokenize(content)
return ' '.join(tokens)
def word_freq(word, code):
if len(code.words) == 0:
return 0
else:
return code.word_counts[word] / len(code.words)
def return_tokenized_data(datadf):
all_code = join_all_code(datadf)
all_tokens = tokenize(all_code)
return tb(all_tokens)
def calculate_scores(token_blob):
scores = {word: word_freq(word,token_blob) for word in token_blob.words}
return sorted(scores.items(), key=lambda x: x[1], reverse=True)
def select_features(word_list):
exclude_list = ['l','6','d','x1','x2','5','j','m','w','i','e','g','4','v','n','0','1','a','x','n','y','b','2','c','s','3']
final={}
counter = 0
for word, score in word_list:
if word not in exclude_list:
final[word] = round(score,5)
counter += 1
if counter == 160:
break
print("features#", len(final))
return final
def create_vectors(final,data):
super_final = []
for ind, row in data.iterrows():
mydict = {}
for word, score in final.items():
if row['Code'].find(word) != -1:
mydict[word] = score
else:
mydict[word] = 0
super_final.append(mydict)
vec = DictVectorizer()
return vec.fit_transform(super_final).toarray()
def test_model(x,y,model):
if model == 'tree':
clf = tree.DecisionTreeClassifier()
elif model == 'bernoulli':
clf = BernoulliNB()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=0)
clf = clf.fit(x_train, y_train)
predicted = clf.predict(x_test)
print('predicted',predicted)
print(metrics.classification_report(y_test, predicted))
print(metrics.f1_score(y_test, predicted))
scores = cross_val_score(clf, x, y, cv=5)
return scores, clf
def test_model(x,y,model):
if model == 'tree':
clf = tree.DecisionTreeClassifier()
elif model == 'bernoulli':
clf = BernoulliNB()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=0)
clf = clf.fit(x_train, y_train)
predicted = clf.predict(x_test)
#print('predicted',predicted)
print(metrics.classification_report(y_test, predicted))
print(metrics.f1_score(y_test, predicted))
scores = cross_val_score(clf, x, y, cv=5)
return scores, clf
if __name__ == '__main__':
datadf = read_train_data()
target = datadf['Language'].values
token_blob = return_tokenized_data(datadf)
word_list = calculate_scores(token_blob)
final_words = select_features(word_list)
#print("word_list for model", final_words)
new_x = create_vectors(final_words,datadf)
bernoulli_score = test_model(new_x, target, 'bernoulli')
tree_score, clf_model = test_model(new_x, target, 'tree')
pickle.dump( clf_model, open( "model.p", "wb" ) )
pickle.dump( final_words, open( "features.p", "wb" ) )