forked from lovrop/topcoder-moj
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLanguageAutoDetection.java
More file actions
94 lines (76 loc) · 2.66 KB
/
Copy pathLanguageAutoDetection.java
File metadata and controls
94 lines (76 loc) · 2.66 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
package moj;
import java.util.*;
import java.util.regex.Pattern;
public class LanguageAutoDetection {
final static String[] CPLUSPLUS_MARKERS = {
"#\\s*include", "#\\s*define",
"private:", "public:", "protected:",
"struct\\s",
"using\\s+namespace",
"template\\s*<",
"inline\\s*",
"vector\\s*<",
"::",
};
final static String[] JAVA_MARKERS = {
"import\\s+java\\.",
"public \\w",
"String\\s*\\[\\]", "int\\s*\\[\\]", "long\\s*\\[\\]",
"HashMap", "TreeMap"
};
final static String[] CSHARP_MARKERS = {
"using\\s+System",
"string\\s*\\[\\]",
"\\[,\\]", "\\[,,\\]", "\\[,,,\\]", "\\[,,,,\\]",
};
static String filterComments(String source) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<source.length();) {
if (i+2 <= source.length()) {
if (source.substring(i, i+2).equals("//")) {
int nextpos = source.indexOf("\n", i+2);
if (nextpos == -1) break;
i = nextpos;
continue;
} else if (source.substring(i, i+2).equals("/*")){
int nextpos = source.indexOf("*/", i+2);
if (nextpos == -1) break;
sb.append(' ');
i = nextpos+2;
continue;
}
}
sb.append(source.charAt(i++));
}
return sb.toString();
}
static boolean isMostLikely(String source, String candidate) {
source = filterComments(source);
Map<String, String[]> markers = new TreeMap<String, String[]>();
markers.put("C++", CPLUSPLUS_MARKERS);
markers.put("Java", JAVA_MARKERS);
markers.put("C#", CSHARP_MARKERS);
System.err.printf("moj language auto detection:");
int best = 0;
String ret = "C++";
boolean first = true;
for (String language : markers.keySet()) {
int matched = 0, total = 0;
for (String m : markers.get(language)) {
if (Pattern.compile(m, Pattern.MULTILINE).matcher(source).find())
++matched;
++total;
}
if (matched > best ||
matched == best && language.equals(candidate)) {
best = matched;
ret = language;
}
if (!first) System.err.printf(",");
first = false;
System.err.printf(" %s %d markers", language, matched);
}
System.err.printf("\n");
return ret.equals(candidate);
}
}