#24 Completed Hamlet parser lab.

otevřený
jonathan-hinds chce sloučit 1 revizí z větve jonathan-hinds/ZCW-Regex-Hamlet-Parser:master do větve master

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

+ 37
- 1
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.
@@ -16,7 +18,7 @@ public class HamletParser {
16 18
     private String loadFile(){
17 19
         ClassLoader classLoader = getClass().getClassLoader();
18 20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
21
+        StringBuilder result = new StringBuilder();
20 22
 
21 23
         try(Scanner scanner = new Scanner(file)){
22 24
             while(scanner.hasNextLine()){
@@ -36,4 +38,38 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+    public String swapWords(String input, String replace, String replacement){
42
+        Pattern pattern = Pattern.compile("\\w+(\\W+)?");
43
+        Matcher matcher = pattern.matcher(input);
44
+        String result = "";
45
+        while(matcher.find()){
46
+            String word = matcher.group();
47
+            String punctuation = findPunctuation(word);
48
+            if(!word.trim().equals(replace) && !word.trim().equals(replace.toLowerCase())){
49
+                result += word;
50
+            } else{
51
+                result += replacement + punctuation;
52
+            }
53
+        }
54
+        return result.trim();
55
+    }
56
+
57
+    public String findPunctuation(String testString) {
58
+        String punctuation = "";
59
+
60
+        Pattern pattern1 = Pattern.compile("\\W+");
61
+        Matcher matcher1 = pattern1.matcher(testString);
62
+
63
+        if(matcher1.find()){
64
+            punctuation = matcher1.group();
65
+        }
66
+        return punctuation;
67
+    }
68
+
69
+    public boolean findName(String input, String name){
70
+        Pattern pattern = Pattern.compile(name);
71
+        Matcher matcher = pattern.matcher(input);
72
+
73
+        return matcher.find();
74
+    }
39 75
 }

+ 45
- 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,61 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        String testString = "Hamlet is a really terrible story.";
20
+        String acutal = hamletParser.swapWords(testString, "Hamlet", "Leon");
21
+        String expect = "Leon is a really terrible story.";
22
+
23
+        Assert.assertEquals(expect, acutal);
24
+    }
25
+
26
+    @Test
27
+    public void findPunctuation(){
28
+        String testString = "Hamlet.";
29
+        String expect = ".";
30
+        String actual = hamletParser.findPunctuation(testString);
31
+
32
+        Assert.assertEquals(expect, actual);
18 33
     }
19 34
 
20 35
     @Test
21 36
     public void testChangeHoratioToTariq() {
37
+        String testString = "Horatio is a persons name?";
38
+        String acutal = hamletParser.swapWords(testString, "Horatio", "Tariq");
39
+        String expect = "Tariq is a persons name?";
40
+
41
+        Assert.assertEquals(expect, acutal);
22 42
     }
23 43
 
24 44
     @Test
25 45
     public void testFindHoratio() {
46
+        String testString = "Horatio is a persons name?";
47
+        boolean acutal = hamletParser.findName(testString,"Horatio");
48
+        boolean expect = true;
49
+
50
+        Assert.assertEquals(expect, acutal);
26 51
     }
27 52
 
28 53
     @Test
29 54
     public void testFindHamlet() {
55
+        String testString = "Hamlet is a persons name?";
56
+        boolean acutal = hamletParser.findName(testString,"Hamlet");
57
+        boolean expect = true;
58
+
59
+        Assert.assertEquals(expect, acutal);
60
+    }
61
+
62
+    @Test
63
+    public void testFindHoratioFail() {
64
+        String testString = "Hamlet is a persons name?";
65
+        boolean actual = hamletParser.findName(testString,"Horatio");
66
+        Assert.assertFalse(actual);
67
+    }
68
+
69
+    @Test
70
+    public void testFindHamletFail() {
71
+        String testString = "Horatio is a persons name?";
72
+        boolean actual = hamletParser.findName(testString,"Hamlet");
73
+
74
+        Assert.assertFalse(actual);
30 75
     }
31 76
 }

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