Katrice Williams-Dredden hace 6 años
padre
commit
abe1bc4a26
Se han modificado 4 ficheros con 5511 adiciones y 0 borrados
  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 Ver fichero

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>regex</artifactId>
9 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 23
     <dependencies>
12 24
         <dependency>

+ 77
- 0
src/main/java/HamletParser.java Ver fichero

@@ -1,18 +1,29 @@
1
+
2
+
1 3
 import java.io.File;
2 4
 import java.io.IOException;
3 5
 import java.util.Scanner;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
4 8
 
5 9
 /**
6 10
  * Created by thook on 10/7/15.
7 11
  */
8 12
 public class HamletParser {
9 13
 
14
+    private String[] words;
10 15
     private String hamletData;
11 16
 
12 17
     public HamletParser(){
18
+
13 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 27
     private String loadFile(){
17 28
         ClassLoader classLoader = getClass().getClassLoader();
18 29
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
@@ -32,8 +43,74 @@ public class HamletParser {
32 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 60
     public String getHamletData(){
61
+
36 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 Ver fichero

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -15,17 +16,55 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 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 33
     @Test
21 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 48
     @Test
25 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 59
     @Test
29 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
La diferencia del archivo ha sido suprimido porque es demasiado grande
Ver fichero