Explorar el Código

Merge 5f14e5c41267d5ba83a9002a19c0bd7cb79bc1cf into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

JoeHendricks415 hace 6 años
padre
commit
e4c7cff780
Ninguna cuenta está vinculada al correo electrónico del colaborador

+ 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>

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

@@ -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.
@@ -8,6 +10,7 @@ import java.util.Scanner;
8 10
 public class HamletParser {
9 11
 
10 12
     private String hamletData;
13
+    private int counter = 0;
11 14
 
12 15
     public HamletParser(){
13 16
         this.hamletData = loadFile();
@@ -33,7 +36,65 @@ public class HamletParser {
33 36
     }
34 37
 
35 38
     public String getHamletData(){
39
+
36 40
         return hamletData;
37 41
     }
38 42
 
43
+    public boolean findHoratio(){
44
+        return findMatch("[Hh][Oo][Rr][Aa][Tt][Ii][Oo]", hamletData);
45
+    }
46
+
47
+
48
+    public boolean findHamlet(){
49
+        return findMatch("[Hh][Aa][Mm][Ll][Ee][Tt]", hamletData);
50
+    }
51
+
52
+    public String changeHamletToLeon(String stringBook){
53
+
54
+        Pattern patternReplace = Pattern.compile("[Hh][a][m][l][e][t]");
55
+        Matcher matcher = patternReplace.matcher(stringBook);
56
+        String finalReplace = matcher.replaceAll("Leon");
57
+
58
+        Pattern patternReplaceCaps = Pattern.compile("HAMLET");
59
+        Matcher matcherCaps = patternReplaceCaps.matcher(finalReplace);
60
+        String finalReplaceCaps = matcherCaps.replaceAll("LEON");
61
+
62
+        return finalReplaceCaps;
63
+
64
+    }
65
+
66
+    public String changeHoratioToTariq(String stringBook){
67
+
68
+        Pattern patternReplace = Pattern.compile("[Hh][o][r][a][t][i][o]");
69
+        Matcher matcher = patternReplace.matcher(stringBook);
70
+        String finalReplace = matcher.replaceAll("Tariq");
71
+
72
+        Pattern patternReplaceCaps = Pattern.compile("HORATIO");
73
+        Matcher matcherCaps = patternReplaceCaps.matcher(finalReplace);
74
+        String finalReplaceCaps = matcherCaps.replaceAll("TARIQ");
75
+
76
+        return finalReplaceCaps;
77
+    }
78
+
79
+    public int getCounterTimeSeen(){
80
+        return counter;
81
+    }
82
+
83
+
84
+    public boolean findMatch(String regexSearch, String hamletData){
85
+
86
+        Pattern checkRegex = Pattern.compile(regexSearch);
87
+
88
+        Matcher regexMatcher = checkRegex.matcher(hamletData);
89
+
90
+        boolean findMatch = false;
91
+
92
+        while(regexMatcher.find()){
93
+            findMatch = true;
94
+            counter++;
95
+        }
96
+
97
+        return findMatch;
98
+    }
99
+
39 100
 }

+ 1
- 0
src/main/resources/Leon.txt Ver fichero

@@ -0,0 +1 @@
1
+Hamlet is a great person. Hamlet likes to eat cheese.

+ 1
- 0
src/main/resources/Wilhem.txt Ver fichero

@@ -0,0 +1 @@
1
+Horatio is a great person. Horatio likes to eat cheese.

+ 33
- 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
 
@@ -6,26 +7,58 @@ import static org.junit.Assert.*;
6 7
 public class HamletParserTest {
7 8
     private String hamletText;
8 9
     private HamletParser hamletParser;
10
+    private String leonText;
11
+    private String tariqText;
9 12
 
10 13
     @Before
11 14
     public void setUp() {
15
+
12 16
         this.hamletParser = new HamletParser();
13 17
         this.hamletText = hamletParser.getHamletData();
18
+        this.leonText = hamletParser.getHamletData();
19
+        this.tariqText = hamletParser.getHamletData();
20
+
14 21
     }
15 22
 
16 23
     @Test
17 24
     public void testChangeHamletToLeon() {
25
+        String stringTest = "Hamlet is a cool person because HAMLET likes to eat cheese.";
26
+
27
+        String expected = "Leon is a cool person because LEON likes to eat cheese.";
28
+        String actual = hamletParser.changeHamletToLeon(stringTest);
29
+
30
+        Assert.assertEquals(expected, actual);
18 31
     }
19 32
 
20 33
     @Test
21 34
     public void testChangeHoratioToTariq() {
35
+        String stringTest = "Horatio is a cool person because HORATIO likes to eat cheese.";
36
+
37
+        String expected = "Tariq is a cool person because TARIQ likes to eat cheese.";
38
+        String actual = hamletParser.changeHoratioToTariq(stringTest);
39
+
40
+        Assert.assertEquals(expected, actual);
22 41
     }
23 42
 
24 43
     @Test
25 44
     public void testFindHoratio() {
45
+
46
+        Assert.assertTrue(hamletParser.findHoratio());
26 47
     }
27 48
 
28 49
     @Test
29 50
     public void testFindHamlet() {
51
+
52
+        Assert.assertTrue(hamletParser.findHamlet());
53
+    }
54
+
55
+    @Test
56
+    public void getCounterTest(){
57
+        hamletParser.findMatch("[Hh][Aa][Mm][Ll][Ee][Tt]", hamletText);
58
+
59
+        int expected = 472;
60
+        int actual = hamletParser.getCounterTimeSeen();
61
+
62
+        Assert.assertEquals(expected, actual);
30 63
     }
31 64
 }

+ 1
- 0
target/classes/Leon.txt Ver fichero

@@ -0,0 +1 @@
1
+Hamlet is a great person. Hamlet likes to eat cheese.

+ 1
- 0
target/classes/Wilhem.txt Ver fichero

@@ -0,0 +1 @@
1
+Horatio is a great person. Horatio likes to eat cheese.

+ 5383
- 0
target/classes/hamlet.txt
La diferencia del archivo ha sido suprimido porque es demasiado grande
Ver fichero