|
@@ -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,83 @@ public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
11
|
13
|
|
12
|
|
- public HamletParser(){
|
|
14
|
+ public HamletParser() {
|
13
|
15
|
this.hamletData = loadFile();
|
14
|
16
|
}
|
15
|
17
|
|
16
|
|
- private String loadFile(){
|
|
18
|
+ private String loadFile() {
|
17
|
19
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
20
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
21
|
StringBuilder result = new StringBuilder("");
|
20
|
22
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
23
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
24
|
+ while (scanner.hasNextLine()) {
|
23
|
25
|
String line = scanner.nextLine();
|
24
|
26
|
result.append(line).append("\n");
|
25
|
27
|
}
|
26
|
28
|
|
27
|
29
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
30
|
+ } catch (IOException e) {
|
29
|
31
|
e.printStackTrace();
|
30
|
32
|
}
|
31
|
33
|
|
32
|
34
|
return result.toString();
|
33
|
35
|
}
|
34
|
36
|
|
35
|
|
- public String getHamletData(){
|
|
37
|
+ public String getHamletData() {
|
36
|
38
|
return hamletData;
|
37
|
39
|
}
|
38
|
40
|
|
|
41
|
+ public String changeHamletToLeon(String hamletData) {
|
|
42
|
+ String input = hamletData;
|
|
43
|
+ Pattern p = Pattern.compile("Hamlet");
|
|
44
|
+ Matcher m = p.matcher(input);
|
|
45
|
+ String hamletToLeon = m.replaceAll("Leon");
|
|
46
|
+ Pattern upperP = Pattern.compile("HAMLET");
|
|
47
|
+ Matcher upperM = upperP.matcher(hamletToLeon);
|
|
48
|
+ String hamletToLeonUpper = upperM.replaceAll("LEON");
|
|
49
|
+
|
|
50
|
+ return hamletToLeonUpper.toString();
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public String changeHoratioToTariq(String hamletData) {
|
|
54
|
+ String input = hamletData;
|
|
55
|
+ Pattern p = Pattern.compile("Horatio");
|
|
56
|
+ Matcher m = p.matcher(input);
|
|
57
|
+ String horatioToTariq = m.replaceAll("Tariq");
|
|
58
|
+ Pattern upperP = Pattern.compile("HORATIO");
|
|
59
|
+ Matcher upperM = upperP.matcher(horatioToTariq);
|
|
60
|
+ String horatioToTariqUpper = upperM.replaceAll("TARIQ");
|
|
61
|
+
|
|
62
|
+ return horatioToTariqUpper.toString();
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public String changeAll(String hamletData) {
|
|
66
|
+ String changeAll = changeHoratioToTariq(changeHamletToLeon(hamletData));
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+ return changeAll;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+ public Integer findHalmet(String hamletData) {
|
|
74
|
+ String input = hamletData;
|
|
75
|
+ Pattern p = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
|
|
76
|
+ Matcher m = p.matcher(input);
|
|
77
|
+ int count = 0;
|
|
78
|
+ while (m.find())
|
|
79
|
+ count++;
|
|
80
|
+ return count;
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ public Integer findHoratio(String hamletData) {
|
|
84
|
+ String input = hamletData;
|
|
85
|
+ Pattern p = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
|
|
86
|
+ Matcher m = p.matcher(input);
|
|
87
|
+ int count = 0;
|
|
88
|
+ while (m.find())
|
|
89
|
+ count++;
|
|
90
|
+ return count;
|
|
91
|
+ }
|
|
92
|
+
|
39
|
93
|
}
|