Katrice Williams-Dredden 6 years ago
parent
commit
abe1bc4a26
4 changed files with 5511 additions and 0 deletions
  1. 12
    0
      pom.xml
  2. 77
    0
      src/main/java/HamletParser.java
  3. 39
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml View File

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>regex</artifactId>
8
     <artifactId>regex</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
 
22
 
11
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 77
- 0
src/main/java/HamletParser.java View File

1
+
2
+
1
 import java.io.File;
3
 import java.io.File;
2
 import java.io.IOException;
4
 import java.io.IOException;
3
 import java.util.Scanner;
5
 import java.util.Scanner;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
4
 
8
 
5
 /**
9
 /**
6
  * Created by thook on 10/7/15.
10
  * Created by thook on 10/7/15.
7
  */
11
  */
8
 public class HamletParser {
12
 public class HamletParser {
9
 
13
 
14
+    private String[] words;
10
     private String hamletData;
15
     private String hamletData;
11
 
16
 
12
     public HamletParser(){
17
     public HamletParser(){
18
+
13
         this.hamletData = loadFile();
19
         this.hamletData = loadFile();
14
     }
20
     }
15
 
21
 
22
+    public HamletParser(String input){
23
+        this.hamletData = input;
24
+        this.words = input.split("\\W");
25
+    }
26
+
16
     private String loadFile(){
27
     private String loadFile(){
17
         ClassLoader classLoader = getClass().getClassLoader();
28
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
29
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
32
         return result.toString();
43
         return result.toString();
33
     }
44
     }
34
 
45
 
46
+    //I know I need pattern and I know I need matching
47
+//    public static void changeHamletToLeon(String hamlet, String file2Check){
48
+//
49
+//        Pattern checkHamletRegex = Pattern.compile(hamlet);
50
+//        Matcher regexMatcher = checkHamletRegex.matcher(file2Check);
51
+//
52
+//        while(regexMatcher.find()){
53
+//            if(regexMatcher.group().length()!=0){
54
+//
55
+//            }
56
+//        }
57
+//    }
58
+
59
+
35
     public String getHamletData(){
60
     public String getHamletData(){
61
+
36
         return hamletData;
62
         return hamletData;
37
     }
63
     }
38
 
64
 
65
+
66
+    public void changeHamletToLeon(){
67
+        String hamletText = "Hamlet";
68
+        String leonText = "Leon";
69
+
70
+        Pattern checkRegex = Pattern.compile(hamletText);
71
+        Matcher m = checkRegex.matcher(hamletData);
72
+
73
+        hamletData = m.replaceAll(leonText);
74
+    }
75
+
76
+    public void changeHoratioToTariq(){
77
+        String horatioText = "Horatio";
78
+        String tariqText = "Tariq";
79
+
80
+        Pattern checkRegex = Pattern.compile(horatioText);
81
+        Matcher m = checkRegex.matcher(hamletData);
82
+
83
+        hamletData = m.replaceAll(tariqText);
84
+    }
85
+
86
+    public boolean findHamlet(){
87
+        boolean findHamlet = false;
88
+
89
+        String hamletString = "Hamlet";
90
+
91
+        Pattern checkHamletRegex = Pattern.compile(hamletString);
92
+        Matcher regexHamletMatcher = checkHamletRegex.matcher(hamletData);
93
+
94
+        if(regexHamletMatcher.find()){
95
+            findHamlet = true;
96
+        }
97
+
98
+        return findHamlet;
99
+    }
100
+
101
+    public boolean findHoratio(){
102
+        boolean findHoratio = false;
103
+
104
+        String horatioString = "Horatio";
105
+
106
+        Pattern checkHoratioRegex = Pattern.compile(horatioString);
107
+        Matcher regexHoratioMatcher = checkHoratioRegex.matcher(hamletData);
108
+
109
+        if(regexHoratioMatcher.find()){
110
+            findHoratio = true;
111
+        }
112
+
113
+        return findHoratio;
114
+    }
115
+
39
 }
116
 }

+ 39
- 0
src/test/java/HamletParserTest.java View File

1
+import org.junit.Assert;
1
 import org.junit.Before;
2
 import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
15
 
16
 
16
     @Test
17
     @Test
17
     public void testChangeHamletToLeon() {
18
     public void testChangeHamletToLeon() {
19
+        String input = "LAERTES wounds Hamlet; then in scuffling, they change rapiers, and Hamlet wounds LAERTES";
20
+        String expected = "LAERTES wounds Leon; then in scuffling, they change rapiers, and Leon wounds LAERTES";
21
+
22
+        HamletParser hamletParser = new HamletParser(input);
23
+
24
+        //When
25
+        hamletParser.changeHamletToLeon();
26
+
27
+        //Then
28
+        String actual = hamletParser.getHamletData();
29
+        Assert.assertEquals(expected, actual);
30
+
18
     }
31
     }
19
 
32
 
20
     @Test
33
     @Test
21
     public void testChangeHoratioToTariq() {
34
     public void testChangeHoratioToTariq() {
35
+        String input = "Horatio needs to change to Horatio";
36
+        String expected = "Tariq needs to change to Tariq";
37
+
38
+        HamletParser hamletParser = new HamletParser(input);
39
+
40
+        //When
41
+        hamletParser.changeHoratioToTariq();
42
+
43
+        //Then
44
+        String actual = hamletParser.getHamletData();
45
+        Assert.assertEquals(expected, actual);
22
     }
46
     }
23
 
47
 
24
     @Test
48
     @Test
25
     public void testFindHoratio() {
49
     public void testFindHoratio() {
50
+        //Given
51
+        boolean expected = true;
52
+        HamletParser hamletParser = new HamletParser();
53
+        //When
54
+        boolean actual = hamletParser.findHamlet();
55
+        //Then
56
+        Assert.assertEquals(expected, actual);
26
     }
57
     }
27
 
58
 
28
     @Test
59
     @Test
29
     public void testFindHamlet() {
60
     public void testFindHamlet() {
61
+
62
+        //Given
63
+        boolean expected = true;
64
+        HamletParser hamletParser = new HamletParser();
65
+        //When
66
+        boolean actual = hamletParser.findHoratio();
67
+        //Then
68
+        Assert.assertEquals(expected, actual);
30
     }
69
     }
31
 }
70
 }

+ 5383
- 0
target/classes/hamlet.txt
File diff suppressed because it is too large
View File