|
@@ -1,6 +1,9 @@
|
1
|
1
|
import java.io.File;
|
2
|
2
|
import java.io.IOException;
|
|
3
|
+import java.util.Arrays;
|
3
|
4
|
import java.util.Scanner;
|
|
5
|
+import java.util.regex.Pattern;
|
|
6
|
+import java.util.regex.Matcher;
|
4
|
7
|
|
5
|
8
|
/**
|
6
|
9
|
* Created by thook on 10/7/15.
|
|
@@ -8,11 +11,18 @@ import java.util.Scanner;
|
8
|
11
|
public class HamletParser {
|
9
|
12
|
|
10
|
13
|
private String hamletData;
|
|
14
|
+ private String[] words;
|
11
|
15
|
|
12
|
16
|
public HamletParser(){
|
13
|
17
|
this.hamletData = loadFile();
|
14
|
18
|
}
|
15
|
19
|
|
|
20
|
+ // My constructor
|
|
21
|
+ public HamletParser(String input) {
|
|
22
|
+ this.hamletData = input;
|
|
23
|
+ this.words = input.split("\\W");
|
|
24
|
+ }
|
|
25
|
+
|
16
|
26
|
private String loadFile(){
|
17
|
27
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
28
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
|
@@ -23,9 +33,8 @@ public class HamletParser {
|
23
|
33
|
String line = scanner.nextLine();
|
24
|
34
|
result.append(line).append("\n");
|
25
|
35
|
}
|
26
|
|
-
|
27
|
36
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
37
|
+ } catch(IOException e) {
|
29
|
38
|
e.printStackTrace();
|
30
|
39
|
}
|
31
|
40
|
|
|
@@ -36,4 +45,58 @@ public class HamletParser {
|
36
|
45
|
return hamletData;
|
37
|
46
|
}
|
38
|
47
|
|
|
48
|
+ public void changeHamletToLeon() {
|
|
49
|
+ String HamletString = "Hamlet";
|
|
50
|
+ String LeonString = "Leon";
|
|
51
|
+
|
|
52
|
+ Pattern HamletPattern = Pattern.compile(HamletString);
|
|
53
|
+ Matcher HamletMatcher = HamletPattern.matcher(hamletData);
|
|
54
|
+ hamletData = HamletMatcher.replaceAll(LeonString);
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ public void changeHoratioToTariq() {
|
|
58
|
+ String HoratioString = "Horatio";
|
|
59
|
+ String TariqString = "Tariq";
|
|
60
|
+
|
|
61
|
+ Pattern HoratioPattern = Pattern.compile(HoratioString);
|
|
62
|
+ Matcher HoratioMatcher = HoratioPattern.matcher(hamletData);
|
|
63
|
+ hamletData = HoratioMatcher.replaceAll(TariqString);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ public boolean findHoratio() {
|
|
67
|
+ boolean findHoratio = false;
|
|
68
|
+
|
|
69
|
+ String HoratioString = "Horatio";
|
|
70
|
+ Pattern HoratioPattern = Pattern.compile(HoratioString);
|
|
71
|
+ Matcher HoratioMatcher = HoratioPattern.matcher(hamletData);
|
|
72
|
+
|
|
73
|
+ if (HoratioMatcher.find()) {
|
|
74
|
+ findHoratio = true;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ return findHoratio;
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ public boolean findHamlet() {
|
|
81
|
+ boolean findHamlet = false;
|
|
82
|
+
|
|
83
|
+ String HamletString = "Horatio";
|
|
84
|
+ Pattern HamletPattern = Pattern.compile(HamletString);
|
|
85
|
+ Matcher HamletMatcher = HamletPattern.matcher(hamletData);
|
|
86
|
+
|
|
87
|
+ if (HamletMatcher.find()) {
|
|
88
|
+ findHamlet = true;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ return findHamlet;
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ public static void main(String[] args) {
|
|
95
|
+ String inputString = "Change hamleta;ldsfjkhamletHamlet and Hamlet and also Hamlet to Leon";
|
|
96
|
+ HamletParser hamletParser = new HamletParser(inputString);
|
|
97
|
+ System.out.println(hamletParser.getHamletData());
|
|
98
|
+ hamletParser.changeHamletToLeon();
|
|
99
|
+ System.out.println(hamletParser.getHamletData());
|
|
100
|
+ }
|
|
101
|
+
|
39
|
102
|
}
|