|
@@ -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,65 @@ public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
11
|
13
|
|
12
|
|
- public HamletParser(){
|
|
14
|
+
|
|
15
|
+ public HamletParser() {
|
13
|
16
|
this.hamletData = loadFile();
|
14
|
17
|
}
|
15
|
18
|
|
16
|
|
- private String loadFile(){
|
|
19
|
+ private String loadFile() {
|
17
|
20
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
21
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
22
|
StringBuilder result = new StringBuilder("");
|
20
|
23
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
24
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
25
|
+ while (scanner.hasNextLine()) {
|
23
|
26
|
String line = scanner.nextLine();
|
24
|
27
|
result.append(line).append("\n");
|
25
|
28
|
}
|
26
|
29
|
|
27
|
30
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
31
|
+ } catch (IOException e) {
|
29
|
32
|
e.printStackTrace();
|
30
|
33
|
}
|
31
|
34
|
|
32
|
35
|
return result.toString();
|
33
|
36
|
}
|
34
|
37
|
|
35
|
|
- public String getHamletData(){
|
|
38
|
+ public String getHamletData() {
|
36
|
39
|
return hamletData;
|
37
|
40
|
}
|
38
|
41
|
|
39
|
|
-}
|
|
42
|
+ public String changeHamletToLeon(String input) {
|
|
43
|
+ Pattern smallCase = Pattern.compile("Hamlet");
|
|
44
|
+ Matcher m1 = smallCase.matcher(input);
|
|
45
|
+ String result1 = m1.replaceAll("Leon");
|
|
46
|
+
|
|
47
|
+ Pattern upperCase = Pattern.compile("HAMLET");
|
|
48
|
+ Matcher m2 = upperCase.matcher(result1);
|
|
49
|
+ String result2 = m2.replaceAll("LEON");
|
|
50
|
+ return result2;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public String changeHoratioToTariq(String input) {
|
|
54
|
+ Pattern smallCase = Pattern.compile("Horatio");
|
|
55
|
+ Matcher m1 = smallCase.matcher(input);
|
|
56
|
+ String result1 = m1.replaceAll("Tariq");
|
|
57
|
+
|
|
58
|
+ Pattern upperCase = Pattern.compile("HORATIO");
|
|
59
|
+ Matcher m2 = upperCase.matcher(result1);
|
|
60
|
+ String result2 = m2.replaceAll("TARIQ");
|
|
61
|
+ return result2;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public boolean findHamlet(String input) {
|
|
65
|
+ if (hamletData.contains("HAMLET") || hamletData.contains("Hamlet")) {
|
|
66
|
+ }
|
|
67
|
+ return true;
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ public boolean findHoratio(String input) {
|
|
71
|
+ if (hamletData.contains("HORATIO") || hamletData.contains("Horatio")) {
|
|
72
|
+ }
|
|
73
|
+ return true;
|
|
74
|
+ }
|
|
75
|
+}
|