Skip to content

Finished Leomlet #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>regex</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by thook on 10/7/15.
*/
public class HamletParser {

private String hamletData;
private int counter = 0;

public HamletParser(){
this.hamletData = loadFile();
Expand All @@ -33,7 +36,65 @@ private String loadFile(){
}

public String getHamletData(){

return hamletData;
}

public boolean findHoratio(){
return findMatch("[Hh][Oo][Rr][Aa][Tt][Ii][Oo]", hamletData);
}


public boolean findHamlet(){
return findMatch("[Hh][Aa][Mm][Ll][Ee][Tt]", hamletData);
}

public String changeHamletToLeon(String stringBook){

Pattern patternReplace = Pattern.compile("[Hh][a][m][l][e][t]");
Matcher matcher = patternReplace.matcher(stringBook);
String finalReplace = matcher.replaceAll("Leon");

Pattern patternReplaceCaps = Pattern.compile("HAMLET");
Matcher matcherCaps = patternReplaceCaps.matcher(finalReplace);
String finalReplaceCaps = matcherCaps.replaceAll("LEON");

return finalReplaceCaps;

}

public String changeHoratioToTariq(String stringBook){

Pattern patternReplace = Pattern.compile("[Hh][o][r][a][t][i][o]");
Matcher matcher = patternReplace.matcher(stringBook);
String finalReplace = matcher.replaceAll("Tariq");

Pattern patternReplaceCaps = Pattern.compile("HORATIO");
Matcher matcherCaps = patternReplaceCaps.matcher(finalReplace);
String finalReplaceCaps = matcherCaps.replaceAll("TARIQ");

return finalReplaceCaps;
}

public int getCounterTimeSeen(){
return counter;
}


public boolean findMatch(String regexSearch, String hamletData){

Pattern checkRegex = Pattern.compile(regexSearch);

Matcher regexMatcher = checkRegex.matcher(hamletData);

boolean findMatch = false;

while(regexMatcher.find()){
findMatch = true;
counter++;
}

return findMatch;
}

}
1 change: 1 addition & 0 deletions src/main/resources/Leon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hamlet is a great person. Hamlet likes to eat cheese.
1 change: 1 addition & 0 deletions src/main/resources/Wilhem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Horatio is a great person. Horatio likes to eat cheese.
33 changes: 33 additions & 0 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -6,26 +7,58 @@
public class HamletParserTest {
private String hamletText;
private HamletParser hamletParser;
private String leonText;
private String tariqText;

@Before
public void setUp() {

this.hamletParser = new HamletParser();
this.hamletText = hamletParser.getHamletData();
this.leonText = hamletParser.getHamletData();
this.tariqText = hamletParser.getHamletData();

}

@Test
public void testChangeHamletToLeon() {
String stringTest = "Hamlet is a cool person because HAMLET likes to eat cheese.";

String expected = "Leon is a cool person because LEON likes to eat cheese.";
String actual = hamletParser.changeHamletToLeon(stringTest);

Assert.assertEquals(expected, actual);
}

@Test
public void testChangeHoratioToTariq() {
String stringTest = "Horatio is a cool person because HORATIO likes to eat cheese.";

String expected = "Tariq is a cool person because TARIQ likes to eat cheese.";
String actual = hamletParser.changeHoratioToTariq(stringTest);

Assert.assertEquals(expected, actual);
}

@Test
public void testFindHoratio() {

Assert.assertTrue(hamletParser.findHoratio());
}

@Test
public void testFindHamlet() {

Assert.assertTrue(hamletParser.findHamlet());
}

@Test
public void getCounterTest(){
hamletParser.findMatch("[Hh][Aa][Mm][Ll][Ee][Tt]", hamletText);

int expected = 472;
int actual = hamletParser.getCounterTimeSeen();

Assert.assertEquals(expected, actual);
}
}
1 change: 1 addition & 0 deletions target/classes/Leon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hamlet is a great person. Hamlet likes to eat cheese.
1 change: 1 addition & 0 deletions target/classes/Wilhem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Horatio is a great person. Horatio likes to eat cheese.
Loading