|
@@ -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,11 +11,13 @@ public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
11
|
13
|
|
12
|
|
- public HamletParser(){
|
|
14
|
+ public HamletParser()
|
|
15
|
+ {
|
13
|
16
|
this.hamletData = loadFile();
|
14
|
17
|
}
|
15
|
18
|
|
16
|
|
- private String loadFile(){
|
|
19
|
+ private String loadFile()
|
|
20
|
+ {
|
17
|
21
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
22
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
23
|
StringBuilder result = new StringBuilder("");
|
|
@@ -32,8 +36,43 @@ public class HamletParser {
|
32
|
36
|
return result.toString();
|
33
|
37
|
}
|
34
|
38
|
|
35
|
|
- public String getHamletData(){
|
|
39
|
+ public String getHamletData()
|
|
40
|
+ {
|
36
|
41
|
return hamletData;
|
37
|
42
|
}
|
38
|
43
|
|
|
44
|
+ public void changeHamletToLeon() {
|
|
45
|
+ if(findHamlet()) {
|
|
46
|
+ Matcher matcher = matchingwords("Hamlet");
|
|
47
|
+ matcher.replaceAll("Leon");
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ private Matcher matchingwords(String hamlet)
|
|
52
|
+ {
|
|
53
|
+ Pattern pattern = Pattern.compile(hamlet);
|
|
54
|
+ Matcher matcher = pattern.matcher(this.hamletData.toLowerCase());
|
|
55
|
+ return matcher;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public void changeHoratioToTariq() {
|
|
59
|
+ if(findHamlet()) {
|
|
60
|
+ Matcher matcher = matchingwords("Horatio");
|
|
61
|
+ matcher.replaceAll("Tariq");
|
|
62
|
+ }
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public boolean findHoratio() {
|
|
66
|
+ Matcher matcher = matchingwords("horatio");
|
|
67
|
+ return matcher.find();
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ public boolean findHamlet() {
|
|
71
|
+ /* String str="((Hamlet)(.+?)";
|
|
72
|
+ Pattern pattern =Pattern.compile(str) ;*/
|
|
73
|
+ Matcher matcher = matchingwords("hamlet" );
|
|
74
|
+ return matcher.find();
|
|
75
|
+ }
|
39
|
76
|
}
|
|
77
|
+
|
|
78
|
+
|