Aleena Rose-Mathew преди 6 години
родител
ревизия
14583e54bf
променени са 4 файла, в които са добавени 5479 реда и са изтрити 11 реда
  1. 5
    0
      pom.xml
  2. 69
    6
      src/main/java/HamletParser.java
  3. 22
    5
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 5
- 0
pom.xml Целия файл

@@ -8,6 +8,11 @@
8 8
     <artifactId>regex</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <properties>
12
+        <maven.compiler.source>1.8</maven.compiler.source>
13
+        <maven.compiler.target>1.8</maven.compiler.target>
14
+    </properties>
15
+
11 16
     <dependencies>
12 17
         <dependency>
13 18
             <groupId>junit</groupId>

+ 69
- 6
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.
@@ -18,22 +20,83 @@ public class HamletParser {
18 20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 21
         StringBuilder result = new StringBuilder("");
20 22
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
23
+        try(Scanner scanner = new Scanner(file))
24
+        {
25
+            while(scanner.hasNextLine())
26
+            {
23 27
                 String line = scanner.nextLine();
24 28
                 result.append(line).append("\n");
29
+                //String horatioToTariq = changeHoratioToTariq(changeHamletToLeon(line));
30
+                //result= result.append(changeHoratioToTariq(changeHamletToLeon(line)));
31
+                //result.append("\n");
32
+                //System.out.println(result);
25 33
             }
26
-
27
-            scanner.close();
28 34
         }catch(IOException e){
29 35
             e.printStackTrace();
30 36
         }
31
-
32 37
         return result.toString();
33 38
     }
39
+    public static int changeHamletToLeon(String line)
40
+    {
41
+        int count=0;
42
+        Pattern patternHamlet =Pattern. compile("\\bHamlet\\b",Pattern.CASE_INSENSITIVE);
43
+        Matcher m1=patternHamlet.matcher(line);
44
+        String str1=((Matcher) m1).replaceAll("Leon");
45
+
46
+        Matcher m11=patternHamlet.matcher(str1);
47
+        while(m11.find())
48
+        {
49
+            count++;
50
+        }
51
+        return count;
52
+    }
34 53
 
35
-    public String getHamletData(){
54
+    public static int changeHoratioToTariq(String line) {
55
+        int count=0;
56
+        Pattern patternHoratio =Pattern.compile("\\bHoratio\\b",Pattern.CASE_INSENSITIVE);
57
+        Matcher m2=patternHoratio.matcher(line);
58
+        String str2=((Matcher) m2).replaceAll("Tariq");
59
+
60
+        Matcher m22=patternHoratio.matcher(str2);
61
+        while(m22.find())
62
+        {
63
+            count++;
64
+        }
65
+        return count;
66
+    }
67
+    public String getHamletData()
68
+    {
36 69
         return hamletData;
37 70
     }
38 71
 
72
+    public int FindHoratio()
73
+    {
74
+        int count=0;
75
+        Pattern patternHoratio =Pattern.compile("\\bHoratio\\b",Pattern.CASE_INSENSITIVE);
76
+        Matcher m2=patternHoratio.matcher(hamletData);
77
+
78
+        while(m2.find())
79
+        {
80
+            count++;
81
+        }
82
+        return count;
83
+    }
84
+    public int FindHamlet()
85
+    {
86
+        int count=0;
87
+        Pattern patternHamlet =Pattern.compile("\\bHamlet\\b",Pattern.CASE_INSENSITIVE);
88
+        Matcher m2=patternHamlet.matcher(hamletData);
89
+
90
+        while(m2.find())
91
+        {
92
+            count++;
93
+        }
94
+        return count;
95
+    }
96
+
97
+    public static void main(String[] args) {
98
+        HamletParser hamletParser=new HamletParser();
99
+        
100
+    }
101
+
39 102
 }

+ 22
- 5
src/test/java/HamletParserTest.java Целия файл

@@ -1,6 +1,10 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
4 8
 import static org.junit.Assert.*;
5 9
 
6 10
 public class HamletParserTest {
@@ -13,19 +17,32 @@ public class HamletParserTest {
13 17
         this.hamletText = hamletParser.getHamletData();
14 18
     }
15 19
 
16
-    @Test
17
-    public void testChangeHamletToLeon() {
18
-    }
19 20
 
20 21
     @Test
21
-    public void testChangeHoratioToTariq() {
22
+    public void testChangeHamletToLeonCount() {
23
+        int expected =0;
24
+        int actual=hamletParser.changeHamletToLeon(hamletText);
25
+        assertEquals(expected, actual);
22 26
     }
23 27
 
28
+
24 29
     @Test
25 30
     public void testFindHoratio() {
31
+        int expected=158;
32
+        int actual=hamletParser.FindHoratio();
33
+        Assert.assertEquals(expected, actual);
34
+    }
35
+    @Test
36
+    public void testChangeHoratioToTariqCount()
37
+    {
38
+        int expected=0;
39
+        int actual=hamletParser.changeHoratioToTariq(hamletText);
40
+        Assert.assertEquals(expected, actual);
26 41
     }
27
-
28 42
     @Test
29 43
     public void testFindHamlet() {
44
+        int expected=471;
45
+        int actual=hamletParser.FindHamlet();
46
+        Assert.assertEquals(expected, actual);
30 47
     }
31 48
 }

+ 5383
- 0
target/classes/hamlet.txt
Файловите разлики са ограничени, защото са твърде много
Целия файл