瀏覽代碼

finished Hamlet

Tommy Rogers 6 年之前
父節點
當前提交
4193a1d560
共有 4 個文件被更改,包括 5442 次插入0 次删除
  1. 12
    0
      pom.xml
  2. 17
    0
      src/main/java/HamletParser.java
  3. 30
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml 查看文件

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

+ 17
- 0
src/main/java/HamletParser.java 查看文件

@@ -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.
@@ -36,4 +38,19 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+    public String changeHamletToLeon(String search, String replace){
42
+        Pattern patt = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
43
+        Matcher m = patt.matcher(hamletData);
44
+        return m.replaceAll(replace);
45
+    }
46
+
47
+    public int findWord(CharSequence hamletData ,String search) {
48
+        int count = 0;
49
+        Pattern patt = Pattern.compile("^" + search + "", Pattern.CASE_INSENSITIVE);
50
+        Matcher m = patt.matcher(hamletData);
51
+     while (m.find())
52
+            count++;
53
+     return count;
54
+    }
55
+
39 56
 }

+ 30
- 0
src/test/java/HamletParserTest.java 查看文件

@@ -15,17 +15,47 @@ public class HamletParserTest {
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
+        String leon = hamletParser.changeHamletToLeon("Hamlet", "Leon");
19
+
20
+        int expected = hamletParser.findWord(hamletText , "Hamlet");
21
+        int actual = hamletParser.findWord(leon , "Hamlet");
22
+
23
+        System.out.println(leon);
24
+       // System.out.println(hamletText);
25
+
26
+
27
+
28
+        assertEquals(expected, actual);
18 29
     }
19 30
 
20 31
     @Test
21 32
     public void testChangeHoratioToTariq() {
33
+        String leon = hamletParser.changeHamletToLeon("Horatio", "Tariq");
34
+
35
+        int expected = hamletParser.findWord(hamletText , "Horatio");
36
+        int actual = hamletParser.findWord(leon , "Tariq");
37
+
38
+        System.out.println(leon);
39
+        // System.out.println(hamletText);
40
+
41
+
42
+
43
+        assertEquals(expected, actual);
22 44
     }
23 45
 
24 46
     @Test
25 47
     public void testFindHoratio() {
48
+        int expected = 158;
49
+        int actual = hamletParser.findWord(hamletText,"Horatio");
50
+
51
+        assertEquals(expected, actual);
26 52
     }
27 53
 
28 54
     @Test
29 55
     public void testFindHamlet() {
56
+        int expected = 472;
57
+        int actual = hamletParser.findWord(hamletText,"Hamlet");
58
+
59
+        assertEquals(expected, actual);
30 60
     }
31 61
 }

+ 5383
- 0
target/classes/hamlet.txt
文件差異過大導致無法顯示
查看文件