De'Jon Johnson před 6 roky
rodič
revize
fc16c4693c

+ 12
- 0
pom.xml Zobrazit soubor

@@ -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>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 68
- 7
src/main/java/HamletParser.java Zobrazit soubor

@@ -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
 }

+ 52
- 0
src/test/java/HamletParserTest.java Zobrazit soubor

@@ -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,68 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        //Given
20
+        String word = "Hamlet went to the mall";
21
+
22
+        //When
23
+        String actual = HamletParser.replaceHamletToLeon(word);
24
+        String expect = "Leon went to the mall";
25
+
26
+        //Then
27
+        Assert.assertEquals(expect, actual);
28
+
29
+
18 30
     }
19 31
 
20 32
     @Test
33
+    public void testPunctuationFinder(){
34
+        //Given
35
+        String word = "I am Hamlet.";
36
+
37
+        //When
38
+        String expect = ".";
39
+        String actual = HamletParser.punctuationFinder(word);
40
+
41
+
42
+        //Then
43
+        Assert.assertEquals(expect, actual);
44
+
45
+
46
+    }
47
+
48
+
49
+
50
+    @Test
21 51
     public void testChangeHoratioToTariq() {
52
+        //Given
53
+
54
+        String word = "Horatio went to the mall";
55
+
56
+        //When
57
+        String actual = HamletParser.replaceHoratioToTariq(word);
58
+        String expect = "Tariq went to the mall";
59
+
60
+        //Then
61
+        Assert.assertEquals(expect, actual);
62
+
63
+
22 64
     }
23 65
 
24 66
     @Test
25 67
     public void testFindHoratio() {
68
+
69
+        String testString = "Horatio is a persons name?";
70
+         boolean acutal = hamletParser.findName(testString,"Horatio");
71
+       boolean expect = false;
72
+       Assert.assertEquals(expect, acutal);
26 73
     }
27 74
 
28 75
     @Test
29 76
     public void testFindHamlet() {
77
+
78
+        String testString = "Hamlet is a persons name?";
79
+     boolean acutal = hamletParser.findName(testString,"Hamlet");
80
+       boolean expect = false;
81
+      Assert.assertEquals(expect, acutal);
30 82
     }
31 83
 }

+ 5383
- 0
target/classes/hamlet.txt
Diff nebyl zobrazen, protože je příliš veliký
Zobrazit soubor