|
@@ -1,39 +1,69 @@
|
1
|
1
|
import java.io.File;
|
|
2
|
+import java.io.FileWriter;
|
2
|
3
|
import java.io.IOException;
|
3
|
4
|
import java.util.Scanner;
|
|
5
|
+import java.util.regex.Matcher;
|
|
6
|
+import java.util.regex.Pattern;
|
4
|
7
|
|
5
|
8
|
/**
|
6
|
9
|
* Created by thook on 10/7/15.
|
7
|
10
|
*/
|
8
|
11
|
public class HamletParser {
|
9
|
12
|
|
10
|
|
- private String hamletData;
|
|
13
|
+ private static String hamletData;
|
11
|
14
|
|
12
|
|
- public HamletParser(){
|
13
|
|
- this.hamletData = loadFile();
|
|
15
|
+ HamletParser(){
|
|
16
|
+ hamletData = loadFile();
|
14
|
17
|
}
|
15
|
18
|
|
16
|
|
- private String loadFile(){
|
|
19
|
+ public 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
|
|
-
|
21
|
23
|
try(Scanner scanner = new Scanner(file)){
|
22
|
24
|
while(scanner.hasNextLine()){
|
23
|
25
|
String line = scanner.nextLine();
|
24
|
26
|
result.append(line).append("\n");
|
25
|
27
|
}
|
26
|
|
-
|
27
|
28
|
scanner.close();
|
28
|
29
|
}catch(IOException e){
|
29
|
30
|
e.printStackTrace();
|
30
|
31
|
}
|
31
|
|
-
|
32
|
32
|
return result.toString();
|
33
|
33
|
}
|
34
|
34
|
|
35
|
|
- public String getHamletData(){
|
36
|
|
- return hamletData;
|
|
35
|
+ public static String changeHamletToLeon(String string) {
|
|
36
|
+ StringBuffer sb = new StringBuffer();
|
|
37
|
+ String hamletPattern = "([Hh][Aa][Mm][Ll][Ee][Tt])";
|
|
38
|
+ Pattern pattern = Pattern.compile(hamletPattern);
|
|
39
|
+ Matcher matcher = pattern.matcher(string);
|
|
40
|
+ while (matcher.find()) {
|
|
41
|
+ matcher.appendReplacement(sb, "Leon");
|
|
42
|
+ }
|
|
43
|
+ return matcher.appendTail(sb).toString();
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public static String changeHoratioToTariq(String string) {
|
|
47
|
+ StringBuffer sb = new StringBuffer();
|
|
48
|
+ String horatioPattern = "([Hh][Oo][Rr][Aa][Tt][Ii][Oo])";
|
|
49
|
+ Pattern pattern = Pattern.compile(horatioPattern);
|
|
50
|
+ Matcher matcher = pattern.matcher(string);
|
|
51
|
+ while (matcher.find()) {
|
|
52
|
+ matcher.appendReplacement(sb, "Tariq");
|
|
53
|
+ }
|
|
54
|
+ return matcher.appendTail(sb).toString();
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ private void changeEverythingAndPutInFile() throws IOException {
|
|
58
|
+ String fixedFile = changeHamletToLeon(changeHoratioToTariq(hamletData));
|
|
59
|
+ FileWriter fileWriter = new FileWriter("src/main/resources/zipcodeHamlet.txt", true);
|
|
60
|
+ fileWriter.write(fixedFile);
|
|
61
|
+ fileWriter.close();
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public static void main(String[] args) throws IOException {
|
|
65
|
+ HamletParser h = new HamletParser();
|
|
66
|
+ h.changeEverythingAndPutInFile();
|
37
|
67
|
}
|
38
|
68
|
|
39
|
69
|
}
|