|
@@ -1,39 +1,62 @@
|
|
1
|
+
|
1
|
2
|
import java.io.File;
|
2
|
3
|
import java.io.IOException;
|
3
|
4
|
import java.util.Scanner;
|
|
5
|
+import java.util.regex.Matcher;
|
|
6
|
+import java.util.regex.Pattern;
|
4
|
7
|
|
5
|
8
|
/**
|
6
|
9
|
* Created by thook on 10/7/15.
|
7
|
10
|
*/
|
8
|
11
|
public class HamletParser {
|
9
|
12
|
|
10
|
|
- private String hamletData;
|
|
13
|
+ String hamletData;
|
11
|
14
|
|
12
|
|
- public HamletParser(){
|
|
15
|
+ public HamletParser() {
|
13
|
16
|
this.hamletData = loadFile();
|
14
|
17
|
}
|
15
|
18
|
|
16
|
|
- private String loadFile(){
|
|
19
|
+ String hamletPattern = "[Hh][Aa][Mm][Ll][Ee][Tt]";
|
|
20
|
+ Pattern hamletC = Pattern.compile(hamletPattern);
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+ String horatioPattern = "[Hh][Oo][Rr][Aa][Tt][Ii][Oo]";
|
|
24
|
+ Pattern horatioC = Pattern.compile(horatioPattern);
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+ private String loadFile() {
|
17
|
28
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
29
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
30
|
StringBuilder result = new StringBuilder("");
|
20
|
31
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
32
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
33
|
+ while (scanner.hasNextLine()) {
|
23
|
34
|
String line = scanner.nextLine();
|
24
|
35
|
result.append(line).append("\n");
|
25
|
36
|
}
|
26
|
37
|
|
27
|
38
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
39
|
+ } catch (IOException e) {
|
29
|
40
|
e.printStackTrace();
|
30
|
41
|
}
|
31
|
42
|
|
32
|
43
|
return result.toString();
|
33
|
44
|
}
|
34
|
45
|
|
35
|
|
- public String getHamletData(){
|
|
46
|
+
|
|
47
|
+ public String getHamletData() {
|
36
|
48
|
return hamletData;
|
37
|
49
|
}
|
38
|
50
|
|
|
51
|
+ public String changeHamletToLeon(String input) {
|
|
52
|
+ Matcher hamletMatcher = hamletC.matcher(input);
|
|
53
|
+ return hamletMatcher.replaceAll("Leon");
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public String changeHoratioToTariq(String input) {
|
|
57
|
+ Matcher horatioMatcher = horatioC.matcher(input);
|
|
58
|
+ return horatioMatcher.replaceAll("Tariq");
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+
|
39
|
62
|
}
|