Description
Match a Simple Sentence or a partial sentence Suitable for matching
- People Names (to some extent)
- Product Titles (to some extent)
- Correct use of apostrophe (to some extent)
Input/Result
match("Because I'm Batman") true match("John Cena's fans") true match("Nice.") true match("Mat's Mug") true match("Good Boy") true match("Assert True.") true match("The students' projects") true match("The Johnsons' house is on fire.") true match("Tim's and Marty's ice cream") true match("You're late.") true match("Cat's eyes are blue.") true match("B.A.T.") true match("B. A. T.") true match(" Hello World ") true match(" K.O. ") true match(" Js' friend ") true match(" J A V A ") true match("I'll look into it") true match("I can't beleive it.") true match("The students'") false match("The students") false match(".") false match("") false match("AA") false match("A") false match("B..A.T.") false match("You''re late.") false match("' ; delete from users ;") false match("abcdefg@") false match("Bhathiya & JaDogg are the same") false match("Interesting !") false match("B..A.T.!@#$!@") false match("null") false match("The# students'") false match("The students are bad") false match("aaaa#") false match("abc$") false match("xyz%") false match("@@@@@@@") false match(".........") false match("I'm happy..") false match("Why?") false match("Gim'me") false match("weird4431") false match("1234123") false match(""hello"") false match("'hello'") false
Other
- Compiled with jdk1.8.0_05
- Source is in Java 1.6
- JUnit4 is used for Unit Tests
Code
This code can also be downloaded from GitHub
SimpleSentence.java: (34 lines, 971 bytes)
/**
* Match a Simple Sentence or a partial sentence<br />
* Suitable for matching
* <ul>
* <li>People Names (to some extent)</li>
* <li>Product Titles (to some extent)</li>
* <li>Correct use of apostrophe (to some extent)</li>
* </ul>
*
* @author Bhathiya
*/
public class SimpleSentence {
private static final String SIMPLE_SENTENCE
= "([a-zA-Z]+(\\.|\\. |'(s |re |t |m |ll )|s' | )?)+";
private static final Pattern SIMPLE_SENTENCE_PATTERN = Pattern.compile(
SIMPLE_SENTENCE);
public static boolean match(String toTest) {
if (toTest == null) {
return false;
}
String trimmed = toTest.trim();
return (trimmed.length() > 2
&& !trimmed.startsWith("'") && !trimmed.endsWith("'")
&& SIMPLE_SENTENCE_PATTERN.matcher(trimmed).matches());
}
}
Unit Test
SimpleSentenceTest.java: (74 lines, 2666 bytes)
/**
* Unit Test for SimpleSentence.java
*
* @author Bhathiya
*/
public class SimpleSentenceTest {
public static boolean match(String strToTest) {
boolean matches = SimpleSentence.match(strToTest);
System.out.printf("%-60s%s\n",
String.format("match(\"%s\")", strToTest),
matches ? "true" : "false");
return matches;
}
@Test
public void testMatching() {
assertTrue(match("Because I'm Batman"));
assertTrue(match("John Cena's fans"));
assertTrue(match("Nice."));
assertTrue(match("Mat's Mug"));
assertTrue(match("Good Boy"));
assertTrue(match("Assert True."));
assertTrue(match("The students' projects"));
assertTrue(match("The Johnsons' house is on fire."));
assertTrue(match("Tim's and Marty's ice cream"));
assertTrue(match("You're late."));
assertTrue(match("Cat's eyes are blue."));
assertTrue(match("B.A.T."));
assertTrue(match("B. A. T."));
assertTrue(match(" Hello World "));
assertTrue(match(" K.O. "));
assertTrue(match(" Js' friend "));
assertTrue(match(" J A V A "));
assertTrue(match("I'll look into it"));
assertTrue(match("I can't beleive it."));
}
@Test
public void testNotMatching() {
assertFalse(match("The students'"));
assertFalse(match("The students")); //extra space inside
assertFalse(match("."));
assertFalse(match(""));
assertFalse(match("AA"));
assertFalse(match("A"));
assertFalse(match("B..A.T."));
assertFalse(match("You''re late."));
assertFalse(match("' ; delete from users ;"));
assertFalse(match("abcdefg@"));
assertFalse(match("Bhathiya & JaDogg are the same"));
assertFalse(match("Interesting !"));
assertFalse(match("B..A.T.!@#$!@"));
assertFalse(match(null));
assertFalse(match("The# students'"));
assertFalse(match("The students are bad")); //extra spaces inside
assertFalse(match("aaaa#"));
assertFalse(match("abc$"));
assertFalse(match("xyz%"));
assertFalse(match("@@@@@@@"));
assertFalse(match("........."));
assertFalse(match("I'm happy.."));
assertFalse(match("Why?"));
assertFalse(match("Gim'me"));
assertFalse(match("weird4431"));
assertFalse(match("1234123"));
assertFalse(match("\"hello\""));
assertFalse(match("'hello'"));
}
}
Project Object Model
pom.xml: (23 lines, 917 bytes)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>info.simpll</groupId>
<artifactId>SimpleSentence</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>
Questions
- Is there a better way to write the regex or
match()
? - Does the given code follow best practices?
- Does the unit test code follow best practices?
assertTrue(match("Mat's Mug"));
- hi! \$\endgroup\$The students'
is a perfectly valid sentence. Otherwise,Mat's Mug
isn't a sentence either. Grammatically,students'
is the plural ofstudent's
, which means that something belongs to thestudents
. \$\endgroup\$