2013-09-10 10:16:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.test.AndroidTestCase;
|
|
|
|
import android.test.suitebuilder.annotation.LargeTest;
|
2013-10-04 07:48:22 +00:00
|
|
|
import android.text.TextUtils;
|
2013-09-24 07:32:25 +00:00
|
|
|
import android.util.Pair;
|
2013-09-10 10:16:33 +00:00
|
|
|
|
2013-09-17 06:11:24 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.CodePointUtils;
|
2013-09-10 10:16:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2013-12-13 08:09:16 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
|
|
|
|
import com.android.inputmethod.latin.utils.FileUtils;
|
2014-01-15 07:04:05 +00:00
|
|
|
import com.android.inputmethod.latin.utils.LanguageModelParam;
|
2014-01-31 02:06:42 +00:00
|
|
|
import com.android.inputmethod.latin.utils.WordProperty;
|
2013-09-10 10:16:33 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2013-09-17 06:32:37 +00:00
|
|
|
import java.util.ArrayList;
|
2013-09-10 10:16:33 +00:00
|
|
|
import java.util.HashMap;
|
2013-09-27 14:12:12 +00:00
|
|
|
import java.util.HashSet;
|
2013-09-10 10:16:33 +00:00
|
|
|
import java.util.Locale;
|
2013-09-26 03:59:02 +00:00
|
|
|
import java.util.Map;
|
2013-09-17 06:11:24 +00:00
|
|
|
import java.util.Random;
|
2013-09-10 10:16:33 +00:00
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
// TODO Use the seed passed as an argument for makedict test.
|
2013-09-10 10:16:33 +00:00
|
|
|
@LargeTest
|
|
|
|
public class BinaryDictionaryTests extends AndroidTestCase {
|
|
|
|
private static final String TEST_DICT_FILE_EXTENSION = ".testDict";
|
|
|
|
private static final String TEST_LOCALE = "test";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void tearDown() throws Exception {
|
|
|
|
super.tearDown();
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
private File createEmptyDictionaryAndGetFile(final String dictId,
|
|
|
|
final int formatVersion) throws IOException {
|
|
|
|
if (formatVersion == FormatSpec.VERSION4) {
|
|
|
|
return createEmptyVer4DictionaryAndGetFile(dictId);
|
|
|
|
} else {
|
|
|
|
throw new IOException("Dictionary format version " + formatVersion
|
|
|
|
+ " is not supported.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private File createEmptyVer4DictionaryAndGetFile(final String dictId) throws IOException {
|
|
|
|
final File file = File.createTempFile(dictId, TEST_DICT_FILE_EXTENSION,
|
2013-09-10 10:16:33 +00:00
|
|
|
getContext().getCacheDir());
|
2013-12-13 08:09:16 +00:00
|
|
|
file.delete();
|
|
|
|
file.mkdir();
|
2013-09-26 03:59:02 +00:00
|
|
|
Map<String, String> attributeMap = new HashMap<String, String>();
|
|
|
|
if (BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
|
2013-12-13 08:09:16 +00:00
|
|
|
FormatSpec.VERSION4, attributeMap)) {
|
2013-09-26 03:59:02 +00:00
|
|
|
return file;
|
|
|
|
} else {
|
2013-12-13 08:09:16 +00:00
|
|
|
throw new IOException("Empty dictionary " + file.getAbsolutePath()
|
|
|
|
+ " cannot be created.");
|
2013-09-26 03:59:02 +00:00
|
|
|
}
|
2013-09-10 10:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testIsValidDictionary() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testIsValidDictionary(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testIsValidDictionary(final int formatVersion) {
|
2013-09-10 10:16:33 +00:00
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-10 10:16:33 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
assertTrue("binaryDictionary must be valid for existing valid dictionary file.",
|
|
|
|
binaryDictionary.isValidDictionary());
|
|
|
|
binaryDictionary.close();
|
|
|
|
assertFalse("binaryDictionary must be invalid after closing.",
|
|
|
|
binaryDictionary.isValidDictionary());
|
2013-12-13 08:09:16 +00:00
|
|
|
FileUtils.deleteRecursively(dictFile);
|
2013-09-10 10:16:33 +00:00
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(), 0 /* offset */,
|
|
|
|
dictFile.length(), true /* useFullEditDistance */, Locale.getDefault(),
|
|
|
|
TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
assertFalse("binaryDictionary must be invalid for not existing dictionary file.",
|
|
|
|
binaryDictionary.isValidDictionary());
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
2013-09-10 13:46:10 +00:00
|
|
|
|
2014-01-25 00:32:42 +00:00
|
|
|
public void testAddTooLongWord() {
|
|
|
|
testAddTooLongWord(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddTooLongWord(final int formatVersion) {
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final StringBuffer stringBuilder = new StringBuffer();
|
|
|
|
for (int i = 0; i < Constants.DICTIONARY_MAX_WORD_LENGTH; i++) {
|
|
|
|
stringBuilder.append('a');
|
|
|
|
}
|
|
|
|
final String validLongWord = stringBuilder.toString();
|
|
|
|
stringBuilder.append('a');
|
|
|
|
final String invalidLongWord = stringBuilder.toString();
|
|
|
|
final int probability = 100;
|
|
|
|
addUnigramWord(binaryDictionary, "aaa", probability);
|
|
|
|
addUnigramWord(binaryDictionary, validLongWord, probability);
|
|
|
|
addUnigramWord(binaryDictionary, invalidLongWord, probability);
|
|
|
|
// Too long short cut.
|
|
|
|
binaryDictionary.addUnigramWord("a", probability, invalidLongWord,
|
|
|
|
10 /* shortcutProbability */, false /* isNotAWord */, false /* isBlacklisted */,
|
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
|
|
|
addUnigramWord(binaryDictionary, "abc", probability);
|
|
|
|
final int updatedProbability = 200;
|
|
|
|
// Update.
|
|
|
|
addUnigramWord(binaryDictionary, validLongWord, updatedProbability);
|
|
|
|
addUnigramWord(binaryDictionary, invalidLongWord, updatedProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "abc", updatedProbability);
|
|
|
|
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("aaa"));
|
|
|
|
assertEquals(updatedProbability, binaryDictionary.getFrequency(validLongWord));
|
|
|
|
assertEquals(BinaryDictionary.NOT_A_PROBABILITY,
|
|
|
|
binaryDictionary.getFrequency(invalidLongWord));
|
|
|
|
assertEquals(updatedProbability, binaryDictionary.getFrequency("abc"));
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
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 */);
|
|
|
|
}
|
|
|
|
|
2013-09-10 13:46:10 +00:00
|
|
|
public void testAddUnigramWord() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testAddUnigramWord(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddUnigramWord(final int formatVersion) {
|
2013-09-10 13:46:10 +00:00
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-10 13:46:10 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final int probability = 100;
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaa", probability);
|
2013-09-10 13:46:10 +00:00
|
|
|
// Reallocate and create.
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aab", probability);
|
2013-09-10 13:46:10 +00:00
|
|
|
// Insert into children.
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aac", probability);
|
2013-09-10 13:46:10 +00:00
|
|
|
// Make terminal.
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aa", probability);
|
2013-09-10 13:46:10 +00:00
|
|
|
// Create children.
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaaa", probability);
|
2013-09-10 13:46:10 +00:00
|
|
|
// Reallocate and make termianl.
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "a", probability);
|
2013-09-10 13:46:10 +00:00
|
|
|
|
|
|
|
final int updatedProbability = 200;
|
|
|
|
// Update.
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaa", updatedProbability);
|
2013-09-10 13:46:10 +00:00
|
|
|
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("aab"));
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("aac"));
|
2013-09-17 06:11:24 +00:00
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("aa"));
|
2013-09-10 13:46:10 +00:00
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("aaaa"));
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("a"));
|
|
|
|
assertEquals(updatedProbability, binaryDictionary.getFrequency("aaa"));
|
2013-09-17 06:11:24 +00:00
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testRandomlyAddUnigramWord() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testRandomlyAddUnigramWord(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testRandomlyAddUnigramWord(final int formatVersion) {
|
2013-09-17 06:11:24 +00:00
|
|
|
final int wordCount = 1000;
|
|
|
|
final int codePointSetSize = 50;
|
2013-10-04 07:48:22 +00:00
|
|
|
final long seed = System.currentTimeMillis();
|
2013-09-17 06:11:24 +00:00
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-17 06:11:24 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final HashMap<String, Integer> probabilityMap = new HashMap<String, Integer>();
|
|
|
|
// Test a word that isn't contained within the dictionary.
|
|
|
|
final Random random = new Random(seed);
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
for (int i = 0; i < wordCount; ++i) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
2013-09-18 02:18:28 +00:00
|
|
|
probabilityMap.put(word, random.nextInt(0xFF));
|
2013-09-17 06:11:24 +00:00
|
|
|
}
|
|
|
|
for (String word : probabilityMap.keySet()) {
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, word, probabilityMap.get(word));
|
2013-09-17 06:11:24 +00:00
|
|
|
}
|
|
|
|
for (String word : probabilityMap.keySet()) {
|
|
|
|
assertEquals(word, (int)probabilityMap.get(word), binaryDictionary.getFrequency(word));
|
|
|
|
}
|
|
|
|
dictFile.delete();
|
2013-09-10 13:46:10 +00:00
|
|
|
}
|
2013-09-17 03:52:21 +00:00
|
|
|
|
|
|
|
public void testAddBigramWords() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testAddBigramWords(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddBigramWords(final int formatVersion) {
|
2013-09-17 03:52:21 +00:00
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-17 03:52:21 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final int unigramProbability = 100;
|
|
|
|
final int bigramProbability = 10;
|
2013-09-18 02:18:28 +00:00
|
|
|
final int updatedBigramProbability = 15;
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaa", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "abb", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "bcc", unigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "aaa", "bcc", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "abb", "aaa", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "abb", "bcc", bigramProbability);
|
2013-09-17 03:52:21 +00:00
|
|
|
|
2013-09-18 02:18:28 +00:00
|
|
|
final int probability = binaryDictionary.calculateProbability(unigramProbability,
|
|
|
|
bigramProbability);
|
2013-09-17 03:52:21 +00:00
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("aaa", "abb"));
|
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("aaa", "bcc"));
|
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("abb", "aaa"));
|
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("abb", "bcc"));
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("aaa", "abb"));
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("aaa", "bcc"));
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("abb", "aaa"));
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("abb", "bcc"));
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, "aaa", "abb", updatedBigramProbability);
|
2013-09-18 02:18:28 +00:00
|
|
|
final int updatedProbability = binaryDictionary.calculateProbability(unigramProbability,
|
|
|
|
updatedBigramProbability);
|
|
|
|
assertEquals(updatedProbability, binaryDictionary.getBigramProbability("aaa", "abb"));
|
2013-09-17 03:52:21 +00:00
|
|
|
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("bcc", "aaa"));
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("bcc", "bbc"));
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("aaa", "aaa"));
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
|
|
|
binaryDictionary.getBigramProbability("bcc", "aaa"));
|
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
|
|
|
binaryDictionary.getBigramProbability("bcc", "bbc"));
|
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
|
|
|
binaryDictionary.getBigramProbability("aaa", "aaa"));
|
|
|
|
|
|
|
|
// Testing bigram link.
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "abcde", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "fghij", unigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "abcde", "fghij", bigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "fgh", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "abc", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "f", unigramProbability);
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("abcde", "fghij"));
|
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
|
|
|
binaryDictionary.getBigramProbability("abcde", "fgh"));
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, "abcde", "fghij", updatedBigramProbability);
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(updatedProbability, binaryDictionary.getBigramProbability("abcde", "fghij"));
|
2013-09-17 03:52:21 +00:00
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-09-17 06:32:37 +00:00
|
|
|
|
|
|
|
public void testRandomlyAddBigramWords() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testRandomlyAddBigramWords(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testRandomlyAddBigramWords(final int formatVersion) {
|
2013-09-17 06:32:37 +00:00
|
|
|
final int wordCount = 100;
|
|
|
|
final int bigramCount = 1000;
|
|
|
|
final int codePointSetSize = 50;
|
2013-10-04 07:48:22 +00:00
|
|
|
final long seed = System.currentTimeMillis();
|
|
|
|
final Random random = new Random(seed);
|
2013-09-17 08:49:22 +00:00
|
|
|
|
2013-09-17 06:32:37 +00:00
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-17 06:32:37 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
2013-10-04 07:48:22 +00:00
|
|
|
|
2013-09-17 06:32:37 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
2013-10-04 07:48:22 +00:00
|
|
|
final ArrayList<Pair<String, String>> bigramWords = new ArrayList<Pair<String,String>>();
|
2013-09-17 06:32:37 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
2013-10-04 07:48:22 +00:00
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities =
|
|
|
|
new HashMap<Pair<String, String>, Integer>();
|
|
|
|
|
2013-09-17 06:32:37 +00:00
|
|
|
for (int i = 0; i < wordCount; ++i) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
words.add(word);
|
2013-09-18 02:18:28 +00:00
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
2013-10-04 07:48:22 +00:00
|
|
|
unigramProbabilities.put(word, unigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
2013-09-17 06:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < bigramCount; i++) {
|
2013-10-04 07:48:22 +00:00
|
|
|
final String word0 = words.get(random.nextInt(wordCount));
|
|
|
|
final String word1 = words.get(random.nextInt(wordCount));
|
|
|
|
if (TextUtils.equals(word0, word1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
|
|
|
|
bigramWords.add(bigram);
|
2013-09-18 02:18:28 +00:00
|
|
|
final int bigramProbability = random.nextInt(0xF);
|
2013-10-04 07:48:22 +00:00
|
|
|
bigramProbabilities.put(bigram, bigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, word0, word1, bigramProbability);
|
2013-09-17 06:32:37 +00:00
|
|
|
}
|
|
|
|
|
2013-10-04 07:48:22 +00:00
|
|
|
for (final Pair<String, String> bigram : bigramWords) {
|
|
|
|
final int unigramProbability = unigramProbabilities.get(bigram.second);
|
|
|
|
final int bigramProbability = bigramProbabilities.get(bigram);
|
|
|
|
final int probability = binaryDictionary.calculateProbability(unigramProbability,
|
|
|
|
bigramProbability);
|
|
|
|
assertEquals(probability,
|
|
|
|
binaryDictionary.getBigramProbability(bigram.first, bigram.second));
|
2013-09-17 06:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-09-17 08:49:22 +00:00
|
|
|
|
|
|
|
public void testRemoveBigramWords() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testRemoveBigramWords(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testRemoveBigramWords(final int formatVersion) {
|
2013-09-17 08:49:22 +00:00
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-17 08:49:22 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
final int unigramProbability = 100;
|
|
|
|
final int bigramProbability = 10;
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaa", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "abb", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "bcc", unigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "aaa", "bcc", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "abb", "aaa", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "abb", "bcc", bigramProbability);
|
2013-09-17 08:49:22 +00:00
|
|
|
|
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("aaa", "abb"));
|
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("aaa", "bcc"));
|
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("abb", "aaa"));
|
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("abb", "bcc"));
|
|
|
|
|
|
|
|
binaryDictionary.removeBigramWords("aaa", "abb");
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("aaa", "abb"));
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
|
2013-09-17 08:49:22 +00:00
|
|
|
assertEquals(true, binaryDictionary.isValidBigram("aaa", "abb"));
|
|
|
|
|
|
|
|
|
|
|
|
binaryDictionary.removeBigramWords("aaa", "bcc");
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("aaa", "bcc"));
|
|
|
|
binaryDictionary.removeBigramWords("abb", "aaa");
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("abb", "aaa"));
|
|
|
|
binaryDictionary.removeBigramWords("abb", "bcc");
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("abb", "bcc"));
|
|
|
|
|
|
|
|
binaryDictionary.removeBigramWords("aaa", "abb");
|
|
|
|
// Test remove non-existing bigram operation.
|
|
|
|
binaryDictionary.removeBigramWords("aaa", "abb");
|
|
|
|
binaryDictionary.removeBigramWords("bcc", "aaa");
|
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-09-18 04:19:14 +00:00
|
|
|
|
|
|
|
public void testFlushDictionary() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testFlushDictionary(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testFlushDictionary(final int formatVersion) {
|
2013-09-18 04:19:14 +00:00
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-18 04:19:14 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final int probability = 100;
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaa", probability);
|
|
|
|
addUnigramWord(binaryDictionary, "abcd", probability);
|
2013-09-18 04:19:14 +00:00
|
|
|
// Close without flushing.
|
|
|
|
binaryDictionary.close();
|
|
|
|
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
2013-09-18 09:08:33 +00:00
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY, binaryDictionary.getFrequency("aaa"));
|
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY, binaryDictionary.getFrequency("abcd"));
|
2013-09-18 04:19:14 +00:00
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaa", probability);
|
|
|
|
addUnigramWord(binaryDictionary, "abcd", probability);
|
2013-09-18 04:19:14 +00:00
|
|
|
binaryDictionary.flush();
|
|
|
|
binaryDictionary.close();
|
|
|
|
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("aaa"));
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("abcd"));
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "bcde", probability);
|
2013-09-18 04:19:14 +00:00
|
|
|
binaryDictionary.flush();
|
|
|
|
binaryDictionary.close();
|
|
|
|
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("bcde"));
|
|
|
|
binaryDictionary.close();
|
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-09-24 06:29:56 +00:00
|
|
|
|
|
|
|
public void testFlushWithGCDictionary() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testFlushWithGCDictionary(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testFlushWithGCDictionary(final int formatVersion) {
|
2013-09-24 06:29:56 +00:00
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-24 06:29:56 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final int unigramProbability = 100;
|
|
|
|
final int bigramProbability = 10;
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, "aaa", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "abb", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "bcc", unigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "aaa", "bcc", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "abb", "aaa", bigramProbability);
|
|
|
|
addBigramWords(binaryDictionary, "abb", "bcc", bigramProbability);
|
2013-09-24 06:29:56 +00:00
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
final int probability = binaryDictionary.calculateProbability(unigramProbability,
|
|
|
|
bigramProbability);
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("aaa"));
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("abb"));
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("bcc"));
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("aaa", "abb"));
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("aaa", "bcc"));
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("abb", "aaa"));
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability("abb", "bcc"));
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("bcc", "aaa"));
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("bcc", "bbc"));
|
|
|
|
assertEquals(false, binaryDictionary.isValidBigram("aaa", "aaa"));
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-09-24 07:32:25 +00:00
|
|
|
|
|
|
|
public void testAddBigramWordsAndFlashWithGC() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testAddBigramWordsAndFlashWithGC(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Evaluate performance of GC
|
|
|
|
private void testAddBigramWordsAndFlashWithGC(final int formatVersion) {
|
2013-09-24 07:32:25 +00:00
|
|
|
final int wordCount = 100;
|
|
|
|
final int bigramCount = 1000;
|
|
|
|
final int codePointSetSize = 30;
|
2013-10-04 07:48:22 +00:00
|
|
|
final long seed = System.currentTimeMillis();
|
|
|
|
final Random random = new Random(seed);
|
2013-09-24 07:32:25 +00:00
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-24 07:32:25 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
2013-10-04 07:48:22 +00:00
|
|
|
|
2013-09-24 07:32:25 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
2013-10-04 07:48:22 +00:00
|
|
|
final ArrayList<Pair<String, String>> bigramWords = new ArrayList<Pair<String,String>>();
|
2013-09-24 07:32:25 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
2013-10-04 07:48:22 +00:00
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities =
|
|
|
|
new HashMap<Pair<String, String>, Integer>();
|
|
|
|
|
2013-09-24 07:32:25 +00:00
|
|
|
for (int i = 0; i < wordCount; ++i) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
words.add(word);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
2013-10-04 07:48:22 +00:00
|
|
|
unigramProbabilities.put(word, unigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < bigramCount; i++) {
|
2013-10-04 07:48:22 +00:00
|
|
|
final String word0 = words.get(random.nextInt(wordCount));
|
|
|
|
final String word1 = words.get(random.nextInt(wordCount));
|
|
|
|
if (TextUtils.equals(word0, word1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
|
|
|
|
bigramWords.add(bigram);
|
2013-09-24 07:32:25 +00:00
|
|
|
final int bigramProbability = random.nextInt(0xF);
|
2013-10-04 07:48:22 +00:00
|
|
|
bigramProbabilities.put(bigram, bigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, word0, word1, bigramProbability);
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
2013-10-04 07:48:22 +00:00
|
|
|
for (final Pair<String, String> bigram : bigramWords) {
|
|
|
|
final int unigramProbability = unigramProbabilities.get(bigram.second);
|
|
|
|
final int bigramProbability = bigramProbabilities.get(bigram);
|
|
|
|
final int probability = binaryDictionary.calculateProbability(unigramProbability,
|
|
|
|
bigramProbability);
|
|
|
|
assertEquals(probability,
|
|
|
|
binaryDictionary.getBigramProbability(bigram.first, bigram.second));
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
2013-10-04 07:48:22 +00:00
|
|
|
|
2013-09-24 07:32:25 +00:00
|
|
|
dictFile.delete();
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public void testRandomOperationsAndFlashWithGC() {
|
|
|
|
testRandomOperationsAndFlashWithGC(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testRandomOperationsAndFlashWithGC(final int formatVersion) {
|
2013-09-24 07:32:25 +00:00
|
|
|
final int flashWithGCIterationCount = 50;
|
|
|
|
final int operationCountInEachIteration = 200;
|
|
|
|
final int initialUnigramCount = 100;
|
|
|
|
final float addUnigramProb = 0.5f;
|
|
|
|
final float addBigramProb = 0.8f;
|
|
|
|
final float removeBigramProb = 0.2f;
|
|
|
|
final int codePointSetSize = 30;
|
|
|
|
|
2013-10-04 07:48:22 +00:00
|
|
|
final long seed = System.currentTimeMillis();
|
2013-09-24 07:32:25 +00:00
|
|
|
final Random random = new Random(seed);
|
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-24 07:32:25 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
|
|
|
final ArrayList<Pair<String, String>> bigramWords = new ArrayList<Pair<String,String>>();
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities =
|
|
|
|
new HashMap<Pair<String, String>, Integer>();
|
|
|
|
for (int i = 0; i < initialUnigramCount; ++i) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
words.add(word);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
|
|
|
unigramProbabilities.put(word, unigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
|
|
|
|
for (int gcCount = 0; gcCount < flashWithGCIterationCount; gcCount++) {
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
for (int opCount = 0; opCount < operationCountInEachIteration; opCount++) {
|
|
|
|
// Add unigram.
|
|
|
|
if (random.nextFloat() < addUnigramProb) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
words.add(word);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
|
|
|
unigramProbabilities.put(word, unigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
// Add bigram.
|
|
|
|
if (random.nextFloat() < addBigramProb && words.size() > 2) {
|
|
|
|
final int word0Index = random.nextInt(words.size());
|
|
|
|
int word1Index = random.nextInt(words.size() - 1);
|
|
|
|
if (word0Index <= word1Index) {
|
|
|
|
word1Index++;
|
|
|
|
}
|
|
|
|
final String word0 = words.get(word0Index);
|
|
|
|
final String word1 = words.get(word1Index);
|
2013-10-04 07:48:22 +00:00
|
|
|
if (TextUtils.equals(word0, word1)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-24 07:32:25 +00:00
|
|
|
final int bigramProbability = random.nextInt(0xF);
|
|
|
|
final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
|
|
|
|
bigramWords.add(bigram);
|
|
|
|
bigramProbabilities.put(bigram, bigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, word0, word1, bigramProbability);
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
// Remove bigram.
|
|
|
|
if (random.nextFloat() < removeBigramProb && !bigramWords.isEmpty()) {
|
|
|
|
final int bigramIndex = random.nextInt(bigramWords.size());
|
|
|
|
final Pair<String, String> bigram = bigramWords.get(bigramIndex);
|
|
|
|
bigramWords.remove(bigramIndex);
|
|
|
|
bigramProbabilities.remove(bigram);
|
|
|
|
binaryDictionary.removeBigramWords(bigram.first, bigram.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test whether the all unigram operations are collectlly handled.
|
|
|
|
for (int i = 0; i < words.size(); i++) {
|
|
|
|
final String word = words.get(i);
|
|
|
|
final int unigramProbability = unigramProbabilities.get(word);
|
|
|
|
assertEquals(word, unigramProbability, binaryDictionary.getFrequency(word));
|
|
|
|
}
|
|
|
|
// Test whether the all bigram operations are collectlly handled.
|
|
|
|
for (int i = 0; i < bigramWords.size(); i++) {
|
|
|
|
final Pair<String, String> bigram = bigramWords.get(i);
|
|
|
|
final int unigramProbability = unigramProbabilities.get(bigram.second);
|
|
|
|
final int probability;
|
|
|
|
if (bigramProbabilities.containsKey(bigram)) {
|
|
|
|
final int bigramProbability = bigramProbabilities.get(bigram);
|
|
|
|
probability = binaryDictionary.calculateProbability(unigramProbability,
|
|
|
|
bigramProbability);
|
|
|
|
} else {
|
|
|
|
probability = Dictionary.NOT_A_PROBABILITY;
|
|
|
|
}
|
|
|
|
assertEquals(probability,
|
|
|
|
binaryDictionary.getBigramProbability(bigram.first, bigram.second));
|
|
|
|
}
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-09-24 10:21:17 +00:00
|
|
|
|
|
|
|
public void testAddManyUnigramsAndFlushWithGC() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testAddManyUnigramsAndFlushWithGC(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddManyUnigramsAndFlushWithGC(final int formatVersion) {
|
2013-09-24 10:21:17 +00:00
|
|
|
final int flashWithGCIterationCount = 3;
|
|
|
|
final int codePointSetSize = 50;
|
|
|
|
|
2013-10-04 07:48:22 +00:00
|
|
|
final long seed = System.currentTimeMillis();
|
2013-09-24 10:21:17 +00:00
|
|
|
final Random random = new Random(seed);
|
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-24 10:21:17 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
|
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
|
|
|
|
BinaryDictionary binaryDictionary;
|
|
|
|
for (int i = 0; i < flashWithGCIterationCount; i++) {
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
2013-09-30 05:21:48 +00:00
|
|
|
while(!binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) {
|
2013-09-24 10:21:17 +00:00
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
words.add(word);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
|
|
|
unigramProbabilities.put(word, unigramProbability);
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
2013-09-24 10:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < words.size(); j++) {
|
|
|
|
final String word = words.get(j);
|
|
|
|
final int unigramProbability = unigramProbabilities.get(word);
|
|
|
|
assertEquals(word, unigramProbability, binaryDictionary.getFrequency(word));
|
|
|
|
}
|
|
|
|
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-09-27 14:12:12 +00:00
|
|
|
|
|
|
|
public void testUnigramAndBigramCount() {
|
2013-12-13 08:09:16 +00:00
|
|
|
testUnigramAndBigramCount(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testUnigramAndBigramCount(final int formatVersion) {
|
2013-09-27 14:12:12 +00:00
|
|
|
final int flashWithGCIterationCount = 10;
|
|
|
|
final int codePointSetSize = 50;
|
|
|
|
final int unigramCountPerIteration = 1000;
|
|
|
|
final int bigramCountPerIteration = 2000;
|
2013-10-04 07:48:22 +00:00
|
|
|
final long seed = System.currentTimeMillis();
|
2013-09-27 14:12:12 +00:00
|
|
|
final Random random = new Random(seed);
|
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
2013-09-27 14:12:12 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
|
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
|
|
|
final HashSet<Pair<String, String>> bigrams = new HashSet<Pair<String, String>>();
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
|
|
|
|
BinaryDictionary binaryDictionary;
|
|
|
|
for (int i = 0; i < flashWithGCIterationCount; i++) {
|
|
|
|
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
for (int j = 0; j < unigramCountPerIteration; j++) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
words.add(word);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
2013-12-13 08:09:16 +00:00
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
2013-09-27 14:12:12 +00:00
|
|
|
}
|
|
|
|
for (int j = 0; j < bigramCountPerIteration; j++) {
|
|
|
|
final String word0 = words.get(random.nextInt(words.size()));
|
|
|
|
final String word1 = words.get(random.nextInt(words.size()));
|
2013-10-04 07:48:22 +00:00
|
|
|
if (TextUtils.equals(word0, word1)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-27 14:12:12 +00:00
|
|
|
bigrams.add(new Pair<String, String>(word0, word1));
|
|
|
|
final int bigramProbability = random.nextInt(0xF);
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, word0, word1, bigramProbability);
|
2013-09-27 14:12:12 +00:00
|
|
|
}
|
|
|
|
assertEquals(new HashSet<String>(words).size(), Integer.parseInt(
|
2014-01-15 22:55:57 +00:00
|
|
|
binaryDictionary.getPropertyForTest(BinaryDictionary.UNIGRAM_COUNT_QUERY)));
|
2013-09-27 14:12:12 +00:00
|
|
|
assertEquals(new HashSet<Pair<String, String>>(bigrams).size(), Integer.parseInt(
|
2014-01-15 22:55:57 +00:00
|
|
|
binaryDictionary.getPropertyForTest(BinaryDictionary.BIGRAM_COUNT_QUERY)));
|
2013-09-27 14:12:12 +00:00
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
assertEquals(new HashSet<String>(words).size(), Integer.parseInt(
|
2014-01-15 22:55:57 +00:00
|
|
|
binaryDictionary.getPropertyForTest(BinaryDictionary.UNIGRAM_COUNT_QUERY)));
|
2013-09-27 14:12:12 +00:00
|
|
|
assertEquals(new HashSet<Pair<String, String>>(bigrams).size(), Integer.parseInt(
|
2014-01-15 22:55:57 +00:00
|
|
|
binaryDictionary.getPropertyForTest(BinaryDictionary.BIGRAM_COUNT_QUERY)));
|
2013-09-27 14:12:12 +00:00
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
|
|
|
|
public void testAddMultipleDictionaryEntries() {
|
|
|
|
testAddMultipleDictionaryEntries(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddMultipleDictionaryEntries(final int formatVersion) {
|
|
|
|
final int codePointSetSize = 20;
|
|
|
|
final int lmParamCount = 1000;
|
|
|
|
final double bigramContinueRate = 0.9;
|
|
|
|
final long seed = System.currentTimeMillis();
|
|
|
|
final Random random = new Random(seed);
|
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities =
|
|
|
|
new HashMap<Pair<String, String>, Integer>();
|
|
|
|
|
|
|
|
final LanguageModelParam[] languageModelParams = new LanguageModelParam[lmParamCount];
|
|
|
|
String prevWord = null;
|
|
|
|
for (int i = 0; i < languageModelParams.length; i++) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
final int probability = random.nextInt(0xFF);
|
|
|
|
final int bigramProbability = random.nextInt(0xF);
|
|
|
|
unigramProbabilities.put(word, probability);
|
|
|
|
if (prevWord == null) {
|
|
|
|
languageModelParams[i] = new LanguageModelParam(word, probability,
|
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
|
|
|
} else {
|
|
|
|
languageModelParams[i] = new LanguageModelParam(prevWord, word, probability,
|
|
|
|
bigramProbability, BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
|
|
|
bigramProbabilities.put(new Pair<String, String>(prevWord, word),
|
|
|
|
bigramProbability);
|
|
|
|
}
|
|
|
|
prevWord = (random.nextDouble() < bigramContinueRate) ? word : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
binaryDictionary.addMultipleDictionaryEntries(languageModelParams);
|
|
|
|
|
|
|
|
for (Map.Entry<String, Integer> entry : unigramProbabilities.entrySet()) {
|
|
|
|
assertEquals((int)entry.getValue(), binaryDictionary.getFrequency(entry.getKey()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Map.Entry<Pair<String, String>, Integer> entry : bigramProbabilities.entrySet()) {
|
|
|
|
final String word0 = entry.getKey().first;
|
|
|
|
final String word1 = entry.getKey().second;
|
|
|
|
final int unigramProbability = unigramProbabilities.get(word1);
|
|
|
|
final int bigramProbability = entry.getValue();
|
|
|
|
final int probability = binaryDictionary.calculateProbability(
|
|
|
|
unigramProbability, bigramProbability);
|
|
|
|
assertEquals(probability, binaryDictionary.getBigramProbability(word0, word1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 11:32:44 +00:00
|
|
|
public void testGetWordProperties() {
|
|
|
|
testGetWordProperties(FormatSpec.VERSION4);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 11:32:44 +00:00
|
|
|
private void testGetWordProperties(final int formatVersion) {
|
2013-12-13 08:09:16 +00:00
|
|
|
final long seed = System.currentTimeMillis();
|
|
|
|
final Random random = new Random(seed);
|
2014-02-03 02:47:26 +00:00
|
|
|
final int UNIGRAM_COUNT = 1000;
|
|
|
|
final int BIGRAM_COUNT = 1000;
|
2013-12-13 08:09:16 +00:00
|
|
|
final int codePointSetSize = 20;
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
2014-01-31 11:32:44 +00:00
|
|
|
final WordProperty invalidWordProperty = binaryDictionary.getWordProperty("dummyWord");
|
|
|
|
assertFalse(invalidWordProperty.isValid());
|
2013-12-13 08:09:16 +00:00
|
|
|
|
2014-02-03 02:47:26 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
|
|
|
final HashMap<String, Integer> wordProbabilities = new HashMap<String, Integer>();
|
|
|
|
final HashMap<String, HashSet<String>> bigrams = new HashMap<String, HashSet<String>>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities =
|
|
|
|
new HashMap<Pair<String, String>, Integer>();
|
|
|
|
|
|
|
|
for (int i = 0; i < UNIGRAM_COUNT; i++) {
|
2013-12-13 08:09:16 +00:00
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
|
|
|
final boolean isNotAWord = random.nextBoolean();
|
|
|
|
final boolean isBlacklisted = random.nextBoolean();
|
|
|
|
// TODO: Add tests for historical info.
|
|
|
|
binaryDictionary.addUnigramWord(word, unigramProbability,
|
|
|
|
null /* shortcutTarget */, BinaryDictionary.NOT_A_PROBABILITY,
|
|
|
|
isNotAWord, isBlacklisted, BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
2014-02-03 02:47:26 +00:00
|
|
|
if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
words.add(word);
|
|
|
|
wordProbabilities.put(word, unigramProbability);
|
|
|
|
final WordProperty unigramProperty = binaryDictionary.getWordProperty(word);
|
|
|
|
assertEquals(word, unigramProperty.mCodePoints);
|
|
|
|
assertTrue(unigramProperty.isValid());
|
|
|
|
assertEquals(isNotAWord, unigramProperty.mIsNotAWord);
|
|
|
|
assertEquals(isBlacklisted, unigramProperty.mIsBlacklisted);
|
|
|
|
assertEquals(false, unigramProperty.mHasBigrams);
|
|
|
|
assertEquals(false, unigramProperty.mHasShortcuts);
|
|
|
|
assertEquals(unigramProbability, unigramProperty.mProbabilityInfo.mProbability);
|
|
|
|
assertTrue(unigramProperty.mShortcutTargets.isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < BIGRAM_COUNT; i++) {
|
|
|
|
final int word0Index = random.nextInt(wordProbabilities.size());
|
|
|
|
final int word1Index = random.nextInt(wordProbabilities.size());
|
|
|
|
if (word0Index == word1Index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String word0 = words.get(word0Index);
|
|
|
|
final String word1 = words.get(word1Index);
|
|
|
|
final int bigramProbability = random.nextInt(0xF);
|
|
|
|
binaryDictionary.addBigramWords(word0, word1, bigramProbability,
|
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
|
|
|
if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
if (!bigrams.containsKey(word0)) {
|
|
|
|
final HashSet<String> bigramWord1s = new HashSet<String>();
|
|
|
|
bigrams.put(word0, bigramWord1s);
|
|
|
|
}
|
|
|
|
bigrams.get(word0).add(word1);
|
|
|
|
bigramProbabilities.put(new Pair<String, String>(word0, word1), bigramProbability);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < words.size(); i++) {
|
|
|
|
final String word0 = words.get(i);
|
|
|
|
if (!bigrams.containsKey(word0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final HashSet<String> bigramWord1s = bigrams.get(word0);
|
|
|
|
final WordProperty unigramProperty = binaryDictionary.getWordProperty(word0);
|
|
|
|
assertEquals(bigramWord1s.size(), unigramProperty.mBigramTargets.size());
|
|
|
|
assertEquals(unigramProperty.mBigramTargets.size(),
|
|
|
|
unigramProperty.mBigramProbabilityInfo.size());
|
|
|
|
for (int j = 0; j < unigramProperty.mBigramTargets.size(); j++) {
|
|
|
|
final String word1 = unigramProperty.mBigramTargets.get(j).mWord;
|
|
|
|
assertTrue(bigramWord1s.contains(word1));
|
2014-02-05 12:44:55 +00:00
|
|
|
final int probability = unigramProperty.mBigramTargets.get(j).getProbability();
|
2014-02-03 02:47:26 +00:00
|
|
|
assertEquals((int)bigramProbabilities.get(new Pair<String, String>(word0, word1)),
|
|
|
|
probability);
|
|
|
|
assertEquals(unigramProperty.mBigramProbabilityInfo.get(j).mProbability,
|
|
|
|
probability);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 13:22:35 +00:00
|
|
|
public void testIterateAllWords() {
|
|
|
|
testIterateAllWords(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testIterateAllWords(final int formatVersion) {
|
|
|
|
final long seed = System.currentTimeMillis();
|
|
|
|
final Random random = new Random(seed);
|
|
|
|
final int UNIGRAM_COUNT = 1000;
|
|
|
|
final int BIGRAM_COUNT = 1000;
|
|
|
|
final int codePointSetSize = 20;
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final WordProperty invalidWordProperty = binaryDictionary.getWordProperty("dummyWord");
|
|
|
|
assertFalse(invalidWordProperty.isValid());
|
|
|
|
|
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
|
|
|
final HashMap<String, Integer> wordProbabilitiesToCheckLater =
|
|
|
|
new HashMap<String, Integer>();
|
|
|
|
final HashMap<String, HashSet<String>> bigrams = new HashMap<String, HashSet<String>>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilitiesToCheckLater =
|
|
|
|
new HashMap<Pair<String, String>, Integer>();
|
|
|
|
|
|
|
|
for (int i = 0; i < UNIGRAM_COUNT; i++) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
|
|
|
if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
words.add(word);
|
|
|
|
wordProbabilitiesToCheckLater.put(word, unigramProbability);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < BIGRAM_COUNT; i++) {
|
|
|
|
final int word0Index = random.nextInt(wordProbabilitiesToCheckLater.size());
|
|
|
|
final int word1Index = random.nextInt(wordProbabilitiesToCheckLater.size());
|
|
|
|
if (word0Index == word1Index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String word0 = words.get(word0Index);
|
|
|
|
final String word1 = words.get(word1Index);
|
|
|
|
final int bigramProbability = random.nextInt(0xF);
|
|
|
|
binaryDictionary.addBigramWords(word0, word1, bigramProbability,
|
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
|
|
|
if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
if (!bigrams.containsKey(word0)) {
|
|
|
|
final HashSet<String> bigramWord1s = new HashSet<String>();
|
|
|
|
bigrams.put(word0, bigramWord1s);
|
|
|
|
}
|
|
|
|
bigrams.get(word0).add(word1);
|
|
|
|
bigramProbabilitiesToCheckLater.put(
|
|
|
|
new Pair<String, String>(word0, word1), bigramProbability);
|
|
|
|
}
|
|
|
|
|
|
|
|
final HashSet<String> wordSet = new HashSet<String>(words);
|
|
|
|
final HashSet<Pair<String, String>> bigramSet =
|
|
|
|
new HashSet<Pair<String,String>>(bigramProbabilitiesToCheckLater.keySet());
|
|
|
|
int token = 0;
|
|
|
|
do {
|
|
|
|
final BinaryDictionary.GetNextWordPropertyResult result =
|
|
|
|
binaryDictionary.getNextWordProperty(token);
|
|
|
|
final WordProperty wordProperty = result.mWordProperty;
|
|
|
|
final String word0 = wordProperty.mCodePoints;
|
|
|
|
assertEquals((int)wordProbabilitiesToCheckLater.get(word0),
|
|
|
|
wordProperty.mProbabilityInfo.mProbability);
|
|
|
|
wordSet.remove(word0);
|
|
|
|
final HashSet<String> bigramWord1s = bigrams.get(word0);
|
|
|
|
for (int j = 0; j < wordProperty.mBigramTargets.size(); j++) {
|
|
|
|
final String word1 = wordProperty.mBigramTargets.get(j).mWord;
|
|
|
|
assertTrue(bigramWord1s.contains(word1));
|
2014-02-05 12:44:55 +00:00
|
|
|
final int probability = wordProperty.mBigramTargets.get(j).getProbability();
|
2014-02-03 13:22:35 +00:00
|
|
|
final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
|
|
|
|
assertEquals((int)bigramProbabilitiesToCheckLater.get(bigram), probability);
|
|
|
|
bigramSet.remove(bigram);
|
|
|
|
}
|
|
|
|
token = result.mNextToken;
|
|
|
|
} while (token != 0);
|
|
|
|
assertTrue(wordSet.isEmpty());
|
|
|
|
assertTrue(bigramSet.isEmpty());
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public void testAddShortcuts() {
|
|
|
|
testAddShortcuts(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddShortcuts(final int formatVersion) {
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
final int unigramProbability = 100;
|
|
|
|
final int shortcutProbability = 10;
|
|
|
|
binaryDictionary.addUnigramWord("aaa", unigramProbability, "zzz",
|
|
|
|
shortcutProbability, false /* isNotAWord */, false /* isBlacklisted */,
|
|
|
|
0 /* timestamp */);
|
2014-01-31 11:32:44 +00:00
|
|
|
WordProperty wordProperty = binaryDictionary.getWordProperty("aaa");
|
|
|
|
assertEquals(1, wordProperty.mShortcutTargets.size());
|
|
|
|
assertEquals("zzz", wordProperty.mShortcutTargets.get(0).mWord);
|
2014-02-05 12:44:55 +00:00
|
|
|
assertEquals(shortcutProbability, wordProperty.mShortcutTargets.get(0).getProbability());
|
2013-12-13 08:09:16 +00:00
|
|
|
final int updatedShortcutProbability = 2;
|
|
|
|
binaryDictionary.addUnigramWord("aaa", unigramProbability, "zzz",
|
|
|
|
updatedShortcutProbability, false /* isNotAWord */, false /* isBlacklisted */,
|
|
|
|
0 /* timestamp */);
|
2014-01-31 11:32:44 +00:00
|
|
|
wordProperty = binaryDictionary.getWordProperty("aaa");
|
|
|
|
assertEquals(1, wordProperty.mShortcutTargets.size());
|
|
|
|
assertEquals("zzz", wordProperty.mShortcutTargets.get(0).mWord);
|
2013-12-13 08:09:16 +00:00
|
|
|
assertEquals(updatedShortcutProbability,
|
2014-02-05 12:44:55 +00:00
|
|
|
wordProperty.mShortcutTargets.get(0).getProbability());
|
2013-12-13 08:09:16 +00:00
|
|
|
binaryDictionary.addUnigramWord("aaa", unigramProbability, "yyy",
|
|
|
|
shortcutProbability, false /* isNotAWord */, false /* isBlacklisted */,
|
|
|
|
0 /* timestamp */);
|
|
|
|
final HashMap<String, Integer> shortcutTargets = new HashMap<String, Integer>();
|
|
|
|
shortcutTargets.put("zzz", updatedShortcutProbability);
|
|
|
|
shortcutTargets.put("yyy", shortcutProbability);
|
2014-01-31 11:32:44 +00:00
|
|
|
wordProperty = binaryDictionary.getWordProperty("aaa");
|
|
|
|
assertEquals(2, wordProperty.mShortcutTargets.size());
|
|
|
|
for (WeightedString shortcutTarget : wordProperty.mShortcutTargets) {
|
2013-12-13 08:09:16 +00:00
|
|
|
assertTrue(shortcutTargets.containsKey(shortcutTarget.mWord));
|
2014-02-05 12:44:55 +00:00
|
|
|
assertEquals((int)shortcutTargets.get(shortcutTarget.mWord),
|
|
|
|
shortcutTarget.getProbability());
|
2013-12-13 08:09:16 +00:00
|
|
|
shortcutTargets.remove(shortcutTarget.mWord);
|
|
|
|
}
|
|
|
|
shortcutTargets.put("zzz", updatedShortcutProbability);
|
|
|
|
shortcutTargets.put("yyy", shortcutProbability);
|
|
|
|
binaryDictionary.flushWithGC();
|
2014-01-31 11:32:44 +00:00
|
|
|
wordProperty = binaryDictionary.getWordProperty("aaa");
|
|
|
|
assertEquals(2, wordProperty.mShortcutTargets.size());
|
|
|
|
for (WeightedString shortcutTarget : wordProperty.mShortcutTargets) {
|
2013-12-13 08:09:16 +00:00
|
|
|
assertTrue(shortcutTargets.containsKey(shortcutTarget.mWord));
|
2014-02-05 12:44:55 +00:00
|
|
|
assertEquals((int)shortcutTargets.get(shortcutTarget.mWord),
|
|
|
|
shortcutTarget.getProbability());
|
2013-12-13 08:09:16 +00:00
|
|
|
shortcutTargets.remove(shortcutTarget.mWord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testAddManyShortcuts() {
|
|
|
|
testAddManyShortcuts(FormatSpec.VERSION4);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddManyShortcuts(final int formatVersion) {
|
|
|
|
final long seed = System.currentTimeMillis();
|
|
|
|
final Random random = new Random(seed);
|
|
|
|
final int UNIGRAM_COUNT = 1000;
|
|
|
|
final int SHORTCUT_COUNT = 10000;
|
|
|
|
final int codePointSetSize = 20;
|
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
|
|
|
|
final ArrayList<String> words = new ArrayList<String>();
|
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
|
|
|
|
final HashMap<String, HashMap<String, Integer>> shortcutTargets =
|
|
|
|
new HashMap<String, HashMap<String, Integer>>();
|
|
|
|
|
|
|
|
File dictFile = null;
|
|
|
|
try {
|
|
|
|
dictFile = createEmptyDictionaryAndGetFile("TestBinaryDictionary", formatVersion);
|
|
|
|
} catch (IOException e) {
|
|
|
|
fail("IOException while writing an initial dictionary : " + e);
|
|
|
|
}
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
|
|
|
|
for (int i = 0; i < UNIGRAM_COUNT; i++) {
|
|
|
|
final String word = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
final int unigramProbability = random.nextInt(0xFF);
|
|
|
|
addUnigramWord(binaryDictionary, word, unigramProbability);
|
|
|
|
words.add(word);
|
|
|
|
unigramProbabilities.put(word, unigramProbability);
|
|
|
|
if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < SHORTCUT_COUNT; i++) {
|
|
|
|
final String shortcutTarget = CodePointUtils.generateWord(random, codePointSet);
|
|
|
|
final int shortcutProbability = random.nextInt(0xF);
|
|
|
|
final String word = words.get(random.nextInt(words.size()));
|
|
|
|
final int unigramProbability = unigramProbabilities.get(word);
|
|
|
|
binaryDictionary.addUnigramWord(word, unigramProbability, shortcutTarget,
|
|
|
|
shortcutProbability, false /* isNotAWord */, false /* isBlacklisted */,
|
|
|
|
0 /* timestamp */);
|
|
|
|
if (shortcutTargets.containsKey(word)) {
|
|
|
|
final HashMap<String, Integer> shortcutTargetsOfWord = shortcutTargets.get(word);
|
|
|
|
shortcutTargetsOfWord.put(shortcutTarget, shortcutProbability);
|
|
|
|
} else {
|
|
|
|
final HashMap<String, Integer> shortcutTargetsOfWord =
|
|
|
|
new HashMap<String, Integer>();
|
|
|
|
shortcutTargetsOfWord.put(shortcutTarget, shortcutProbability);
|
|
|
|
shortcutTargets.put(word, shortcutTargetsOfWord);
|
|
|
|
}
|
|
|
|
if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final String word : words) {
|
2014-01-31 11:32:44 +00:00
|
|
|
final WordProperty wordProperty = binaryDictionary.getWordProperty(word);
|
|
|
|
assertEquals((int)unigramProbabilities.get(word),
|
|
|
|
wordProperty.mProbabilityInfo.mProbability);
|
2013-12-26 08:01:48 +00:00
|
|
|
if (!shortcutTargets.containsKey(word)) {
|
|
|
|
// The word does not have shortcut targets.
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-31 11:32:44 +00:00
|
|
|
assertEquals(shortcutTargets.get(word).size(), wordProperty.mShortcutTargets.size());
|
|
|
|
for (final WeightedString shortcutTarget : wordProperty.mShortcutTargets) {
|
2013-12-13 08:09:16 +00:00
|
|
|
final String targetCodePonts = shortcutTarget.mWord;
|
|
|
|
assertEquals((int)shortcutTargets.get(word).get(targetCodePonts),
|
2014-02-05 12:44:55 +00:00
|
|
|
shortcutTarget.getProbability());
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 10:16:33 +00:00
|
|
|
}
|