|
@@ -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,90 @@ public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
11
|
13
|
|
12
|
|
- public HamletParser(){
|
|
14
|
+ public HamletParser() {
|
13
|
15
|
this.hamletData = loadFile();
|
14
|
16
|
}
|
15
|
17
|
|
16
|
|
- private String loadFile(){
|
|
18
|
+
|
|
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(){
|
36
|
|
- return hamletData;
|
|
38
|
+ public static String replaceHamletToLeon(String word) {
|
|
39
|
+ Pattern pattern = Pattern.compile("\\w+");
|
|
40
|
+ Matcher matcher = pattern.matcher(word);
|
|
41
|
+ StringBuilder builder = new StringBuilder();
|
|
42
|
+ while (matcher.find()) {
|
|
43
|
+ String result = matcher.group();
|
|
44
|
+ if (result.equals("Hamlet")) {
|
|
45
|
+ builder.append("Leon ");
|
|
46
|
+ } else if (result.equals("hamlet")) {
|
|
47
|
+ builder.append("leon ");
|
|
48
|
+
|
|
49
|
+ } else {
|
|
50
|
+ builder.append(result + " ");
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ }
|
|
54
|
+ return builder.toString().trim();
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ public static String punctuationFinder(String word) {
|
|
60
|
+ Pattern newPattern = Pattern.compile("\\.");
|
|
61
|
+ Matcher newMatcher = newPattern.matcher(word);
|
|
62
|
+ String characters = "";
|
|
63
|
+ if (newMatcher.find()) {
|
|
64
|
+ characters = newMatcher.group();
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ return characters;
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ public static String replaceHoratioToTariq(String word) {
|
|
73
|
+ Pattern pattern2 = Pattern.compile("\\w+");
|
|
74
|
+ Matcher matcher2 = pattern2.matcher(word);
|
|
75
|
+ StringBuilder builder = new StringBuilder();
|
|
76
|
+ while (matcher2.find()) {
|
|
77
|
+ String result = matcher2.group();
|
|
78
|
+ if (result.equals("Horatio")) {
|
|
79
|
+ builder.append("Tariq ");
|
|
80
|
+ } else if (result.equals("horatio")) {
|
|
81
|
+ builder.append("tariq ");
|
|
82
|
+
|
|
83
|
+ } else {
|
|
84
|
+ builder.append(result + " ");
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ }
|
|
88
|
+ return builder.toString().trim();
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ public boolean findName(String testString, String horatio) {
|
|
92
|
+ Pattern pattern = Pattern.compile(testString);
|
|
93
|
+ Matcher matcher = pattern.matcher(horatio);
|
|
94
|
+ return matcher.find();
|
37
|
95
|
}
|
38
|
96
|
|
|
97
|
+ public String getHamletData() {
|
|
98
|
+ return "";
|
|
99
|
+ }
|
39
|
100
|
}
|