|
@@ -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.
|
|
@@ -9,31 +11,68 @@ public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
11
|
13
|
|
12
|
|
- public HamletParser(){
|
|
14
|
+
|
|
15
|
+ private Pattern horatioP = Pattern.compile("(\\bHoratio\\b|\\bHORATIO\\b)");
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+ public HamletParser() {
|
13
|
19
|
this.hamletData = loadFile();
|
14
|
20
|
}
|
15
|
21
|
|
16
|
|
- private String loadFile(){
|
|
22
|
+ private String loadFile() {
|
17
|
23
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
24
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
25
|
StringBuilder result = new StringBuilder("");
|
20
|
26
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
27
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
28
|
+ while (scanner.hasNextLine()) {
|
23
|
29
|
String line = scanner.nextLine();
|
24
|
30
|
result.append(line).append("\n");
|
25
|
31
|
}
|
26
|
32
|
|
27
|
33
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
34
|
+ } catch (IOException e) {
|
29
|
35
|
e.printStackTrace();
|
30
|
36
|
}
|
31
|
37
|
|
32
|
38
|
return result.toString();
|
33
|
39
|
}
|
34
|
40
|
|
35
|
|
- public String getHamletData(){
|
|
41
|
+ public String getHamletData() {
|
36
|
42
|
return hamletData;
|
37
|
43
|
}
|
38
|
44
|
|
|
45
|
+ public void changeHoratioToTariq() {
|
|
46
|
+ Pattern horatioL = Pattern.compile("Horatio");
|
|
47
|
+ Matcher tariqL = horatioL.matcher(hamletData);
|
|
48
|
+ hamletData = tariqL.replaceAll("Tariq");
|
|
49
|
+ Pattern horatioU = Pattern.compile("\\bHORATIO\\b");
|
|
50
|
+ Matcher tariqU = horatioU.matcher(hamletData);
|
|
51
|
+ hamletData = tariqU.replaceAll("TARIQ");
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public void changeHamletToLeon() {
|
|
55
|
+ Pattern hamletL = Pattern.compile("Hamlet");
|
|
56
|
+ Matcher leonL = hamletL.matcher(hamletData);
|
|
57
|
+ hamletData = leonL.replaceAll("Leon");
|
|
58
|
+ Pattern hamletU = Pattern.compile("\\bHAMLET\\b");
|
|
59
|
+ Matcher leonU = hamletU.matcher(hamletData);
|
|
60
|
+ hamletData = leonU.replaceAll("LEON");
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ public void replaceBothNames(){
|
|
64
|
+ changeHamletToLeon();
|
|
65
|
+ changeHoratioToTariq();
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ public boolean contains(String wordToLookOutFor){
|
|
69
|
+ String[] hamletArray = getHamletData().split(" ");
|
|
70
|
+ for(String word : hamletArray){
|
|
71
|
+ if(word.equals(wordToLookOutFor)){
|
|
72
|
+ return true;
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+ return false;
|
|
76
|
+ }
|
|
77
|
+
|
39
|
78
|
}
|