-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnglishOrCode.java
More file actions
executable file
·63 lines (50 loc) · 1.82 KB
/
EnglishOrCode.java
File metadata and controls
executable file
·63 lines (50 loc) · 1.82 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
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
public class EnglishOrCode {
private static Tokenizer tokenizer = null;
public static void initializeTokenizer() {
tokenizer = new Tokenizer();
//key words
String keyString = "abstract assert boolean break byte case catch "
+ "char class const continue default do double else enum"
+ " extends false final finally float for goto if implements "
+ "import instanceof int interface long native new null "
+ "package private protected public return short static "
+ "strictfp super switch synchronized this throw throws true "
+ "transient try void volatile while todo";
String[] keys = keyString.split(" ");
String keyStr = StringUtils.join(keys, "|");
tokenizer.add(keyStr, 1);
tokenizer.add("\\(|\\)|\\{|\\}|\\[|\\]|;|,|\\.|=|>|<|!|~|"
+ "\\?|:|==|<=|>=|!=|&&|\\|\\||\\+\\+|--|"
+ "\\+|-|\\*|/|&|\\||\\^|%|\'|\"|\n|\r|\\$|\\#",
2);//separators, operators, etc
tokenizer.add("[0-9]+", 3); //number
tokenizer.add("[a-zA-Z][a-zA-Z0-9_]*", 4);//identifier
tokenizer.add("@", 4);
}
public static void main(String[] args) throws ClassNotFoundException, IOException {
initializeTokenizer();
String s = "do something in English";
if(isEnglish(s)){
System.out.println("English");
}else{
System.out.println("Java Code");
}
s = "for (int i = 0; i < b.size(); i++) {";
if(isEnglish(s)){
System.out.println("English");
}else{
System.out.println("Java Code");
}
}
private static boolean isEnglish(String replaced) {
tokenizer.tokenize(replaced);
String patternString = tokenizer.getTokensString();
if(patternString.matches(".*444.*") || patternString.matches("4+")){
return true;
}else{
return false;
}
}
}