Initial Commit
This commit is contained in:
0
java/word-count/src/main/java/.keep
Normal file
0
java/word-count/src/main/java/.keep
Normal file
26
java/word-count/src/main/java/WordCount.java
Normal file
26
java/word-count/src/main/java/WordCount.java
Normal file
@@ -0,0 +1,26 @@
|
||||
import java.util.*;
|
||||
import java.lang.String;
|
||||
|
||||
public class WordCount {
|
||||
/**
|
||||
* Phrase takes a string and returns a map that counts
|
||||
* how many of each word are in the phrase
|
||||
* @param thePhrase The phrase to work with
|
||||
* @return The map with word counts
|
||||
*/
|
||||
public Map<String, Integer> Phrase(String thePhrase) {
|
||||
Map<String, Integer> ret = new HashMap<String, Integer>();
|
||||
thePhrase = thePhrase.replaceAll("[^a-zA-Z0-9 ]", "").replaceAll(" +"," ").toLowerCase();
|
||||
String[] words = thePhrase.split(" ");
|
||||
for(int i = 0; i < words.length; i++) {
|
||||
int wordCount = 0;
|
||||
if(ret.containsKey(words[i])) {
|
||||
wordCount = ret.get(words[i]);
|
||||
}
|
||||
if(words[i].trim() != "") {
|
||||
ret.put(words[i], wordCount+1);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
99
java/word-count/src/test/java/WordCountTest.java
Normal file
99
java/word-count/src/test/java/WordCountTest.java
Normal file
@@ -0,0 +1,99 @@
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.Integer;
|
||||
import java.lang.String;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class WordCountTest {
|
||||
|
||||
private final WordCount wordCount = new WordCount();
|
||||
|
||||
@Test
|
||||
public void countOneWord() {
|
||||
Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
|
||||
final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
|
||||
expectedWordCount.put("word", 1);
|
||||
|
||||
actualWordCount = wordCount.Phrase("word");
|
||||
assertEquals(
|
||||
expectedWordCount, actualWordCount
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void countOneOfEach() {
|
||||
Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
|
||||
final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
|
||||
expectedWordCount.put("one", 1);
|
||||
expectedWordCount.put("of", 1);
|
||||
expectedWordCount.put("each", 1);
|
||||
|
||||
actualWordCount = wordCount.Phrase("one of each");
|
||||
assertEquals(
|
||||
expectedWordCount, actualWordCount
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void countMultipleOccurences() {
|
||||
Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
|
||||
final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
|
||||
expectedWordCount.put("one", 1);
|
||||
expectedWordCount.put("fish", 4);
|
||||
expectedWordCount.put("two", 1);
|
||||
expectedWordCount.put("red", 1);
|
||||
expectedWordCount.put("blue", 1);
|
||||
|
||||
actualWordCount = wordCount.Phrase("one fish two fish red fish blue fish");
|
||||
assertEquals(
|
||||
expectedWordCount, actualWordCount
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignorePunctuation() {
|
||||
Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
|
||||
final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
|
||||
expectedWordCount.put("car", 1);
|
||||
expectedWordCount.put("carpet", 1);
|
||||
expectedWordCount.put("as", 1);
|
||||
expectedWordCount.put("java", 1);
|
||||
expectedWordCount.put("javascript", 1);
|
||||
|
||||
actualWordCount = wordCount.Phrase("car : carpet as java : javascript!!&@$%^&");
|
||||
assertEquals(
|
||||
expectedWordCount, actualWordCount
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeNumbers() {
|
||||
Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
|
||||
final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
|
||||
expectedWordCount.put("testing", 2);
|
||||
expectedWordCount.put("1", 1);
|
||||
expectedWordCount.put("2", 1);
|
||||
|
||||
actualWordCount = wordCount.Phrase("testing, 1, 2 testing");
|
||||
assertEquals(
|
||||
expectedWordCount, actualWordCount
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalizeCase() {
|
||||
Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
|
||||
final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
|
||||
expectedWordCount.put("go", 3);
|
||||
|
||||
actualWordCount = wordCount.Phrase("go Go GO");
|
||||
assertEquals(
|
||||
expectedWordCount, actualWordCount
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user