|
@@ -3,6 +3,9 @@ import java.io.IOException;
|
3
|
3
|
import java.util.Scanner;
|
4
|
4
|
import java.lang.*;
|
5
|
5
|
import java.util.*;
|
|
6
|
+import java.util.regex.Pattern;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+
|
6
|
9
|
/**
|
7
|
10
|
* Created by thook on 10/7/15.
|
8
|
11
|
*/
|
|
@@ -10,92 +13,125 @@ public class HamletParser {
|
10
|
13
|
|
11
|
14
|
private String hamletData;
|
12
|
15
|
private Formatter formatter;
|
|
16
|
+ private String hamletFile = "hamlet.txt";
|
13
|
17
|
|
14
|
18
|
public HamletParser(){
|
15
|
|
- this.hamletData = loadFile();
|
|
19
|
+ // this.hamletData = loadFile(fileName);
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ public String getHamletFile() {
|
|
23
|
+ return hamletFile;
|
16
|
24
|
}
|
17
|
25
|
|
18
|
|
- private String loadFile(){
|
|
26
|
+ protected String loadFile(String fileName){
|
19
|
27
|
ClassLoader classLoader = getClass().getClassLoader();
|
20
|
|
- File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
|
28
|
+ File file = new File(classLoader.getResource(fileName).getFile());
|
21
|
29
|
StringBuilder result = new StringBuilder("");
|
22
|
|
- StringBuilder lineResult;
|
23
|
|
- String[] wordArr;
|
24
|
30
|
|
25
|
31
|
try(Scanner scanner = new Scanner(file)){
|
26
|
32
|
while(scanner.hasNextLine()){
|
27
|
|
- String line = scanner.nextLine();
|
28
|
|
- wordArr = line.split(" ");
|
29
|
|
- lineResult = new StringBuilder("");
|
30
|
|
- for (String s: wordArr) {
|
31
|
|
- if (findHamlet(s)) {
|
32
|
|
- lineResult.append(changeHamletToLeon()).append(" ");
|
33
|
|
- } else if (findHoratio(s)) {
|
34
|
|
- lineResult.append(changeChangeHoratioToTariq()).append(" ");
|
35
|
|
- } else {
|
36
|
|
- lineResult.append(s).append(" ");
|
37
|
|
- }
|
38
|
|
- }
|
39
|
|
- result.append(lineResult.toString().trim());
|
|
33
|
+ String line = replaceHamletAndHoratio(scanner.nextLine());
|
|
34
|
+ result.append(line).append("\n");
|
40
|
35
|
}
|
41
|
|
-
|
42
|
36
|
scanner.close();
|
43
|
37
|
}catch(IOException e){
|
44
|
38
|
e.printStackTrace();
|
45
|
39
|
}
|
46
|
|
- openOutFile();
|
47
|
|
- addText(result.toString());
|
48
|
|
- closeOutFile();
|
49
|
40
|
return result.toString();
|
50
|
41
|
}
|
|
42
|
+// protected String loadFile(){
|
|
43
|
+// ClassLoader classLoader = getClass().getClassLoader();
|
|
44
|
+// File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
|
45
|
+// StringBuilder result = new StringBuilder("");
|
|
46
|
+// StringBuilder lineResult;
|
|
47
|
+// String[] wordArr;
|
|
48
|
+//
|
|
49
|
+// try(Scanner scanner = new Scanner(file)){
|
|
50
|
+// while(scanner.hasNextLine()){
|
|
51
|
+// String line = scanner.nextLine();
|
|
52
|
+// wordArr = line.split(" ");
|
|
53
|
+// lineResult = new StringBuilder("");
|
|
54
|
+// for (String s: wordArr) {
|
|
55
|
+// if (findHamlet(s)) {
|
|
56
|
+// lineResult.append(changeHamletToLeon()).append(" ");
|
|
57
|
+// } else if (findHoratio(s)) {
|
|
58
|
+// lineResult.append(changeChangeHoratioToTariq()).append(" ");
|
|
59
|
+// } else {
|
|
60
|
+// lineResult.append(s).append(" ");
|
|
61
|
+// }
|
|
62
|
+// }
|
|
63
|
+// result.append(lineResult.toString().trim() + "\n");
|
|
64
|
+// }
|
|
65
|
+//
|
|
66
|
+// scanner.close();
|
|
67
|
+// }catch(IOException e){
|
|
68
|
+// e.printStackTrace();
|
|
69
|
+// }
|
|
70
|
+// openOutFile();
|
|
71
|
+// addText(result.toString());
|
|
72
|
+// closeOutFile();
|
|
73
|
+// return result.toString();
|
|
74
|
+// }
|
51
|
75
|
|
52
|
76
|
public String getHamletData(){
|
53
|
77
|
return hamletData;
|
54
|
78
|
}
|
55
|
79
|
|
56
|
|
- public boolean findHamlet(String s){
|
57
|
|
- if (s.toLowerCase().matches("hamlet")){
|
58
|
|
- return true;
|
59
|
|
- } else {
|
60
|
|
- return false;
|
61
|
|
- }
|
62
|
|
- }
|
63
|
|
-
|
64
|
|
- public boolean findHoratio(String s){
|
65
|
|
- if (s.toLowerCase().matches("horatio")){
|
66
|
|
- return true;
|
67
|
|
- } else {
|
68
|
|
- return false;
|
69
|
|
- }
|
70
|
|
- }
|
71
|
|
-
|
72
|
|
- public String changeHamletToLeon(){
|
73
|
|
- return "Leon";
|
74
|
|
- }
|
75
|
|
-
|
76
|
|
- public String changeChangeHoratioToTariq(){
|
77
|
|
- return "Tariq";
|
78
|
|
- }
|
|
80
|
+// public boolean findHamlet(String s){
|
|
81
|
+// if (s.toLowerCase().matches("hamlet")){
|
|
82
|
+// return true;
|
|
83
|
+// } else {
|
|
84
|
+// return false;
|
|
85
|
+// }
|
|
86
|
+// }
|
|
87
|
+//
|
|
88
|
+// public boolean findHoratio(String s){
|
|
89
|
+// if (s.toLowerCase().matches("horatio")){
|
|
90
|
+// return true;
|
|
91
|
+// } else {
|
|
92
|
+// return false;
|
|
93
|
+// }
|
|
94
|
+// }
|
|
95
|
+//
|
|
96
|
+// public String changeHamletToLeon(){
|
|
97
|
+// return "Leon";
|
|
98
|
+// }
|
|
99
|
+//
|
|
100
|
+// public String changeChangeHoratioToTariq(){
|
|
101
|
+// return "Tariq";
|
|
102
|
+// }
|
79
|
103
|
|
80
|
104
|
/**
|
81
|
105
|
* Methods to print out to a new file
|
82
|
106
|
*/
|
83
|
|
- public void openOutFile(){
|
|
107
|
+ public void openOutFile(String fileName){
|
84
|
108
|
try {
|
85
|
|
- formatter = new Formatter("output.txt");
|
|
109
|
+ formatter = new Formatter(fileName);
|
86
|
110
|
}
|
87
|
111
|
catch (Exception e){
|
88
|
112
|
System.out.println("Error writing to file.");
|
89
|
113
|
}
|
90
|
114
|
}
|
91
|
|
-
|
92
|
|
- public String addText(String s){
|
93
|
|
- formatter.format("%s", s);
|
94
|
|
- return loadFile();
|
|
115
|
+ public String writeToFile(String output){
|
|
116
|
+ formatter.format("%s", output);
|
|
117
|
+ return output;
|
95
|
118
|
}
|
96
|
|
-
|
97
|
119
|
public void closeOutFile(){
|
98
|
120
|
formatter.close();
|
99
|
121
|
}
|
100
|
122
|
|
|
123
|
+ public String replaceHamletAndHoratio(String input) {
|
|
124
|
+// String noHamlet = input.replaceAll("(?i)hamlet", "Leon");
|
|
125
|
+// String noHoratioOrHamlet = noHamlet.replaceAll("(?i)horatio", "Tarik");
|
|
126
|
+// return noHoratioOrHamlet;
|
|
127
|
+ String returnString = input;
|
|
128
|
+ String[] replaceArr = {"Hamlet", "Horatio", "hamlet", "horatio", "HAMLET", "HORATIO"};
|
|
129
|
+ String[] changeToArr = {"Leon", "Tarik", "leon", "tarik", "LEON", "TARIK"};
|
|
130
|
+
|
|
131
|
+ for (int i = 0; i < replaceArr.length; i++){
|
|
132
|
+ returnString = returnString.replaceAll(replaceArr[i], changeToArr[i]);
|
|
133
|
+ }
|
|
134
|
+ return returnString;
|
|
135
|
+ }
|
|
136
|
+
|
101
|
137
|
}
|