|
@@ -1,6 +1,8 @@
|
1
|
1
|
import java.io.File;
|
2
|
2
|
import java.io.IOException;
|
3
|
3
|
import java.util.Scanner;
|
|
4
|
+import java.util.regex.Matcher;
|
|
5
|
+import java.util.regex.Pattern;
|
4
|
6
|
|
5
|
7
|
/**
|
6
|
8
|
* Created by thook on 10/7/15.
|
|
@@ -10,22 +12,29 @@ public class HamletParser {
|
10
|
12
|
private String hamletData;
|
11
|
13
|
|
12
|
14
|
public HamletParser(){
|
|
15
|
+
|
13
|
16
|
this.hamletData = loadFile();
|
14
|
17
|
}
|
15
|
18
|
|
16
|
19
|
private String loadFile(){
|
|
20
|
+
|
17
|
21
|
ClassLoader classLoader = getClass().getClassLoader();
|
|
22
|
+
|
18
|
23
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
|
- StringBuilder result = new StringBuilder("");
|
|
24
|
+
|
|
25
|
+ StringBuilder result = new StringBuilder();
|
20
|
26
|
|
21
|
27
|
try(Scanner scanner = new Scanner(file)){
|
|
28
|
+
|
22
|
29
|
while(scanner.hasNextLine()){
|
|
30
|
+
|
23
|
31
|
String line = scanner.nextLine();
|
|
32
|
+
|
24
|
33
|
result.append(line).append("\n");
|
25
|
34
|
}
|
26
|
35
|
|
27
|
|
- scanner.close();
|
28
|
36
|
}catch(IOException e){
|
|
37
|
+
|
29
|
38
|
e.printStackTrace();
|
30
|
39
|
}
|
31
|
40
|
|
|
@@ -33,7 +42,76 @@ public class HamletParser {
|
33
|
42
|
}
|
34
|
43
|
|
35
|
44
|
public String getHamletData(){
|
|
45
|
+
|
36
|
46
|
return hamletData;
|
37
|
47
|
}
|
38
|
48
|
|
|
49
|
+ public String changeHamletToLeon(String testString) {
|
|
50
|
+
|
|
51
|
+ Pattern pattern = Pattern.compile("\\w+(\\W)?");
|
|
52
|
+
|
|
53
|
+ Matcher matcher = pattern.matcher(testString);
|
|
54
|
+
|
|
55
|
+ String answer = "";
|
|
56
|
+
|
|
57
|
+ while(matcher.find()){
|
|
58
|
+
|
|
59
|
+ String word = matcher.group();
|
|
60
|
+
|
|
61
|
+ if(word.equals("Hamlet ")) {
|
|
62
|
+
|
|
63
|
+ answer += "Leon ";
|
|
64
|
+
|
|
65
|
+ } else {
|
|
66
|
+
|
|
67
|
+ answer += word;
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ return answer;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ public String changeHoratioToTariq(String testString) {
|
|
75
|
+
|
|
76
|
+ Pattern pattern = Pattern.compile("\\w+(\\W)?");
|
|
77
|
+
|
|
78
|
+ Matcher matcher = pattern.matcher(testString);
|
|
79
|
+
|
|
80
|
+ String answer = "";
|
|
81
|
+
|
|
82
|
+ while(matcher.find()){
|
|
83
|
+
|
|
84
|
+ String word = matcher.group();
|
|
85
|
+
|
|
86
|
+ if(word.equals("Horatio ")) {
|
|
87
|
+
|
|
88
|
+ answer += "Tariq ";
|
|
89
|
+
|
|
90
|
+ } else {
|
|
91
|
+
|
|
92
|
+ answer += word;
|
|
93
|
+ }
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ return answer;
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ public boolean findHoratio(String name) {
|
|
100
|
+
|
|
101
|
+ Pattern pattern = Pattern.compile(name);
|
|
102
|
+
|
|
103
|
+ Matcher matcher = pattern.matcher(name);
|
|
104
|
+
|
|
105
|
+ return matcher.find();
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+ public boolean findHamlet(String name) {
|
|
110
|
+
|
|
111
|
+ Pattern pattern = Pattern.compile(name);
|
|
112
|
+
|
|
113
|
+ Matcher matcher = pattern.matcher(name);
|
|
114
|
+
|
|
115
|
+ return matcher.find();
|
|
116
|
+ }
|
39
|
117
|
}
|