-
Notifications
You must be signed in to change notification settings - Fork 5
/
paper_example.py
205 lines (189 loc) · 5.68 KB
/
paper_example.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
import re
import json
import random
from collections import Counter
from CodeBLEU.code_bleu import code_bleu
from bleu_ignoring import sentence_bleu, SmoothingFunction
from nltk.util import ngrams
from pygments.lexers.jvm import JavaLexer
from pygments.lexers.c_cpp import CppLexer
from pygments.token import Comment
MAXN=4
sm_func = SmoothingFunction(epsilon=0.0001).method1
lexer = JavaLexer()
# lexer = CppLexer()
all_ngrams = []
with open('lang2.json') as f:
data = json.load(f)
print(len(data.items()))
for k, v in data.items():
prob_name = k.split('_')
if int(prob_name[0]) > 1 or len(prob_name[1]) > 4:
continue
for j in v:
if random.random() < 0.3:
tokenized = [i[1] for i in lexer.get_tokens(j) if not (re.fullmatch('\s+', i[1]) or (i[0] in Comment))]
for j in range(1, MAXN+1):
n_grams = list(ngrams(tokenized, j))
all_ngrams.extend(n_grams)
freq = Counter(all_ngrams)
# # ref:
# code1 = "#include <iostream>\n\
# #include <string>\n\
# #include <stdio.h>\n\
# using namespace std;\n\
# int main()\n\
# {\n\
# int i, j;\n\
# string s, v, t;\n\
# getline(cin,s);\n\
# getline(cin,v);\n\
# cout << v << endl << s << endl;\n\
# while ( getline(cin,t) ){\n\
# for ( i = 0; i < t.size(); i++ ){\n\
# for ( j = 0; j < s.size(); j++ ){\n\
# if ( t[i] == s[j] ){\n\
# t[i] = v[j];\n\
# break;\n\
# }\n\
# }\n\
# }\n\
# cout << t << endl;\n\
# }\n\
# }\n\
# "
# # hyp1:
# code2 = "#include <iostream>\n\
# #include <string>\n\
# using namespace std;\n\
# int main(){\n\
# string a,b,s,t,n;\n\
# n = \"\\n\";\n\
# getline(cin,a);\n\
# getline(cin,b);\n\
# cout<<b+n+a+n;\n\
# while(getline(cin,s))\n\
# {\n\
# t=\"\";\n\
# for(int i = 0 ;i <s.length();i++)\n\
# {\n\
# int d=a.find(s[i]);\n\
# if(d!=string::npos)\n\
# t += b[d];\n\
# else\n\
# t+=s[i];\n\
# }\n\
# cout<<t+n;\n\
# }\n\
# }\n\
# "
# # hyp2:
# code3 = "#include<iostream>\n\
# #include<vector>\n\
# using namespace std;\n\
# int main()\n\
# {\n\
# int a,b,c;\n\
# vector <int> N;\n\
# while(cin>>a>>b)\n\
# {\n\
# N.push_back(a);\n\
# N.push_back(b);\n\
# }\n\
# c=N.size();\n\
# for(int i=0;i<c/2;i++)\n\
# cout<<N[2*i]+N[2*i+1]<<endl;\n\
# }\n\
# "
# ref:
code1 = "import java.util.*;\
public class Main {\
public static void main(String[] args) {\
Scanner in = new Scanner(System.in);\
int t = in.nextInt();\
in.nextLine();\
while (t-- > 0) {\
System.out.println(new StringBuffer(in.nextLine()).reverse());\
}\
}\
}\
"
# hyp1:
code2 = "import java.util.Scanner;\
public class Main {\
public static void main(String argv[]) {\
int num_of_tests = 0;\
Scanner in = new Scanner(System.in);\
num_of_tests = Integer.parseInt(in.nextLine());\
for(int i=0; i<num_of_tests; i++) {\
StringBuilder rev_str = new StringBuilder(in.nextLine());\
System.out.println(rev_str.reverse());\
}\
}\
}\
"
# hyp2:
code3 = "import java.util.Scanner;\
public class Main {\
public static void main(String[] args) {\
Scanner in = new Scanner(System.in);\
while (in.hasNext())\
System.out.println(in.nextInt() + in.nextInt());\
}\
}\
"
# code1 = "import java.util.Scanner;\
# public class Main {\
# public static void main(String[] args) {\
# Scanner in = new Scanner(System.in);\
# while (in.hasNext()) {\
# int a = in.nextInt();\
# int x = 2;\
# while (x <= a) {\
# if (a % x == 0)\
# break;\
# x += 1;\
# }\
# System.out.println((x >= a)?”Prime”:”Not prime”);\
# }\
# }\
# }\
# "
# code2 = "import java.util.Scanner;\
# public class Main {\
# public static void main(String[] args) {\
# Scanner in = new Scanner(System.in);\
# while (in.hasNextInt()) {\
# int a = in.nextInt();\
# int b = in.nextInt();\
# System.out.println(a + b);\
# }\
# }\
# }\
# "
# code3 = "import java.util.*;\
# public class Main {\
# public static void main(String[] args){\
# int x;\
# Scanner in = new Scanner(System.in);\
# while (true) {\
# if (!in.hasNext())\
# break;\
# x = in.nextInt();\
# x += in.nextInt();\
# System.out.println(x);\
# }\
# }\
# }\
# "
tokenized1 = [t for t in list(map(lambda x: x[1], lexer.get_tokens(code1))) if not (re.fullmatch('\s+', t) or re.fullmatch('\/\/.*\n', t) or re.match('\/\*.*\*\/', t, re.DOTALL))]
tokenized2 = [t for t in list(map(lambda x: x[1], lexer.get_tokens(code2))) if not (re.fullmatch('\s+', t) or re.fullmatch('\/\/.*\n', t) or re.match('\/\*.*\*\/', t, re.DOTALL))]
tokenized3 = [t for t in list(map(lambda x: x[1], lexer.get_tokens(code3))) if not (re.fullmatch('\s+', t) or re.fullmatch('\/\/.*\n', t) or re.match('\/\*.*\*\/', t, re.DOTALL))]
print(tokenized1, tokenized2, tokenized3)
print(len(tokenized1), len(tokenized2), len(tokenized3))
most_common_dict = dict(freq.most_common(200))
print('BLEU dissimilar', sentence_bleu([tokenized1], tokenized3, smoothing_function=sm_func, ignoring=None))
print('CrystalBLEU dissimilar', sentence_bleu([tokenized1], tokenized3, smoothing_function=sm_func, ignoring=most_common_dict))
print('BLEU similar', sentence_bleu([tokenized1], tokenized2, smoothing_function=sm_func, ignoring=None))
print('CrystalBLEU similar', sentence_bleu([tokenized1], tokenized2, smoothing_function=sm_func, ignoring=most_common_dict))
print('CodeBLEU dissimilar similar', code_bleu([[tokenized1]], [tokenized3]), code_bleu([[tokenized1]], [tokenized2]))