Browse Source

Merge f87afeabb2b6006c6f81dc65248284577e961dfd into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

Mitch Taylor 6 years ago
parent
commit
f8cac895f7
No account linked to committer's email

+ 39
- 9
src/main/java/HamletParser.java View File

1
 import java.io.File;
1
 import java.io.File;
2
+import java.io.FileWriter;
2
 import java.io.IOException;
3
 import java.io.IOException;
3
 import java.util.Scanner;
4
 import java.util.Scanner;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
4
 
7
 
5
 /**
8
 /**
6
  * Created by thook on 10/7/15.
9
  * Created by thook on 10/7/15.
7
  */
10
  */
8
 public class HamletParser {
11
 public class HamletParser {
9
 
12
 
10
-    private String hamletData;
13
+    private static String hamletData;
11
 
14
 
12
-    public HamletParser(){
13
-        this.hamletData = loadFile();
15
+    HamletParser(){
16
+        hamletData = loadFile();
14
     }
17
     }
15
 
18
 
16
-    private String loadFile(){
19
+    public String loadFile(){
17
         ClassLoader classLoader = getClass().getClassLoader();
20
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
21
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
         StringBuilder result = new StringBuilder("");
22
         StringBuilder result = new StringBuilder("");
20
-
21
         try(Scanner scanner = new Scanner(file)){
23
         try(Scanner scanner = new Scanner(file)){
22
             while(scanner.hasNextLine()){
24
             while(scanner.hasNextLine()){
23
                 String line = scanner.nextLine();
25
                 String line = scanner.nextLine();
24
                 result.append(line).append("\n");
26
                 result.append(line).append("\n");
25
             }
27
             }
26
-
27
             scanner.close();
28
             scanner.close();
28
         }catch(IOException e){
29
         }catch(IOException e){
29
             e.printStackTrace();
30
             e.printStackTrace();
30
         }
31
         }
31
-
32
         return result.toString();
32
         return result.toString();
33
     }
33
     }
34
 
34
 
35
-    public String getHamletData(){
36
-        return hamletData;
35
+    public static String changeHamletToLeon(String string) {
36
+        StringBuffer sb = new StringBuffer();
37
+        String hamletPattern = "([Hh][Aa][Mm][Ll][Ee][Tt])";
38
+        Pattern pattern = Pattern.compile(hamletPattern);
39
+        Matcher matcher = pattern.matcher(string);
40
+        while (matcher.find()) {
41
+            matcher.appendReplacement(sb, "Leon");
42
+        }
43
+        return matcher.appendTail(sb).toString();
44
+    }
45
+
46
+    public static String changeHoratioToTariq(String string) {
47
+        StringBuffer sb = new StringBuffer();
48
+        String horatioPattern = "([Hh][Oo][Rr][Aa][Tt][Ii][Oo])";
49
+        Pattern pattern = Pattern.compile(horatioPattern);
50
+        Matcher matcher = pattern.matcher(string);
51
+        while (matcher.find()) {
52
+            matcher.appendReplacement(sb, "Tariq");
53
+        }
54
+        return matcher.appendTail(sb).toString();
55
+    }
56
+
57
+    private void changeEverythingAndPutInFile() throws IOException {
58
+        String fixedFile = changeHamletToLeon(changeHoratioToTariq(hamletData));
59
+        FileWriter fileWriter = new FileWriter("src/main/resources/zipcodeHamlet.txt", true);
60
+        fileWriter.write(fixedFile);
61
+        fileWriter.close();
62
+    }
63
+
64
+    public static void main(String[] args) throws IOException {
65
+        HamletParser h = new HamletParser();
66
+        h.changeEverythingAndPutInFile();
37
     }
67
     }
38
 
68
 
39
 }
69
 }

+ 5383
- 0
src/main/resources/zipcodeHamlet.txt
File diff suppressed because it is too large
View File


+ 14
- 7
src/test/java/HamletParserTest.java View File

1
+import org.junit.Assert;
1
 import org.junit.Before;
2
 import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
10
     @Before
11
     @Before
11
     public void setUp() {
12
     public void setUp() {
12
         this.hamletParser = new HamletParser();
13
         this.hamletParser = new HamletParser();
13
-        this.hamletText = hamletParser.getHamletData();
14
     }
14
     }
15
 
15
 
16
     @Test
16
     @Test
17
     public void testChangeHamletToLeon() {
17
     public void testChangeHamletToLeon() {
18
+        String startingString = "Here are some words Hamlet and here are some words Horatio.\nHere are more.";
19
+        String expected = "Here are some words Leon and here are some words Horatio.\nHere are more.";
20
+        String actual = hamletParser.changeHamletToLeon(startingString);
21
+        Assert.assertEquals(expected, actual);
18
     }
22
     }
19
 
23
 
20
     @Test
24
     @Test
21
     public void testChangeHoratioToTariq() {
25
     public void testChangeHoratioToTariq() {
26
+        String startingString = "Here are some words Hamlet and here are some words Horatio.";
27
+        String expected = "Here are some words Hamlet and here are some words Tariq.";
28
+        String actual = hamletParser.changeHoratioToTariq(startingString);
29
+        Assert.assertEquals(expected, actual);
22
     }
30
     }
23
 
31
 
24
-    @Test
25
-    public void testFindHoratio() {
26
-    }
32
+//    @Test
33
+//    public void testLoadFile() {
34
+//        String expected = ;
35
+//        String actual = hamletParser.loadFile();
36
+//    }
27
 
37
 
28
-    @Test
29
-    public void testFindHamlet() {
30
-    }
31
 }
38
 }

+ 5383
- 0
target/classes/hamlet.txt
File diff suppressed because it is too large
View File


+ 0
- 0
target/classes/zipcodeHamlet.txt View File