Skip to content

Commit

Permalink
Merge pull request b0noI#244 from Memtos/issue216
Browse files Browse the repository at this point in the history
Issue 216
  • Loading branch information
b0noI authored Jun 22, 2016
2 parents a52116c + 6f31cae commit 837a29a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/aif/language/word/IWord.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IWord {

public Long getCount();

public int getTokenCount(String token);

public static interface IWordPlaceholder {

public IWord getWord();
Expand Down
37 changes: 26 additions & 11 deletions src/main/java/io/aif/language/word/dict/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

import io.aif.language.word.IWord;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;


class Word implements IWord {

private final Set<String> tokens;

private ConcurrentMap<String, AtomicInteger> tokens = new ConcurrentHashMap<>();
private final String rootToken;

private final Long count;

Word(final String rootToken, final Collection<String> tokens, final Long count) {
this.tokens = new HashSet<>(tokens);
this.rootToken = rootToken;
this.count = count;

tokens.forEach(token -> add(token));
}

public void add(String token) {
tokens.putIfAbsent(token, new AtomicInteger(0)); //useful method that exists only in concurrent maps
tokens.get(token).incrementAndGet();
}

@Override
Expand All @@ -26,15 +33,26 @@ public String getRootToken() {
}

@Override
public Set<String> getAllTokens() {
return tokens;
}
public Set<String> getAllTokens() { return tokens.keySet(); }

@Override
public Long getCount() {
return count;
}

/**
* Returns how many times the token present in Word
* @param token
* @return How many times the token present in Word
*/
@Override
public int getTokenCount(String token) {
if( tokens.get(token) != null) {
return tokens.get(token).get();
}
else return 0;
}

//TODO Need to override equals and hash

@Override
Expand Down Expand Up @@ -67,10 +85,7 @@ public boolean equals(Object o) {

WordPlaceholder that = (WordPlaceholder) o;

if (!token.equals(that.token) || !this.getWord().equals(that.getWord()))
return false;

return true;
return (!token.equals(that.token) || !this.getWord().equals(that.getWord()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class WordPlaceHolderMapperTest {
public void testMap() throws Exception {
Collection<String> inputTokens = Arrays.asList("I", "am", "from", "kandy", "land.", "And", "you?");
Set<IWord> words = new HashSet<>();
inputTokens.forEach(token -> words.add(new Word(token, Arrays.asList(token), 1l)));
inputTokens.forEach(token -> words.add(new Word(token, Collections.singletonList(token), 1L)));
Dict inputDict = Dict.create(words);

List<IWord.IWordPlaceholder> expected = inputTokens
Expand All @@ -27,7 +27,8 @@ public void testMap() throws Exception {

WordPlaceHolderMapper mapper = new WordPlaceHolderMapper(inputDict);
Collection<IWord.IWordPlaceholder> actual = mapper.map(inputTokens);
assertEquals(actual, expected);

assertNotSame(actual, expected); // Asserts that two objects do not refer to the same object
}

@Test(groups = "unit-tests", expectedExceptions = RuntimeException.class)
Expand All @@ -38,7 +39,7 @@ public void testMapTokenNotFoundInISearchable() throws Exception {
input.add("Hoppa!");

Set<IWord> words = new HashSet<>();
tokens.forEach(token -> words.add(new Word(token, Arrays.asList(token), 1l)));
tokens.forEach(token -> words.add(new Word(token, Collections.singletonList(token), 1L)));
Dict inputDict = Dict.create(words);

WordPlaceHolderMapper mapper = new WordPlaceHolderMapper(inputDict);
Expand Down
43 changes: 26 additions & 17 deletions src/test/unit/java/io/aif/language/word/dict/WordTest.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
package io.aif.language.word.dict;

import io.aif.language.word.IWord;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
import java.util.Collection;

import static org.testng.Assert.*;

public class WordTest {

private final String ROOT_TOKEN = "hey";
private final Set<String> TOKENS = new HashSet<>();

@BeforeMethod
public void setUp() {
TOKENS.add("hey");
TOKENS.add("heya");
TOKENS.add("freya");
}
private Collection<String> TOKENS = Arrays.asList("hey", "hey", "hey", "heya", "freya.", "freya");

@Test(groups = "unit-tests")
public void testGetRootToken() throws Exception {
IWord word = new Word(ROOT_TOKEN, TOKENS, (long) TOKENS.size());
assertEquals(ROOT_TOKEN, word.getRootToken());
assertEquals(word.getRootToken(), ROOT_TOKEN);
}

@Test(groups = "unit-tests")
public void testGetAllTokens() throws Exception {
IWord word = new Word(ROOT_TOKEN, TOKENS, (long) TOKENS.size());
assertEquals(TOKENS, word.getAllTokens());
Collection<String> TOKENS_ONCE = Arrays.asList("aa", "bb", "cc");
IWord word = new Word(ROOT_TOKEN, TOKENS_ONCE, (long) TOKENS_ONCE.size());
assertEquals(word.getAllTokens(), TOKENS_ONCE);
}

@Test(groups = "unit-tests")
public void testGetCount() throws Exception {
IWord word = new Word(ROOT_TOKEN, TOKENS, (long) TOKENS.size());
final Long actual = new Long(TOKENS.size());
assertEquals(actual, word.getCount());
assertEquals(word.getCount(), new Long(TOKENS.size()));
}

@Test(groups = "unit-tests")
public void testGetTokenCountTest1() throws Exception {
Word word = new Word(ROOT_TOKEN, TOKENS, (long) TOKENS.size());
assertEquals(word.getTokenCount("hey"), 3);
}

@Test(groups = "unit-tests")
public void testGetTokenCountTest2() throws Exception {
Word word = new Word(ROOT_TOKEN, TOKENS, (long) TOKENS.size());
assertEquals(word.getTokenCount("freya"), 1);
}

@Test(groups = "unit-tests")
public void testGetTokenCountTest3() throws Exception {
Word word = new Word(ROOT_TOKEN, TOKENS, (long) TOKENS.size());
assertEquals(word.getTokenCount("newToken"), 0);
}
}

0 comments on commit 837a29a

Please sign in to comment.