Quit using obsolete methods for testing.

Bug: 11073222
Change-Id: I77c99fbea2f255dbfddb646b6e12bee574647eca
This commit is contained in:
Keisuke Kuroyanagi 2013-12-04 16:03:53 +09:00
parent 7081a36e2f
commit e33072eb50
3 changed files with 121 additions and 116 deletions

View file

@ -57,7 +57,7 @@ public final class BinaryDictionary extends Dictionary {
@UsedForTesting @UsedForTesting
public static final String MAX_BIGRAM_COUNT_QUERY = "MAX_BIGRAM_COUNT"; public static final String MAX_BIGRAM_COUNT_QUERY = "MAX_BIGRAM_COUNT";
public static final int NOT_A_VALID_TIME_STAMP = -1; public static final int NOT_A_VALID_TIMESTAMP = -1;
private long mNativeDict; private long mNativeDict;
private final Locale mLocale; private final Locale mLocale;
@ -285,18 +285,6 @@ public final class BinaryDictionary extends Dictionary {
return getBigramProbabilityNative(mNativeDict, codePoints0, codePoints1); return getBigramProbabilityNative(mNativeDict, codePoints0, codePoints1);
} }
// Add a unigram entry to binary dictionary in native code.
public void addUnigramWord(final String word, final int probability) {
if (TextUtils.isEmpty(word)) {
return;
}
final int[] codePoints = StringUtils.toCodePointArray(word);
final int[] shortcutTargetCodePoints = new int[0];
addUnigramWordNative(mNativeDict, codePoints, probability, shortcutTargetCodePoints,
NOT_A_PROBABILITY, false /* isNotAWord */, false /* isBlacklisted */,
NOT_A_VALID_TIME_STAMP);
}
// Add a unigram entry to binary dictionary with unigram attributes in native code. // Add a unigram entry to binary dictionary with unigram attributes in native code.
public void addUnigramWord(final String word, final int probability, public void addUnigramWord(final String word, final int probability,
final String shortcutTarget, final int shortcutProbability, final boolean isNotAWord, final String shortcutTarget, final int shortcutProbability, final boolean isNotAWord,
@ -311,17 +299,6 @@ public final class BinaryDictionary extends Dictionary {
shortcutProbability, isNotAWord, isBlacklisted, timestamp); shortcutProbability, isNotAWord, isBlacklisted, timestamp);
} }
// Add a bigram entry to binary dictionary in native code.
public void addBigramWords(final String word0, final String word1, final int probability) {
if (TextUtils.isEmpty(word0) || TextUtils.isEmpty(word1)) {
return;
}
final int[] codePoints0 = StringUtils.toCodePointArray(word0);
final int[] codePoints1 = StringUtils.toCodePointArray(word1);
addBigramWordsNative(mNativeDict, codePoints0, codePoints1, probability,
NOT_A_VALID_TIME_STAMP);
}
// Add a bigram entry to binary dictionary with timestamp in native code. // Add a bigram entry to binary dictionary with timestamp in native code.
public void addBigramWords(final String word0, final String word1, final int probability, public void addBigramWords(final String word0, final String word1, final int probability,
final int timestamp) { final int timestamp) {

View file

@ -53,6 +53,20 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
super.tearDown(); super.tearDown();
} }
private void addUnigramWord(final BinaryDictionary binaryDictionary, final String word,
final int probability) {
binaryDictionary.addUnigramWord(word, probability, "" /* shortcutTarget */,
BinaryDictionary.NOT_A_PROBABILITY /* shortcutProbability */,
false /* isNotAWord */, false /* isBlacklisted */,
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
}
private void addBigramWords(final BinaryDictionary binaryDictionary, final String word0,
final String word1, final int probability) {
binaryDictionary.addBigramWords(word0, word1, probability,
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
}
private void forcePassingShortTime(final BinaryDictionary binaryDictionary) { private void forcePassingShortTime(final BinaryDictionary binaryDictionary) {
// Entries having low probability would be suppressed once in 3 GCs. // Entries having low probability would be suppressed once in 3 GCs.
final int count = 3; final int count = 3;
@ -115,35 +129,35 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
0 /* offset */, dictFile.length(), true /* useFullEditDistance */, 0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
binaryDictionary.addUnigramWord("a", Dictionary.NOT_A_PROBABILITY); addUnigramWord(binaryDictionary, "a", Dictionary.NOT_A_PROBABILITY);
assertFalse(binaryDictionary.isValidWord("a")); assertFalse(binaryDictionary.isValidWord("a"));
binaryDictionary.addUnigramWord("a", Dictionary.NOT_A_PROBABILITY); addUnigramWord(binaryDictionary, "a", Dictionary.NOT_A_PROBABILITY);
assertFalse(binaryDictionary.isValidWord("a")); assertFalse(binaryDictionary.isValidWord("a"));
binaryDictionary.addUnigramWord("a", Dictionary.NOT_A_PROBABILITY); addUnigramWord(binaryDictionary, "a", Dictionary.NOT_A_PROBABILITY);
assertFalse(binaryDictionary.isValidWord("a")); assertFalse(binaryDictionary.isValidWord("a"));
binaryDictionary.addUnigramWord("a", Dictionary.NOT_A_PROBABILITY); addUnigramWord(binaryDictionary, "a", Dictionary.NOT_A_PROBABILITY);
assertTrue(binaryDictionary.isValidWord("a")); assertTrue(binaryDictionary.isValidWord("a"));
binaryDictionary.addUnigramWord("b", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "b", DUMMY_PROBABILITY);
assertTrue(binaryDictionary.isValidWord("b")); assertTrue(binaryDictionary.isValidWord("b"));
binaryDictionary.addBigramWords("a", "b", Dictionary.NOT_A_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", Dictionary.NOT_A_PROBABILITY);
assertFalse(binaryDictionary.isValidBigram("a", "b")); assertFalse(binaryDictionary.isValidBigram("a", "b"));
binaryDictionary.addBigramWords("a", "b", Dictionary.NOT_A_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", Dictionary.NOT_A_PROBABILITY);
assertFalse(binaryDictionary.isValidBigram("a", "b")); assertFalse(binaryDictionary.isValidBigram("a", "b"));
binaryDictionary.addBigramWords("a", "b", Dictionary.NOT_A_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", Dictionary.NOT_A_PROBABILITY);
assertFalse(binaryDictionary.isValidBigram("a", "b")); assertFalse(binaryDictionary.isValidBigram("a", "b"));
binaryDictionary.addBigramWords("a", "b", Dictionary.NOT_A_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", Dictionary.NOT_A_PROBABILITY);
assertTrue(binaryDictionary.isValidBigram("a", "b")); assertTrue(binaryDictionary.isValidBigram("a", "b"));
binaryDictionary.addUnigramWord("c", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "c", DUMMY_PROBABILITY);
binaryDictionary.addBigramWords("a", "c", DUMMY_PROBABILITY); addBigramWords(binaryDictionary, "a", "c", DUMMY_PROBABILITY);
assertTrue(binaryDictionary.isValidBigram("a", "c")); assertTrue(binaryDictionary.isValidBigram("a", "c"));
// Add bigrams of not valid unigrams. // Add bigrams of not valid unigrams.
binaryDictionary.addBigramWords("x", "y", Dictionary.NOT_A_PROBABILITY); addBigramWords(binaryDictionary, "x", "y", Dictionary.NOT_A_PROBABILITY);
assertFalse(binaryDictionary.isValidBigram("x", "y")); assertFalse(binaryDictionary.isValidBigram("x", "y"));
binaryDictionary.addBigramWords("x", "y", DUMMY_PROBABILITY); addBigramWords(binaryDictionary, "x", "y", DUMMY_PROBABILITY);
assertFalse(binaryDictionary.isValidBigram("x", "y")); assertFalse(binaryDictionary.isValidBigram("x", "y"));
binaryDictionary.close(); binaryDictionary.close();
@ -165,39 +179,39 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
0 /* offset */, dictFile.length(), true /* useFullEditDistance */, 0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
assertTrue(binaryDictionary.isValidWord("a")); assertTrue(binaryDictionary.isValidWord("a"));
forcePassingShortTime(binaryDictionary); forcePassingShortTime(binaryDictionary);
assertFalse(binaryDictionary.isValidWord("a")); assertFalse(binaryDictionary.isValidWord("a"));
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
forcePassingShortTime(binaryDictionary); forcePassingShortTime(binaryDictionary);
assertTrue(binaryDictionary.isValidWord("a")); assertTrue(binaryDictionary.isValidWord("a"));
forcePassingLongTime(binaryDictionary); forcePassingLongTime(binaryDictionary);
assertFalse(binaryDictionary.isValidWord("a")); assertFalse(binaryDictionary.isValidWord("a"));
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("b", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "b", DUMMY_PROBABILITY);
binaryDictionary.addBigramWords("a", "b", DUMMY_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", DUMMY_PROBABILITY);
assertTrue(binaryDictionary.isValidBigram("a", "b")); assertTrue(binaryDictionary.isValidBigram("a", "b"));
forcePassingShortTime(binaryDictionary); forcePassingShortTime(binaryDictionary);
assertFalse(binaryDictionary.isValidBigram("a", "b")); assertFalse(binaryDictionary.isValidBigram("a", "b"));
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("b", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "b", DUMMY_PROBABILITY);
binaryDictionary.addBigramWords("a", "b", DUMMY_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("b", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "b", DUMMY_PROBABILITY);
binaryDictionary.addBigramWords("a", "b", DUMMY_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("b", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "b", DUMMY_PROBABILITY);
binaryDictionary.addBigramWords("a", "b", DUMMY_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("a", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "a", DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord("b", DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, "b", DUMMY_PROBABILITY);
binaryDictionary.addBigramWords("a", "b", DUMMY_PROBABILITY); addBigramWords(binaryDictionary, "a", "b", DUMMY_PROBABILITY);
assertTrue(binaryDictionary.isValidBigram("a", "b")); assertTrue(binaryDictionary.isValidBigram("a", "b"));
forcePassingShortTime(binaryDictionary); forcePassingShortTime(binaryDictionary);
assertTrue(binaryDictionary.isValidBigram("a", "b")); assertTrue(binaryDictionary.isValidBigram("a", "b"));
@ -241,7 +255,7 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
binaryDictionary.getPropertyForTests(BinaryDictionary.MAX_UNIGRAM_COUNT_QUERY)); binaryDictionary.getPropertyForTests(BinaryDictionary.MAX_UNIGRAM_COUNT_QUERY));
for (int i = 0; i < unigramTypedCount; i++) { for (int i = 0; i < unigramTypedCount; i++) {
final String word = words.get(random.nextInt(words.size())); final String word = words.get(random.nextInt(words.size()));
binaryDictionary.addUnigramWord(word, DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, word, DUMMY_PROBABILITY);
if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) { if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) {
final int unigramCountBeforeGC = final int unigramCountBeforeGC =
@ -310,9 +324,9 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
binaryDictionary.getPropertyForTests(BinaryDictionary.MAX_BIGRAM_COUNT_QUERY)); binaryDictionary.getPropertyForTests(BinaryDictionary.MAX_BIGRAM_COUNT_QUERY));
for (int i = 0; i < bigramTypedCount; ++i) { for (int i = 0; i < bigramTypedCount; ++i) {
final Pair<String, String> bigram = bigrams.get(random.nextInt(bigrams.size())); final Pair<String, String> bigram = bigrams.get(random.nextInt(bigrams.size()));
binaryDictionary.addUnigramWord(bigram.first, DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, bigram.first, DUMMY_PROBABILITY);
binaryDictionary.addUnigramWord(bigram.second, DUMMY_PROBABILITY); addUnigramWord(binaryDictionary, bigram.second, DUMMY_PROBABILITY);
binaryDictionary.addBigramWords(bigram.first, bigram.second, DUMMY_PROBABILITY); addBigramWords(binaryDictionary, bigram.first, bigram.second, DUMMY_PROBABILITY);
if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) { if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) {
final int bigramCountBeforeGC = final int bigramCountBeforeGC =

View file

@ -105,6 +105,20 @@ public class BinaryDictionaryTests extends AndroidTestCase {
binaryDictionary.close(); binaryDictionary.close();
} }
private void addUnigramWord(final BinaryDictionary binaryDictionary, final String word,
final int probability) {
binaryDictionary.addUnigramWord(word, probability, "" /* shortcutTarget */,
BinaryDictionary.NOT_A_PROBABILITY /* shortcutProbability */,
false /* isNotAWord */, false /* isBlacklisted */,
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
}
private void addBigramWords(final BinaryDictionary binaryDictionary, final String word0,
final String word1, final int probability) {
binaryDictionary.addBigramWords(word0, word1, probability,
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
}
public void testAddUnigramWord() { public void testAddUnigramWord() {
testAddUnigramWord(4 /* formatVersion */); testAddUnigramWord(4 /* formatVersion */);
} }
@ -121,21 +135,21 @@ public class BinaryDictionaryTests extends AndroidTestCase {
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
final int probability = 100; final int probability = 100;
binaryDictionary.addUnigramWord("aaa", probability); addUnigramWord(binaryDictionary, "aaa", probability);
// Reallocate and create. // Reallocate and create.
binaryDictionary.addUnigramWord("aab", probability); addUnigramWord(binaryDictionary, "aab", probability);
// Insert into children. // Insert into children.
binaryDictionary.addUnigramWord("aac", probability); addUnigramWord(binaryDictionary, "aac", probability);
// Make terminal. // Make terminal.
binaryDictionary.addUnigramWord("aa", probability); addUnigramWord(binaryDictionary, "aa", probability);
// Create children. // Create children.
binaryDictionary.addUnigramWord("aaaa", probability); addUnigramWord(binaryDictionary, "aaaa", probability);
// Reallocate and make termianl. // Reallocate and make termianl.
binaryDictionary.addUnigramWord("a", probability); addUnigramWord(binaryDictionary, "a", probability);
final int updatedProbability = 200; final int updatedProbability = 200;
// Update. // Update.
binaryDictionary.addUnigramWord("aaa", updatedProbability); addUnigramWord(binaryDictionary, "aaa", updatedProbability);
assertEquals(probability, binaryDictionary.getFrequency("aab")); assertEquals(probability, binaryDictionary.getFrequency("aab"));
assertEquals(probability, binaryDictionary.getFrequency("aac")); assertEquals(probability, binaryDictionary.getFrequency("aac"));
@ -175,7 +189,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
probabilityMap.put(word, random.nextInt(0xFF)); probabilityMap.put(word, random.nextInt(0xFF));
} }
for (String word : probabilityMap.keySet()) { for (String word : probabilityMap.keySet()) {
binaryDictionary.addUnigramWord(word, probabilityMap.get(word)); addUnigramWord(binaryDictionary, word, probabilityMap.get(word));
} }
for (String word : probabilityMap.keySet()) { for (String word : probabilityMap.keySet()) {
assertEquals(word, (int)probabilityMap.get(word), binaryDictionary.getFrequency(word)); assertEquals(word, (int)probabilityMap.get(word), binaryDictionary.getFrequency(word));
@ -201,13 +215,13 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final int unigramProbability = 100; final int unigramProbability = 100;
final int bigramProbability = 10; final int bigramProbability = 10;
final int updatedBigramProbability = 15; final int updatedBigramProbability = 15;
binaryDictionary.addUnigramWord("aaa", unigramProbability); addUnigramWord(binaryDictionary, "aaa", unigramProbability);
binaryDictionary.addUnigramWord("abb", unigramProbability); addUnigramWord(binaryDictionary, "abb", unigramProbability);
binaryDictionary.addUnigramWord("bcc", unigramProbability); addUnigramWord(binaryDictionary, "bcc", unigramProbability);
binaryDictionary.addBigramWords("aaa", "abb", bigramProbability); addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
binaryDictionary.addBigramWords("aaa", "bcc", bigramProbability); addBigramWords(binaryDictionary, "aaa", "bcc", bigramProbability);
binaryDictionary.addBigramWords("abb", "aaa", bigramProbability); addBigramWords(binaryDictionary, "abb", "aaa", bigramProbability);
binaryDictionary.addBigramWords("abb", "bcc", bigramProbability); addBigramWords(binaryDictionary, "abb", "bcc", bigramProbability);
final int probability = binaryDictionary.calculateProbability(unigramProbability, final int probability = binaryDictionary.calculateProbability(unigramProbability,
bigramProbability); bigramProbability);
@ -220,7 +234,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
assertEquals(probability, binaryDictionary.getBigramProbability("abb", "aaa")); assertEquals(probability, binaryDictionary.getBigramProbability("abb", "aaa"));
assertEquals(probability, binaryDictionary.getBigramProbability("abb", "bcc")); assertEquals(probability, binaryDictionary.getBigramProbability("abb", "bcc"));
binaryDictionary.addBigramWords("aaa", "abb", updatedBigramProbability); addBigramWords(binaryDictionary, "aaa", "abb", updatedBigramProbability);
final int updatedProbability = binaryDictionary.calculateProbability(unigramProbability, final int updatedProbability = binaryDictionary.calculateProbability(unigramProbability,
updatedBigramProbability); updatedBigramProbability);
assertEquals(updatedProbability, binaryDictionary.getBigramProbability("aaa", "abb")); assertEquals(updatedProbability, binaryDictionary.getBigramProbability("aaa", "abb"));
@ -236,16 +250,16 @@ public class BinaryDictionaryTests extends AndroidTestCase {
binaryDictionary.getBigramProbability("aaa", "aaa")); binaryDictionary.getBigramProbability("aaa", "aaa"));
// Testing bigram link. // Testing bigram link.
binaryDictionary.addUnigramWord("abcde", unigramProbability); addUnigramWord(binaryDictionary, "abcde", unigramProbability);
binaryDictionary.addUnigramWord("fghij", unigramProbability); addUnigramWord(binaryDictionary, "fghij", unigramProbability);
binaryDictionary.addBigramWords("abcde", "fghij", bigramProbability); addBigramWords(binaryDictionary, "abcde", "fghij", bigramProbability);
binaryDictionary.addUnigramWord("fgh", unigramProbability); addUnigramWord(binaryDictionary, "fgh", unigramProbability);
binaryDictionary.addUnigramWord("abc", unigramProbability); addUnigramWord(binaryDictionary, "abc", unigramProbability);
binaryDictionary.addUnigramWord("f", unigramProbability); addUnigramWord(binaryDictionary, "f", unigramProbability);
assertEquals(probability, binaryDictionary.getBigramProbability("abcde", "fghij")); assertEquals(probability, binaryDictionary.getBigramProbability("abcde", "fghij"));
assertEquals(Dictionary.NOT_A_PROBABILITY, assertEquals(Dictionary.NOT_A_PROBABILITY,
binaryDictionary.getBigramProbability("abcde", "fgh")); binaryDictionary.getBigramProbability("abcde", "fgh"));
binaryDictionary.addBigramWords("abcde", "fghij", updatedBigramProbability); addBigramWords(binaryDictionary, "abcde", "fghij", updatedBigramProbability);
assertEquals(updatedProbability, binaryDictionary.getBigramProbability("abcde", "fghij")); assertEquals(updatedProbability, binaryDictionary.getBigramProbability("abcde", "fghij"));
dictFile.delete(); dictFile.delete();
@ -284,7 +298,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
unigramProbabilities.put(word, unigramProbability); unigramProbabilities.put(word, unigramProbability);
binaryDictionary.addUnigramWord(word, unigramProbability); addUnigramWord(binaryDictionary, word, unigramProbability);
} }
for (int i = 0; i < bigramCount; i++) { for (int i = 0; i < bigramCount; i++) {
@ -297,7 +311,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
bigramWords.add(bigram); bigramWords.add(bigram);
final int bigramProbability = random.nextInt(0xF); final int bigramProbability = random.nextInt(0xF);
bigramProbabilities.put(bigram, bigramProbability); bigramProbabilities.put(bigram, bigramProbability);
binaryDictionary.addBigramWords(word0, word1, bigramProbability); addBigramWords(binaryDictionary, word0, word1, bigramProbability);
} }
for (final Pair<String, String> bigram : bigramWords) { for (final Pair<String, String> bigram : bigramWords) {
@ -328,13 +342,13 @@ public class BinaryDictionaryTests extends AndroidTestCase {
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
final int unigramProbability = 100; final int unigramProbability = 100;
final int bigramProbability = 10; final int bigramProbability = 10;
binaryDictionary.addUnigramWord("aaa", unigramProbability); addUnigramWord(binaryDictionary, "aaa", unigramProbability);
binaryDictionary.addUnigramWord("abb", unigramProbability); addUnigramWord(binaryDictionary, "abb", unigramProbability);
binaryDictionary.addUnigramWord("bcc", unigramProbability); addUnigramWord(binaryDictionary, "bcc", unigramProbability);
binaryDictionary.addBigramWords("aaa", "abb", bigramProbability); addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
binaryDictionary.addBigramWords("aaa", "bcc", bigramProbability); addBigramWords(binaryDictionary, "aaa", "bcc", bigramProbability);
binaryDictionary.addBigramWords("abb", "aaa", bigramProbability); addBigramWords(binaryDictionary, "abb", "aaa", bigramProbability);
binaryDictionary.addBigramWords("abb", "bcc", bigramProbability); addBigramWords(binaryDictionary, "abb", "bcc", bigramProbability);
assertEquals(true, binaryDictionary.isValidBigram("aaa", "abb")); assertEquals(true, binaryDictionary.isValidBigram("aaa", "abb"));
assertEquals(true, binaryDictionary.isValidBigram("aaa", "bcc")); assertEquals(true, binaryDictionary.isValidBigram("aaa", "bcc"));
@ -343,7 +357,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
binaryDictionary.removeBigramWords("aaa", "abb"); binaryDictionary.removeBigramWords("aaa", "abb");
assertEquals(false, binaryDictionary.isValidBigram("aaa", "abb")); assertEquals(false, binaryDictionary.isValidBigram("aaa", "abb"));
binaryDictionary.addBigramWords("aaa", "abb", bigramProbability); addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
assertEquals(true, binaryDictionary.isValidBigram("aaa", "abb")); assertEquals(true, binaryDictionary.isValidBigram("aaa", "abb"));
@ -378,8 +392,8 @@ public class BinaryDictionaryTests extends AndroidTestCase {
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
final int probability = 100; final int probability = 100;
binaryDictionary.addUnigramWord("aaa", probability); addUnigramWord(binaryDictionary, "aaa", probability);
binaryDictionary.addUnigramWord("abcd", probability); addUnigramWord(binaryDictionary, "abcd", probability);
// Close without flushing. // Close without flushing.
binaryDictionary.close(); binaryDictionary.close();
@ -390,8 +404,8 @@ public class BinaryDictionaryTests extends AndroidTestCase {
assertEquals(Dictionary.NOT_A_PROBABILITY, binaryDictionary.getFrequency("aaa")); assertEquals(Dictionary.NOT_A_PROBABILITY, binaryDictionary.getFrequency("aaa"));
assertEquals(Dictionary.NOT_A_PROBABILITY, binaryDictionary.getFrequency("abcd")); assertEquals(Dictionary.NOT_A_PROBABILITY, binaryDictionary.getFrequency("abcd"));
binaryDictionary.addUnigramWord("aaa", probability); addUnigramWord(binaryDictionary, "aaa", probability);
binaryDictionary.addUnigramWord("abcd", probability); addUnigramWord(binaryDictionary, "abcd", probability);
binaryDictionary.flush(); binaryDictionary.flush();
binaryDictionary.close(); binaryDictionary.close();
@ -401,7 +415,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
assertEquals(probability, binaryDictionary.getFrequency("aaa")); assertEquals(probability, binaryDictionary.getFrequency("aaa"));
assertEquals(probability, binaryDictionary.getFrequency("abcd")); assertEquals(probability, binaryDictionary.getFrequency("abcd"));
binaryDictionary.addUnigramWord("bcde", probability); addUnigramWord(binaryDictionary, "bcde", probability);
binaryDictionary.flush(); binaryDictionary.flush();
binaryDictionary.close(); binaryDictionary.close();
@ -431,13 +445,13 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final int unigramProbability = 100; final int unigramProbability = 100;
final int bigramProbability = 10; final int bigramProbability = 10;
binaryDictionary.addUnigramWord("aaa", unigramProbability); addUnigramWord(binaryDictionary, "aaa", unigramProbability);
binaryDictionary.addUnigramWord("abb", unigramProbability); addUnigramWord(binaryDictionary, "abb", unigramProbability);
binaryDictionary.addUnigramWord("bcc", unigramProbability); addUnigramWord(binaryDictionary, "bcc", unigramProbability);
binaryDictionary.addBigramWords("aaa", "abb", bigramProbability); addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
binaryDictionary.addBigramWords("aaa", "bcc", bigramProbability); addBigramWords(binaryDictionary, "aaa", "bcc", bigramProbability);
binaryDictionary.addBigramWords("abb", "aaa", bigramProbability); addBigramWords(binaryDictionary, "abb", "aaa", bigramProbability);
binaryDictionary.addBigramWords("abb", "bcc", bigramProbability); addBigramWords(binaryDictionary, "abb", "bcc", bigramProbability);
binaryDictionary.flushWithGC(); binaryDictionary.flushWithGC();
binaryDictionary.close(); binaryDictionary.close();
@ -497,7 +511,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
unigramProbabilities.put(word, unigramProbability); unigramProbabilities.put(word, unigramProbability);
binaryDictionary.addUnigramWord(word, unigramProbability); addUnigramWord(binaryDictionary, word, unigramProbability);
} }
for (int i = 0; i < bigramCount; i++) { for (int i = 0; i < bigramCount; i++) {
@ -510,7 +524,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
bigramWords.add(bigram); bigramWords.add(bigram);
final int bigramProbability = random.nextInt(0xF); final int bigramProbability = random.nextInt(0xF);
bigramProbabilities.put(bigram, bigramProbability); bigramProbabilities.put(bigram, bigramProbability);
binaryDictionary.addBigramWords(word0, word1, bigramProbability); addBigramWords(binaryDictionary, word0, word1, bigramProbability);
} }
binaryDictionary.flushWithGC(); binaryDictionary.flushWithGC();
@ -568,7 +582,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
unigramProbabilities.put(word, unigramProbability); unigramProbabilities.put(word, unigramProbability);
binaryDictionary.addUnigramWord(word, unigramProbability); addUnigramWord(binaryDictionary, word, unigramProbability);
} }
binaryDictionary.flushWithGC(); binaryDictionary.flushWithGC();
binaryDictionary.close(); binaryDictionary.close();
@ -584,7 +598,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
unigramProbabilities.put(word, unigramProbability); unigramProbabilities.put(word, unigramProbability);
binaryDictionary.addUnigramWord(word, unigramProbability); addUnigramWord(binaryDictionary, word, unigramProbability);
} }
// Add bigram. // Add bigram.
if (random.nextFloat() < addBigramProb && words.size() > 2) { if (random.nextFloat() < addBigramProb && words.size() > 2) {
@ -602,7 +616,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final Pair<String, String> bigram = new Pair<String, String>(word0, word1); final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
bigramWords.add(bigram); bigramWords.add(bigram);
bigramProbabilities.put(bigram, bigramProbability); bigramProbabilities.put(bigram, bigramProbability);
binaryDictionary.addBigramWords(word0, word1, bigramProbability); addBigramWords(binaryDictionary, word0, word1, bigramProbability);
} }
// Remove bigram. // Remove bigram.
if (random.nextFloat() < removeBigramProb && !bigramWords.isEmpty()) { if (random.nextFloat() < removeBigramProb && !bigramWords.isEmpty()) {
@ -674,7 +688,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
unigramProbabilities.put(word, unigramProbability); unigramProbabilities.put(word, unigramProbability);
binaryDictionary.addUnigramWord(word, unigramProbability); addUnigramWord(binaryDictionary, word, unigramProbability);
} }
for (int j = 0; j < words.size(); j++) { for (int j = 0; j < words.size(); j++) {
@ -722,7 +736,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final String word = CodePointUtils.generateWord(random, codePointSet); final String word = CodePointUtils.generateWord(random, codePointSet);
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
binaryDictionary.addUnigramWord(word, unigramProbability); addUnigramWord(binaryDictionary, word, unigramProbability);
} }
for (int j = 0; j < bigramCountPerIteration; j++) { for (int j = 0; j < bigramCountPerIteration; j++) {
final String word0 = words.get(random.nextInt(words.size())); final String word0 = words.get(random.nextInt(words.size()));
@ -732,7 +746,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
} }
bigrams.add(new Pair<String, String>(word0, word1)); bigrams.add(new Pair<String, String>(word0, word1));
final int bigramProbability = random.nextInt(0xF); final int bigramProbability = random.nextInt(0xF);
binaryDictionary.addBigramWords(word0, word1, bigramProbability); addBigramWords(binaryDictionary, word0, word1, bigramProbability);
} }
assertEquals(new HashSet<String>(words).size(), Integer.parseInt( assertEquals(new HashSet<String>(words).size(), Integer.parseInt(
binaryDictionary.getPropertyForTests(BinaryDictionary.UNIGRAM_COUNT_QUERY))); binaryDictionary.getPropertyForTests(BinaryDictionary.UNIGRAM_COUNT_QUERY)));
@ -781,10 +795,10 @@ public class BinaryDictionaryTests extends AndroidTestCase {
unigramProbabilities.put(word, probability); unigramProbabilities.put(word, probability);
if (prevWord == null) { if (prevWord == null) {
languageModelParams[i] = new LanguageModelParam(word, probability, languageModelParams[i] = new LanguageModelParam(word, probability,
BinaryDictionary.NOT_A_VALID_TIME_STAMP); BinaryDictionary.NOT_A_VALID_TIMESTAMP);
} else { } else {
languageModelParams[i] = new LanguageModelParam(prevWord, word, probability, languageModelParams[i] = new LanguageModelParam(prevWord, word, probability,
bigramProbability, BinaryDictionary.NOT_A_VALID_TIME_STAMP); bigramProbability, BinaryDictionary.NOT_A_VALID_TIMESTAMP);
bigramProbabilities.put(new Pair<String, String>(prevWord, word), bigramProbabilities.put(new Pair<String, String>(prevWord, word),
bigramProbability); bigramProbability);
} }