Ver código fonte

HamletParser

Navya Sanal 6 anos atrás
pai
commit
2c22f71f59

+ 58
- 7
src/main/java/HamletParser.java Ver arquivo

@@ -1,6 +1,10 @@
1
+import javax.sound.sampled.Line;
2
+import javax.xml.bind.Element;
1 3
 import java.io.File;
2 4
 import java.io.IOException;
3 5
 import java.util.Scanner;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
4 8
 
5 9
 /**
6 10
  * Created by thook on 10/7/15.
@@ -9,23 +13,32 @@ public class HamletParser {
9 13
 
10 14
     private String hamletData;
11 15
 
12
-    public HamletParser(){
16
+    StringBuilder result = new StringBuilder("");
17
+
18
+
19
+    public HamletParser()
20
+    {
13 21
         this.hamletData = loadFile();
14 22
     }
15 23
 
16 24
     private String loadFile(){
17 25
         ClassLoader classLoader = getClass().getClassLoader();
18 26
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
20 27
 
21
-        try(Scanner scanner = new Scanner(file)){
28
+        try
29
+        {
30
+            Scanner scanner = new Scanner(file);
22 31
             while(scanner.hasNextLine()){
23
-                String line = scanner.nextLine();
24
-                result.append(line).append("\n");
32
+                /*//String line = scanner.nextLine();
33
+                //String updated  = line.replaceAll("Hamlet","Leon");
34
+                //line = updated.replaceAll("Horatio","Tariq");
35
+                //result.append(line).append("\n");*/
36
+                break;
25 37
             }
26
-
27 38
             scanner.close();
28
-        }catch(IOException e){
39
+        }
40
+        catch(IOException e)
41
+        {
29 42
             e.printStackTrace();
30 43
         }
31 44
 
@@ -35,5 +48,43 @@ public class HamletParser {
35 48
     public String getHamletData(){
36 49
         return hamletData;
37 50
     }
51
+    public String ChangeHamletToLeon(String inputLine)
52
+    {
53
+        //String line ;
54
+        Pattern p = Pattern.compile("hamlet", Pattern.CASE_INSENSITIVE);
55
+        Matcher m = p.matcher(inputLine);
56
+
57
+        StringBuilder s = result.append(m.replaceAll("Leon"));
58
+        System.out.println(s);
59
+        return s.toString();
60
+
61
+    }
62
+    public StringBuilder ChangeHoratioToTariq(String inputLine)
63
+    {
64
+        //String line ;
65
+        Pattern p = Pattern.compile("\\Horatio\\b", Pattern.CASE_INSENSITIVE);
66
+        Matcher m = p.matcher(inputLine);
67
+        StringBuilder s = result.append(m.replaceAll("<h1>Tariq</h1>"));
68
+        return s;
69
+
70
+    }
71
+    public boolean findHoratio(String inputLine)
72
+    {
73
+
74
+      Pattern  p = Pattern.compile("Horatio",Pattern.CASE_INSENSITIVE);
75
+      Matcher m = p.matcher(inputLine);
76
+      return m.find();
77
+
78
+
79
+    }
80
+    public boolean findHamlet(String inputLine)
81
+    {
82
+
83
+       Pattern  p = Pattern.compile("Tariq",Pattern.CASE_INSENSITIVE);
84
+       Matcher m  = p.matcher(inputLine);
85
+       return m.find();
86
+
87
+    }
88
+
38 89
 
39 90
 }

+ 24
- 0
src/test/java/HamletParserTest.java Ver arquivo

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -7,6 +8,9 @@ public class HamletParserTest {
7 8
     private String hamletText;
8 9
     private HamletParser hamletParser;
9 10
 
11
+    HamletParser hp = new HamletParser();
12
+
13
+
10 14
     @Before
11 15
     public void setUp() {
12 16
         this.hamletParser = new HamletParser();
@@ -15,17 +19,37 @@ public class HamletParserTest {
15 19
 
16 20
     @Test
17 21
     public void testChangeHamletToLeon() {
22
+        String expected = "This is Leon";
23
+        String actual = hp.ChangeHamletToLeon(expected);
24
+        Assert.assertEquals(expected,actual);
25
+
18 26
     }
19 27
 
20 28
     @Test
21 29
     public void testChangeHoratioToTariq() {
30
+        String expected = "This is Horatio";
31
+        String actual = hp.ChangeHamletToLeon(expected);
32
+        Assert.assertEquals(expected,actual);
33
+
22 34
     }
23 35
 
24 36
     @Test
25 37
     public void testFindHoratio() {
38
+
39
+        boolean expected = true;
40
+       boolean actual = hp.findHoratio("This is horatio");
41
+
42
+        assertEquals(actual, expected);
43
+
26 44
     }
27 45
 
28 46
     @Test
29 47
     public void testFindHamlet() {
48
+
49
+        boolean expected = false;
50
+        boolean actual = hp.findHamlet("This is leon");
51
+
52
+        assertEquals(actual,expected);
53
+
30 54
     }
31 55
 }

+ 5383
- 0
target/classes/hamlet.txt
Diferenças do arquivo suprimidas por serem muito extensas
Ver arquivo