|
@@ -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.
|
|
@@ -8,32 +10,83 @@ import java.util.Scanner;
|
8
|
10
|
public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
|
13
|
+ private int count = 0;
|
11
|
14
|
|
12
|
|
- public HamletParser(){
|
|
15
|
+
|
|
16
|
+ public HamletParser() {
|
13
|
17
|
this.hamletData = loadFile();
|
14
|
18
|
}
|
15
|
19
|
|
16
|
|
- private String loadFile(){
|
|
20
|
+ private String loadFile() {
|
17
|
21
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
22
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
23
|
StringBuilder result = new StringBuilder("");
|
20
|
24
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
25
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
26
|
+ while (scanner.hasNextLine()) {
|
23
|
27
|
String line = scanner.nextLine();
|
24
|
28
|
result.append(line).append("\n");
|
25
|
29
|
}
|
26
|
30
|
|
27
|
31
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
32
|
+ } catch (IOException e) {
|
29
|
33
|
e.printStackTrace();
|
30
|
34
|
}
|
31
|
35
|
|
32
|
36
|
return result.toString();
|
33
|
37
|
}
|
34
|
38
|
|
35
|
|
- public String getHamletData(){
|
|
39
|
+ public String getHamletData() {
|
36
|
40
|
return hamletData;
|
37
|
41
|
}
|
38
|
42
|
|
|
43
|
+ public String changeHamletToLeon(String text) {
|
|
44
|
+
|
|
45
|
+ Pattern replacement = Pattern.compile("Hamlet");
|
|
46
|
+ Matcher match = replacement.matcher(text);
|
|
47
|
+ String hamletData1 = match.replaceAll("Leon");
|
|
48
|
+
|
|
49
|
+ Pattern replacement1 = Pattern.compile("HAMLET");
|
|
50
|
+ Matcher match1 = replacement1.matcher(hamletData1);
|
|
51
|
+ String hamletData2 = match1.replaceAll("LEON");
|
|
52
|
+
|
|
53
|
+ return hamletData2;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public String changeHoratioToTariq(String text) {
|
|
57
|
+
|
|
58
|
+ Pattern replacement = Pattern.compile("Horatio");
|
|
59
|
+ Matcher match = replacement.matcher(text);
|
|
60
|
+ String hamletData1 = match.replaceAll("Tariq");
|
|
61
|
+
|
|
62
|
+ Pattern replacement1 = Pattern.compile("HORATIO");
|
|
63
|
+ Matcher match1 = replacement1.matcher(hamletData1);
|
|
64
|
+ String hamletData2 = match1.replaceAll("TARIQ");
|
|
65
|
+
|
|
66
|
+ return hamletData2;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+ private int findMatch(String regexSearch, String text) {
|
|
71
|
+ Pattern checkName = Pattern.compile(regexSearch);
|
|
72
|
+ Matcher regexCheck = checkName.matcher(text);
|
|
73
|
+
|
|
74
|
+ while (regexCheck.find()) {
|
|
75
|
+ if (regexCheck.group().length() != 0) {
|
|
76
|
+ count++;
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+ return count;
|
|
80
|
+
|
|
81
|
+ }
|
|
82
|
+ public int findHoratio() {
|
|
83
|
+
|
|
84
|
+ return findMatch("[H|h|][O|o][R|r][A|a][T|t][I|i][O|o]", hamletData);
|
|
85
|
+
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ public int findHamlet() {
|
|
89
|
+
|
|
90
|
+ return findMatch("[H|h|][A|a][M|m][L|l][E|e][T|t]", hamletData);
|
|
91
|
+ }
|
39
|
92
|
}
|