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
|
|
|
|
2014-09-29 01:52:18 +00:00
|
|
|
import com.android.inputmethod.latin.NgramContext.WordInfo;
|
2014-10-22 09:24:44 +00:00
|
|
|
import com.android.inputmethod.latin.common.CodePointUtils;
|
2014-10-23 09:37:32 +00:00
|
|
|
import com.android.inputmethod.latin.common.Constants;
|
2014-11-06 11:29:29 +00:00
|
|
|
import com.android.inputmethod.latin.common.FileUtils;
|
2014-10-20 06:01:49 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.DictionaryHeader;
|
2013-09-10 10:16:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2014-03-06 09:53:06 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.WeightedString;
|
2014-02-06 06:13:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.WordProperty;
|
2014-03-05 09:19:34 +00:00
|
|
|
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
|
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-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";
|
2014-05-14 11:37:19 +00:00
|
|
|
private static final int[] DICT_FORMAT_VERSIONS =
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
new int[] { FormatSpec.VERSION402, FormatSpec.VERSION403, FormatSpec.VERSION4_DEV };
|
2014-10-03 10:59:15 +00:00
|
|
|
private static final String DICTIONARY_ID = "TestBinaryDictionary";
|
2013-09-10 10:16:33 +00:00
|
|
|
|
2014-09-16 11:43:00 +00:00
|
|
|
private static boolean supportsNgram(final int formatVersion) {
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
return formatVersion >= FormatSpec.VERSION403;
|
2014-09-16 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 10:59:15 +00:00
|
|
|
private HashSet<File> mDictFilesToBeDeleted = new HashSet<>();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
mDictFilesToBeDeleted.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void tearDown() throws Exception {
|
|
|
|
for (final File dictFile : mDictFilesToBeDeleted) {
|
|
|
|
dictFile.delete();
|
|
|
|
}
|
|
|
|
mDictFilesToBeDeleted.clear();
|
|
|
|
super.tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
private File createEmptyDictionaryAndGetFile(final int formatVersion) {
|
2014-10-20 06:01:49 +00:00
|
|
|
return createEmptyDictionaryWithAttributesAndGetFile(formatVersion,
|
|
|
|
new HashMap<String, String>());
|
|
|
|
}
|
|
|
|
|
|
|
|
private File createEmptyDictionaryWithAttributesAndGetFile(final int formatVersion,
|
|
|
|
final HashMap<String, String> attributeMap) {
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
try {
|
|
|
|
final File dictFile = createEmptyVer4DictionaryAndGetFile(formatVersion,
|
|
|
|
attributeMap);
|
|
|
|
mDictFilesToBeDeleted.add(dictFile);
|
|
|
|
return dictFile;
|
|
|
|
} catch (final IOException e) {
|
|
|
|
fail(e.toString());
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
2014-10-03 10:59:15 +00:00
|
|
|
return null;
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 06:01:49 +00:00
|
|
|
private File createEmptyVer4DictionaryAndGetFile(final int formatVersion,
|
|
|
|
final HashMap<String, String> attributeMap) throws IOException {
|
2014-10-03 10:59:15 +00:00
|
|
|
final File file = File.createTempFile(DICTIONARY_ID, 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();
|
2014-05-09 06:17:01 +00:00
|
|
|
if (BinaryDictionaryUtils.createEmptyDictFile(file.getAbsolutePath(), formatVersion,
|
2014-02-04 14:51:05 +00:00
|
|
|
Locale.ENGLISH, attributeMap)) {
|
2013-09-26 03:59:02 +00:00
|
|
|
return file;
|
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
throw new IOException("Empty dictionary " + file.getAbsolutePath()
|
|
|
|
+ " cannot be created. Format version: " + formatVersion);
|
2013-09-10 10:16:33 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 05:48:56 +00:00
|
|
|
private static BinaryDictionary getBinaryDictionary(final File dictFile) {
|
2014-10-03 10:59:15 +00:00
|
|
|
return new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
}
|
|
|
|
|
|
|
|
private BinaryDictionary getEmptyBinaryDictionary(final int formatVersion) {
|
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
|
|
|
return new BinaryDictionary(dictFile.getAbsolutePath(),
|
|
|
|
0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
|
|
|
|
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
|
|
|
|
}
|
|
|
|
|
2013-09-10 10:16:33 +00:00
|
|
|
public void testIsValidDictionary() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testIsValidDictionary(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testIsValidDictionary(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
|
|
|
BinaryDictionary binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-10 10:16:33 +00:00
|
|
|
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);
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-10 10:16:33 +00:00
|
|
|
assertFalse("binaryDictionary must be invalid for not existing dictionary file.",
|
|
|
|
binaryDictionary.isValidDictionary());
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
2013-09-10 13:46:10 +00:00
|
|
|
|
2014-04-22 20:18:34 +00:00
|
|
|
public void testConstructingDictionaryOnMemory() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testConstructingDictionaryOnMemory(formatVersion);
|
|
|
|
}
|
2014-04-22 20:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testConstructingDictionaryOnMemory(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
2014-04-22 20:18:34 +00:00
|
|
|
FileUtils.deleteRecursively(dictFile);
|
|
|
|
assertFalse(dictFile.exists());
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
|
2014-04-22 20:18:34 +00:00
|
|
|
true /* useFullEditDistance */, Locale.getDefault(), TEST_LOCALE, formatVersion,
|
|
|
|
new HashMap<String, String>());
|
|
|
|
assertTrue(binaryDictionary.isValidDictionary());
|
|
|
|
assertEquals(formatVersion, binaryDictionary.getFormatVersion());
|
|
|
|
final int probability = 100;
|
|
|
|
addUnigramWord(binaryDictionary, "word", probability);
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("word"));
|
|
|
|
assertFalse(dictFile.exists());
|
|
|
|
binaryDictionary.flush();
|
|
|
|
assertTrue(dictFile.exists());
|
|
|
|
assertTrue(binaryDictionary.isValidDictionary());
|
|
|
|
assertEquals(formatVersion, binaryDictionary.getFormatVersion());
|
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("word"));
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
|
|
|
|
2014-01-25 00:32:42 +00:00
|
|
|
public void testAddTooLongWord() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testAddTooLongWord(formatVersion);
|
|
|
|
}
|
2014-01-25 00:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddTooLongWord(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2014-01-25 00:32:42 +00:00
|
|
|
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.
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry("a", probability, invalidLongWord,
|
2014-05-23 10:58:58 +00:00
|
|
|
10 /* shortcutProbability */, false /* isBeginningOfSentence */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isNotAWord */, false /* isPossiblyOffensive */,
|
2014-01-25 00:32:42 +00:00
|
|
|
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));
|
2014-10-20 05:48:56 +00:00
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY, binaryDictionary.getFrequency(invalidLongWord));
|
2014-01-25 00:32:42 +00:00
|
|
|
assertEquals(updatedProbability, binaryDictionary.getFrequency("abc"));
|
|
|
|
}
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
private static void addUnigramWord(final BinaryDictionary binaryDictionary, final String word,
|
2013-12-13 08:09:16 +00:00
|
|
|
final int probability) {
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry(word, probability, "" /* shortcutTarget */,
|
2014-10-20 05:48:56 +00:00
|
|
|
Dictionary.NOT_A_PROBABILITY /* shortcutProbability */,
|
2014-05-23 10:58:58 +00:00
|
|
|
false /* isBeginningOfSentence */, false /* isNotAWord */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isPossiblyOffensive */,
|
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
private static void addBigramWords(final BinaryDictionary binaryDictionary, final String word0,
|
2013-12-13 08:09:16 +00:00
|
|
|
final String word1, final int probability) {
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.addNgramEntry(new NgramContext(new WordInfo(word0)), word1, probability,
|
2013-12-13 08:09:16 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
|
|
|
|
}
|
|
|
|
|
2014-09-16 11:43:00 +00:00
|
|
|
private static void addTrigramEntry(final BinaryDictionary binaryDictionary, final String word0,
|
|
|
|
final String word1, final String word2, final int probability) {
|
2014-09-29 01:52:18 +00:00
|
|
|
final NgramContext ngramContext =
|
|
|
|
new NgramContext(new WordInfo[] { new WordInfo(word1), new WordInfo(word0) } );
|
|
|
|
binaryDictionary.addNgramEntry(ngramContext, word2, probability,
|
2014-09-16 11:43:00 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
|
|
|
|
}
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
private static boolean isValidBigram(final BinaryDictionary binaryDictionary,
|
|
|
|
final String word0, final String word1) {
|
2014-09-29 01:52:18 +00:00
|
|
|
return binaryDictionary.isValidNgram(new NgramContext(new WordInfo(word0)), word1);
|
2014-05-21 02:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void removeBigramEntry(final BinaryDictionary binaryDictionary,
|
|
|
|
final String word0, final String word1) {
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.removeNgramEntry(new NgramContext(new WordInfo(word0)), word1);
|
2014-05-21 02:15:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 11:43:00 +00:00
|
|
|
private static void removeTrigramEntry(final BinaryDictionary binaryDictionary,
|
|
|
|
final String word0, final String word1, final String word2) {
|
2014-09-29 01:52:18 +00:00
|
|
|
final NgramContext ngramContext =
|
|
|
|
new NgramContext(new WordInfo[] { new WordInfo(word1), new WordInfo(word0) } );
|
|
|
|
binaryDictionary.removeNgramEntry(ngramContext, word2);
|
2014-09-16 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
private static int getBigramProbability(final BinaryDictionary binaryDictionary,
|
|
|
|
final String word0, final String word1) {
|
2014-09-29 01:52:18 +00:00
|
|
|
return binaryDictionary.getNgramProbability(new NgramContext(new WordInfo(word0)), word1);
|
2014-05-21 02:15:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 11:43:00 +00:00
|
|
|
private static int getTrigramProbability(final BinaryDictionary binaryDictionary,
|
|
|
|
final String word0, final String word1, final String word2) {
|
2014-09-29 01:52:18 +00:00
|
|
|
final NgramContext ngramContext =
|
|
|
|
new NgramContext(new WordInfo[] { new WordInfo(word1), new WordInfo(word0) } );
|
|
|
|
return binaryDictionary.getNgramProbability(ngramContext, word2);
|
2014-09-16 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 13:46:10 +00:00
|
|
|
public void testAddUnigramWord() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testAddUnigramWord(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddUnigramWord(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2013-09-10 13:46:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public void testRandomlyAddUnigramWord() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testRandomlyAddUnigramWord(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2013-09-17 06:11:24 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashMap<String, Integer> probabilityMap = new HashMap<>();
|
2013-09-17 06:11:24 +00:00
|
|
|
// 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));
|
|
|
|
}
|
2013-09-10 13:46:10 +00:00
|
|
|
}
|
2013-09-17 03:52:21 +00:00
|
|
|
|
|
|
|
public void testAddBigramWords() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testAddBigramWords(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddBigramWords(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2013-09-17 03:52:21 +00:00
|
|
|
|
|
|
|
final int unigramProbability = 100;
|
2014-05-15 07:02:58 +00:00
|
|
|
final int bigramProbability = 150;
|
|
|
|
final int updatedBigramProbability = 200;
|
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
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
assertTrue(isValidBigram(binaryDictionary, "aaa", "abb"));
|
|
|
|
assertTrue(isValidBigram(binaryDictionary, "aaa", "bcc"));
|
|
|
|
assertTrue(isValidBigram(binaryDictionary, "abb", "aaa"));
|
|
|
|
assertTrue(isValidBigram(binaryDictionary, "abb", "bcc"));
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "aaa", "abb"));
|
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "aaa", "bcc"));
|
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "abb", "aaa"));
|
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "abb", "bcc"));
|
2013-09-18 02:18:28 +00:00
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, "aaa", "abb", updatedBigramProbability);
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(updatedBigramProbability,
|
|
|
|
getBigramProbability(binaryDictionary, "aaa", "abb"));
|
2013-09-17 03:52:21 +00:00
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
assertFalse(isValidBigram(binaryDictionary, "bcc", "aaa"));
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "bcc", "bbc"));
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "aaa", "aaa"));
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
2014-05-21 02:15:38 +00:00
|
|
|
getBigramProbability(binaryDictionary, "bcc", "aaa"));
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
2014-05-21 02:15:38 +00:00
|
|
|
getBigramProbability(binaryDictionary, "bcc", "bbc"));
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
2014-05-21 02:15:38 +00:00
|
|
|
getBigramProbability(binaryDictionary, "aaa", "aaa"));
|
2013-09-18 02:18:28 +00:00
|
|
|
|
|
|
|
// 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);
|
2014-05-15 07:02:58 +00:00
|
|
|
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "abcde", "fghij"));
|
2013-09-18 02:18:28 +00:00
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
2014-05-21 02:15:38 +00:00
|
|
|
getBigramProbability(binaryDictionary, "abcde", "fgh"));
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, "abcde", "fghij", updatedBigramProbability);
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(updatedBigramProbability,
|
|
|
|
getBigramProbability(binaryDictionary, "abcde", "fghij"));
|
2013-09-17 03:52:21 +00:00
|
|
|
}
|
2013-09-17 06:32:37 +00:00
|
|
|
|
|
|
|
public void testRandomlyAddBigramWords() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testRandomlyAddBigramWords(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2013-10-04 07:48:22 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final ArrayList<Pair<String, String>> bigramWords = new ArrayList<>();
|
2013-09-17 06:32:37 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities = new HashMap<>();
|
2013-10-04 07:48:22 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2014-05-23 11:18:17 +00:00
|
|
|
final Pair<String, String> bigram = new Pair<>(word0, word1);
|
2013-10-04 07:48:22 +00:00
|
|
|
bigramWords.add(bigram);
|
2014-05-15 07:02:58 +00:00
|
|
|
final int unigramProbability = unigramProbabilities.get(word1);
|
|
|
|
final int bigramProbability =
|
|
|
|
unigramProbability + random.nextInt(0xFF - unigramProbability);
|
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 bigramProbability = bigramProbabilities.get(bigram);
|
2014-05-15 07:02:58 +00:00
|
|
|
assertEquals(bigramProbability != Dictionary.NOT_A_PROBABILITY,
|
2014-05-21 02:15:38 +00:00
|
|
|
isValidBigram(binaryDictionary, bigram.first, bigram.second));
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(bigramProbability,
|
|
|
|
getBigramProbability(binaryDictionary, bigram.first, bigram.second));
|
2013-09-17 06:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-17 08:49:22 +00:00
|
|
|
|
|
|
|
public void testRemoveBigramWords() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testRemoveBigramWords(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testRemoveBigramWords(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2013-09-17 08:49:22 +00:00
|
|
|
final int unigramProbability = 100;
|
2014-05-15 07:02:58 +00:00
|
|
|
final int bigramProbability = 150;
|
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
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
assertTrue(isValidBigram(binaryDictionary, "aaa", "abb"));
|
|
|
|
assertTrue(isValidBigram(binaryDictionary, "aaa", "bcc"));
|
|
|
|
assertTrue(isValidBigram(binaryDictionary, "abb", "aaa"));
|
|
|
|
assertTrue(isValidBigram(binaryDictionary, "abb", "bcc"));
|
2013-09-17 08:49:22 +00:00
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
removeBigramEntry(binaryDictionary, "aaa", "abb");
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "aaa", "abb"));
|
2013-12-13 08:09:16 +00:00
|
|
|
addBigramWords(binaryDictionary, "aaa", "abb", bigramProbability);
|
2014-05-21 02:15:38 +00:00
|
|
|
assertTrue(isValidBigram(binaryDictionary, "aaa", "abb"));
|
2013-09-17 08:49:22 +00:00
|
|
|
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
removeBigramEntry(binaryDictionary, "aaa", "bcc");
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "aaa", "bcc"));
|
|
|
|
removeBigramEntry(binaryDictionary, "abb", "aaa");
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "abb", "aaa"));
|
|
|
|
removeBigramEntry(binaryDictionary, "abb", "bcc");
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "abb", "bcc"));
|
2013-09-17 08:49:22 +00:00
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
removeBigramEntry(binaryDictionary, "aaa", "abb");
|
2013-09-17 08:49:22 +00:00
|
|
|
// Test remove non-existing bigram operation.
|
2014-05-21 02:15:38 +00:00
|
|
|
removeBigramEntry(binaryDictionary, "aaa", "abb");
|
|
|
|
removeBigramEntry(binaryDictionary, "bcc", "aaa");
|
2013-09-17 08:49:22 +00:00
|
|
|
}
|
2013-09-18 04:19:14 +00:00
|
|
|
|
2014-09-16 11:43:00 +00:00
|
|
|
public void testAddTrigramWords() {
|
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
if (supportsNgram(formatVersion)) {
|
|
|
|
testAddTrigramWords(formatVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddTrigramWords(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2014-09-16 11:43:00 +00:00
|
|
|
final int unigramProbability = 100;
|
|
|
|
final int trigramProbability = 150;
|
|
|
|
final int updatedTrigramProbability = 200;
|
|
|
|
addUnigramWord(binaryDictionary, "aaa", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "abb", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "bcc", unigramProbability);
|
|
|
|
|
|
|
|
addBigramWords(binaryDictionary, "abb", "bcc", 10);
|
|
|
|
addBigramWords(binaryDictionary, "abb", "aaa", 10);
|
|
|
|
|
|
|
|
addTrigramEntry(binaryDictionary, "aaa", "abb", "bcc", trigramProbability);
|
|
|
|
addTrigramEntry(binaryDictionary, "bcc", "abb", "aaa", trigramProbability);
|
|
|
|
|
|
|
|
assertEquals(trigramProbability,
|
|
|
|
getTrigramProbability(binaryDictionary, "aaa", "abb", "bcc"));
|
|
|
|
assertEquals(trigramProbability,
|
|
|
|
getTrigramProbability(binaryDictionary, "bcc", "abb", "aaa"));
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "aaa", "abb"));
|
|
|
|
|
|
|
|
addTrigramEntry(binaryDictionary, "bcc", "abb", "aaa", updatedTrigramProbability);
|
|
|
|
assertEquals(updatedTrigramProbability,
|
|
|
|
getTrigramProbability(binaryDictionary, "bcc", "abb", "aaa"));
|
|
|
|
|
|
|
|
removeTrigramEntry(binaryDictionary, "aaa", "abb", "bcc");
|
|
|
|
assertEquals(Dictionary.NOT_A_PROBABILITY,
|
|
|
|
getTrigramProbability(binaryDictionary, "aaa", "abb", "bcc"));
|
|
|
|
assertTrue(isValidBigram(binaryDictionary, "abb", "bcc"));
|
|
|
|
}
|
|
|
|
|
2013-09-18 04:19:14 +00:00
|
|
|
public void testFlushDictionary() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testFlushDictionary(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testFlushDictionary(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
|
|
|
BinaryDictionary binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-18 04:19:14 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-18 04:19:14 +00:00
|
|
|
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();
|
|
|
|
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-18 04:19:14 +00:00
|
|
|
assertEquals(probability, binaryDictionary.getFrequency("bcde"));
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
2013-09-24 06:29:56 +00:00
|
|
|
|
|
|
|
public void testFlushWithGCDictionary() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testFlushWithGCDictionary(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testFlushWithGCDictionary(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
|
|
|
BinaryDictionary binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-24 06:29:56 +00:00
|
|
|
final int unigramProbability = 100;
|
2014-05-15 07:02:58 +00:00
|
|
|
final int bigramProbability = 150;
|
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();
|
|
|
|
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-24 06:29:56 +00:00
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("aaa"));
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("abb"));
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("bcc"));
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "aaa", "abb"));
|
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "aaa", "bcc"));
|
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "abb", "aaa"));
|
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "abb", "bcc"));
|
2014-05-21 02:15:38 +00:00
|
|
|
assertFalse(isValidBigram(binaryDictionary, "bcc", "aaa"));
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "bcc", "bbc"));
|
|
|
|
assertFalse(isValidBigram(binaryDictionary, "aaa", "aaa"));
|
2013-09-24 06:29:56 +00:00
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
2013-09-24 07:32:25 +00:00
|
|
|
|
|
|
|
public void testAddBigramWordsAndFlashWithGC() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testAddBigramWordsAndFlashWithGC(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-10-03 10:59:15 +00:00
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
|
|
|
BinaryDictionary binaryDictionary = getBinaryDictionary(dictFile);
|
2013-10-04 07:48:22 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final ArrayList<Pair<String, String>> bigramWords = new ArrayList<>();
|
2013-09-24 07:32:25 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities = new HashMap<>();
|
2013-10-04 07:48:22 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2014-05-23 11:18:17 +00:00
|
|
|
final Pair<String, String> bigram = new Pair<>(word0, word1);
|
2013-10-04 07:48:22 +00:00
|
|
|
bigramWords.add(bigram);
|
2014-05-15 07:02:58 +00:00
|
|
|
final int unigramProbability = unigramProbabilities.get(word1);
|
|
|
|
final int bigramProbability =
|
|
|
|
unigramProbability + random.nextInt(0xFF - unigramProbability);
|
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();
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
2014-05-15 07:02:58 +00:00
|
|
|
|
2013-10-04 07:48:22 +00:00
|
|
|
for (final Pair<String, String> bigram : bigramWords) {
|
|
|
|
final int bigramProbability = bigramProbabilities.get(bigram);
|
2014-05-15 07:02:58 +00:00
|
|
|
assertEquals(bigramProbability != Dictionary.NOT_A_PROBABILITY,
|
2014-05-21 02:15:38 +00:00
|
|
|
isValidBigram(binaryDictionary, bigram.first, bigram.second));
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(bigramProbability,
|
|
|
|
getBigramProbability(binaryDictionary, bigram.first, bigram.second));
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public void testRandomOperationsAndFlashWithGC() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testRandomOperationsAndFlashWithGC(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testRandomOperationsAndFlashWithGC(final int formatVersion) {
|
2014-10-20 06:01:49 +00:00
|
|
|
final int maxUnigramCount = 5000;
|
|
|
|
final int maxBigramCount = 10000;
|
|
|
|
final HashMap<String, String> attributeMap = new HashMap<>();
|
|
|
|
attributeMap.put(DictionaryHeader.MAX_UNIGRAM_COUNT_KEY, String.valueOf(maxUnigramCount));
|
|
|
|
attributeMap.put(DictionaryHeader.MAX_BIGRAM_COUNT_KEY, String.valueOf(maxBigramCount));
|
|
|
|
|
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);
|
2014-10-20 06:01:49 +00:00
|
|
|
final File dictFile = createEmptyDictionaryWithAttributesAndGetFile(formatVersion,
|
|
|
|
attributeMap);
|
2014-10-03 10:59:15 +00:00
|
|
|
BinaryDictionary binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-24 07:32:25 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final ArrayList<Pair<String, String>> bigramWords = new ArrayList<>();
|
2013-09-24 07:32:25 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities = new HashMap<>();
|
2013-09-24 07:32:25 +00:00
|
|
|
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++) {
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-24 07:32:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-05-15 07:02:58 +00:00
|
|
|
final int unigramProbability = unigramProbabilities.get(word1);
|
|
|
|
final int bigramProbability =
|
|
|
|
unigramProbability + random.nextInt(0xFF - unigramProbability);
|
2014-05-23 11:18:17 +00:00
|
|
|
final Pair<String, String> bigram = new Pair<>(word0, word1);
|
2013-09-24 07:32:25 +00:00
|
|
|
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);
|
2014-05-21 02:15:38 +00:00
|
|
|
removeBigramEntry(binaryDictionary, bigram.first, bigram.second);
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 probability;
|
|
|
|
if (bigramProbabilities.containsKey(bigram)) {
|
|
|
|
final int bigramProbability = bigramProbabilities.get(bigram);
|
2014-05-15 07:02:58 +00:00
|
|
|
probability = bigramProbability;
|
2013-09-24 07:32:25 +00:00
|
|
|
} else {
|
|
|
|
probability = Dictionary.NOT_A_PROBABILITY;
|
|
|
|
}
|
2014-05-15 07:02:58 +00:00
|
|
|
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(probability,
|
|
|
|
getBigramProbability(binaryDictionary, bigram.first, bigram.second));
|
2014-05-15 07:02:58 +00:00
|
|
|
assertEquals(probability != Dictionary.NOT_A_PROBABILITY,
|
2014-05-21 02:15:38 +00:00
|
|
|
isValidBigram(binaryDictionary, bigram.first, bigram.second));
|
2013-09-24 07:32:25 +00:00
|
|
|
}
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 10:21:17 +00:00
|
|
|
|
|
|
|
public void testAddManyUnigramsAndFlushWithGC() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testAddManyUnigramsAndFlushWithGC(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-10-03 10:59:15 +00:00
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
2013-09-24 10:21:17 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<>();
|
2013-09-24 10:21:17 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
|
|
|
|
BinaryDictionary binaryDictionary;
|
|
|
|
for (int i = 0; i < flashWithGCIterationCount; i++) {
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
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();
|
|
|
|
}
|
|
|
|
}
|
2013-09-27 14:12:12 +00:00
|
|
|
|
|
|
|
public void testUnigramAndBigramCount() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testUnigramAndBigramCount(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testUnigramAndBigramCount(final int formatVersion) {
|
2014-10-20 06:01:49 +00:00
|
|
|
final int maxUnigramCount = 5000;
|
|
|
|
final int maxBigramCount = 10000;
|
|
|
|
final HashMap<String, String> attributeMap = new HashMap<>();
|
|
|
|
attributeMap.put(DictionaryHeader.MAX_UNIGRAM_COUNT_KEY, String.valueOf(maxUnigramCount));
|
|
|
|
attributeMap.put(DictionaryHeader.MAX_BIGRAM_COUNT_KEY, String.valueOf(maxBigramCount));
|
|
|
|
|
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);
|
2014-10-20 06:01:49 +00:00
|
|
|
final File dictFile = createEmptyDictionaryWithAttributesAndGetFile(formatVersion,
|
|
|
|
attributeMap);
|
2013-09-27 14:12:12 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final HashSet<Pair<String, String>> bigrams = new HashSet<>();
|
2013-09-27 14:12:12 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
|
|
|
|
|
|
|
BinaryDictionary binaryDictionary;
|
|
|
|
for (int i = 0; i < flashWithGCIterationCount; i++) {
|
2014-10-03 10:59:15 +00:00
|
|
|
binaryDictionary = getBinaryDictionary(dictFile);
|
2013-09-27 14:12:12 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-05-23 11:18:17 +00:00
|
|
|
bigrams.add(new Pair<>(word0, word1));
|
2013-09-27 14:12:12 +00:00
|
|
|
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
|
|
|
}
|
2014-05-23 11:18:17 +00:00
|
|
|
assertEquals(new HashSet<>(words).size(), Integer.parseInt(
|
2014-09-19 11:58:07 +00:00
|
|
|
binaryDictionary.getPropertyForGettingStats(
|
|
|
|
BinaryDictionary.UNIGRAM_COUNT_QUERY)));
|
2014-05-23 11:18:17 +00:00
|
|
|
assertEquals(new HashSet<>(bigrams).size(), Integer.parseInt(
|
2014-09-19 11:58:07 +00:00
|
|
|
binaryDictionary.getPropertyForGettingStats(
|
|
|
|
BinaryDictionary.BIGRAM_COUNT_QUERY)));
|
2013-09-27 14:12:12 +00:00
|
|
|
binaryDictionary.flushWithGC();
|
2014-05-23 11:18:17 +00:00
|
|
|
assertEquals(new HashSet<>(words).size(), Integer.parseInt(
|
2014-09-19 11:58:07 +00:00
|
|
|
binaryDictionary.getPropertyForGettingStats(
|
|
|
|
BinaryDictionary.UNIGRAM_COUNT_QUERY)));
|
2014-05-23 11:18:17 +00:00
|
|
|
assertEquals(new HashSet<>(bigrams).size(), Integer.parseInt(
|
2014-09-19 11:58:07 +00:00
|
|
|
binaryDictionary.getPropertyForGettingStats(
|
|
|
|
BinaryDictionary.BIGRAM_COUNT_QUERY)));
|
2013-09-27 14:12:12 +00:00
|
|
|
binaryDictionary.close();
|
|
|
|
}
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
|
2014-01-31 11:32:44 +00:00
|
|
|
public void testGetWordProperties() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testGetWordProperties(formatVersion);
|
|
|
|
}
|
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);
|
2014-10-03 10:59:15 +00:00
|
|
|
final File dictFile = createEmptyDictionaryAndGetFile(formatVersion);
|
|
|
|
final BinaryDictionary binaryDictionary = getBinaryDictionary(dictFile);
|
2013-12-13 08:09:16 +00:00
|
|
|
|
2014-06-24 03:37:07 +00:00
|
|
|
final WordProperty invalidWordProperty = binaryDictionary.getWordProperty("dummyWord",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-01-31 11:32:44 +00:00
|
|
|
assertFalse(invalidWordProperty.isValid());
|
2013-12-13 08:09:16 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final HashMap<String, Integer> wordProbabilities = new HashMap<>();
|
|
|
|
final HashMap<String, HashSet<String>> bigrams = new HashMap<>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities = new HashMap<>();
|
2014-02-03 02:47:26 +00:00
|
|
|
|
|
|
|
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();
|
2014-10-14 03:13:11 +00:00
|
|
|
final boolean isPossiblyOffensive = random.nextBoolean();
|
2013-12-13 08:09:16 +00:00
|
|
|
// TODO: Add tests for historical info.
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry(word, unigramProbability,
|
2014-10-20 05:48:56 +00:00
|
|
|
null /* shortcutTarget */, Dictionary.NOT_A_PROBABILITY,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isBeginningOfSentence */, isNotAWord, isPossiblyOffensive,
|
2014-05-23 10:58:58 +00:00
|
|
|
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);
|
2014-06-24 03:37:07 +00:00
|
|
|
final WordProperty wordProperty = binaryDictionary.getWordProperty(word,
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-02-06 06:13:33 +00:00
|
|
|
assertEquals(word, wordProperty.mWord);
|
|
|
|
assertTrue(wordProperty.isValid());
|
|
|
|
assertEquals(isNotAWord, wordProperty.mIsNotAWord);
|
2014-10-14 03:13:11 +00:00
|
|
|
assertEquals(isPossiblyOffensive, wordProperty.mIsPossiblyOffensive);
|
2014-10-01 02:21:08 +00:00
|
|
|
assertEquals(false, wordProperty.mHasNgrams);
|
2014-02-06 06:13:33 +00:00
|
|
|
assertEquals(false, wordProperty.mHasShortcuts);
|
|
|
|
assertEquals(unigramProbability, wordProperty.mProbabilityInfo.mProbability);
|
|
|
|
assertTrue(wordProperty.mShortcutTargets.isEmpty());
|
2014-02-03 02:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-05-15 07:02:58 +00:00
|
|
|
final int unigramProbability = wordProbabilities.get(word1);
|
|
|
|
final int bigramProbability =
|
|
|
|
unigramProbability + random.nextInt(0xFF - unigramProbability);
|
2014-05-21 02:15:38 +00:00
|
|
|
addBigramWords(binaryDictionary, word0, word1, bigramProbability);
|
2014-02-03 02:47:26 +00:00
|
|
|
if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
if (!bigrams.containsKey(word0)) {
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashSet<String> bigramWord1s = new HashSet<>();
|
2014-02-03 02:47:26 +00:00
|
|
|
bigrams.put(word0, bigramWord1s);
|
|
|
|
}
|
|
|
|
bigrams.get(word0).add(word1);
|
2014-05-23 11:18:17 +00:00
|
|
|
bigramProbabilities.put(new Pair<>(word0, word1), bigramProbability);
|
2014-02-03 02:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-06-24 03:37:07 +00:00
|
|
|
final WordProperty wordProperty = binaryDictionary.getWordProperty(word0,
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-10-01 02:21:08 +00:00
|
|
|
assertEquals(bigramWord1s.size(), wordProperty.mNgrams.size());
|
|
|
|
// TODO: Support ngram.
|
|
|
|
for (final WeightedString bigramTarget : wordProperty.getBigrams()) {
|
|
|
|
final String word1 = bigramTarget.mWord;
|
2014-02-03 02:47:26 +00:00
|
|
|
assertTrue(bigramWord1s.contains(word1));
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
final int bigramProbability = bigramProbabilities.get(new Pair<>(word0, word1));
|
|
|
|
assertEquals(bigramProbability, bigramTarget.getProbability());
|
2014-02-03 02:47:26 +00:00
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 13:22:35 +00:00
|
|
|
public void testIterateAllWords() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testIterateAllWords(formatVersion);
|
|
|
|
}
|
2014-02-03 13:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2014-02-03 13:22:35 +00:00
|
|
|
|
2014-06-24 03:37:07 +00:00
|
|
|
final WordProperty invalidWordProperty = binaryDictionary.getWordProperty("dummyWord",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-02-03 13:22:35 +00:00
|
|
|
assertFalse(invalidWordProperty.isValid());
|
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final HashMap<String, Integer> wordProbabilitiesToCheckLater = new HashMap<>();
|
|
|
|
final HashMap<String, HashSet<String>> bigrams = new HashMap<>();
|
2014-02-03 13:22:35 +00:00
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilitiesToCheckLater =
|
2014-05-23 11:18:17 +00:00
|
|
|
new HashMap<>();
|
2014-02-03 13:22:35 +00:00
|
|
|
|
|
|
|
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);
|
2014-05-15 07:02:58 +00:00
|
|
|
final int unigramProbability = wordProbabilitiesToCheckLater.get(word1);
|
|
|
|
final int bigramProbability =
|
|
|
|
unigramProbability + random.nextInt(0xFF - unigramProbability);
|
2014-05-21 02:15:38 +00:00
|
|
|
addBigramWords(binaryDictionary, word0, word1, bigramProbability);
|
2014-02-03 13:22:35 +00:00
|
|
|
if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
if (!bigrams.containsKey(word0)) {
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashSet<String> bigramWord1s = new HashSet<>();
|
2014-02-03 13:22:35 +00:00
|
|
|
bigrams.put(word0, bigramWord1s);
|
|
|
|
}
|
|
|
|
bigrams.get(word0).add(word1);
|
2014-05-23 11:18:17 +00:00
|
|
|
bigramProbabilitiesToCheckLater.put(new Pair<>(word0, word1), bigramProbability);
|
2014-02-03 13:22:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashSet<String> wordSet = new HashSet<>(words);
|
2014-02-03 13:22:35 +00:00
|
|
|
final HashSet<Pair<String, String>> bigramSet =
|
2014-05-23 11:18:17 +00:00
|
|
|
new HashSet<>(bigramProbabilitiesToCheckLater.keySet());
|
2014-02-03 13:22:35 +00:00
|
|
|
int token = 0;
|
|
|
|
do {
|
|
|
|
final BinaryDictionary.GetNextWordPropertyResult result =
|
|
|
|
binaryDictionary.getNextWordProperty(token);
|
|
|
|
final WordProperty wordProperty = result.mWordProperty;
|
2014-02-06 06:13:33 +00:00
|
|
|
final String word0 = wordProperty.mWord;
|
2014-02-03 13:22:35 +00:00
|
|
|
assertEquals((int)wordProbabilitiesToCheckLater.get(word0),
|
|
|
|
wordProperty.mProbabilityInfo.mProbability);
|
|
|
|
wordSet.remove(word0);
|
|
|
|
final HashSet<String> bigramWord1s = bigrams.get(word0);
|
2014-10-01 02:21:08 +00:00
|
|
|
// TODO: Support ngram.
|
2014-10-03 10:53:23 +00:00
|
|
|
if (wordProperty.mHasNgrams) {
|
|
|
|
for (final WeightedString bigramTarget : wordProperty.getBigrams()) {
|
|
|
|
final String word1 = bigramTarget.mWord;
|
|
|
|
assertTrue(bigramWord1s.contains(word1));
|
|
|
|
final Pair<String, String> bigram = new Pair<>(word0, word1);
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
final int bigramProbability = bigramProbabilitiesToCheckLater.get(bigram);
|
|
|
|
assertEquals(bigramProbability, bigramTarget.getProbability());
|
2014-10-03 10:53:23 +00:00
|
|
|
bigramSet.remove(bigram);
|
2014-05-15 07:02:58 +00:00
|
|
|
}
|
2014-02-03 13:22:35 +00:00
|
|
|
}
|
|
|
|
token = result.mNextToken;
|
|
|
|
} while (token != 0);
|
|
|
|
assertTrue(wordSet.isEmpty());
|
|
|
|
assertTrue(bigramSet.isEmpty());
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public void testAddShortcuts() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testAddShortcuts(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testAddShortcuts(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2013-12-13 08:09:16 +00:00
|
|
|
|
|
|
|
final int unigramProbability = 100;
|
|
|
|
final int shortcutProbability = 10;
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry("aaa", unigramProbability, "zzz",
|
2014-05-23 10:58:58 +00:00
|
|
|
shortcutProbability, false /* isBeginningOfSentence */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isNotAWord */, false /* isPossiblyOffensive */, 0 /* timestamp */);
|
2014-06-24 03:37:07 +00:00
|
|
|
WordProperty wordProperty = binaryDictionary.getWordProperty("aaa",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-01-31 11:32:44 +00:00
|
|
|
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;
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry("aaa", unigramProbability, "zzz",
|
2014-05-23 10:58:58 +00:00
|
|
|
updatedShortcutProbability, false /* isBeginningOfSentence */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isNotAWord */, false /* isPossiblyOffensive */, 0 /* timestamp */);
|
2014-06-24 03:37:07 +00:00
|
|
|
wordProperty = binaryDictionary.getWordProperty("aaa",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-01-31 11:32:44 +00:00
|
|
|
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());
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry("aaa", unigramProbability, "yyy",
|
2014-05-23 10:58:58 +00:00
|
|
|
shortcutProbability, false /* isBeginningOfSentence */, false /* isNotAWord */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isPossiblyOffensive */, 0 /* timestamp */);
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashMap<String, Integer> shortcutTargets = new HashMap<>();
|
2013-12-13 08:09:16 +00:00
|
|
|
shortcutTargets.put("zzz", updatedShortcutProbability);
|
|
|
|
shortcutTargets.put("yyy", shortcutProbability);
|
2014-06-24 03:37:07 +00:00
|
|
|
wordProperty = binaryDictionary.getWordProperty("aaa",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-01-31 11:32:44 +00:00
|
|
|
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-06-24 03:37:07 +00:00
|
|
|
wordProperty = binaryDictionary.getWordProperty("aaa",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-01-31 11:32:44 +00:00
|
|
|
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() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testAddManyShortcuts(formatVersion);
|
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<>();
|
|
|
|
final HashMap<String, HashMap<String, Integer>> shortcutTargets = new HashMap<>();
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2013-12-13 08:09:16 +00:00
|
|
|
|
|
|
|
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);
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry(word, unigramProbability, shortcutTarget,
|
2014-05-23 10:58:58 +00:00
|
|
|
shortcutProbability, false /* isBeginningOfSentence */, false /* isNotAWord */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isPossiblyOffensive */, 0 /* timestamp */);
|
2013-12-13 08:09:16 +00:00
|
|
|
if (shortcutTargets.containsKey(word)) {
|
|
|
|
final HashMap<String, Integer> shortcutTargetsOfWord = shortcutTargets.get(word);
|
|
|
|
shortcutTargetsOfWord.put(shortcutTarget, shortcutProbability);
|
|
|
|
} else {
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashMap<String, Integer> shortcutTargetsOfWord = new HashMap<>();
|
2013-12-13 08:09:16 +00:00
|
|
|
shortcutTargetsOfWord.put(shortcutTarget, shortcutProbability);
|
|
|
|
shortcutTargets.put(word, shortcutTargetsOfWord);
|
|
|
|
}
|
|
|
|
if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final String word : words) {
|
2014-06-24 03:37:07 +00:00
|
|
|
final WordProperty wordProperty = binaryDictionary.getWordProperty(word,
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-01-31 11:32:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-09 06:17:01 +00:00
|
|
|
|
2014-10-14 03:13:11 +00:00
|
|
|
public void testPossiblyOffensiveAttributeMaintained() {
|
|
|
|
final BinaryDictionary binaryDictionary =
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
getEmptyBinaryDictionary(FormatSpec.VERSION403);
|
2014-10-14 03:13:11 +00:00
|
|
|
binaryDictionary.addUnigramEntry("ddd", 100, null, Dictionary.NOT_A_PROBABILITY,
|
|
|
|
false, true, true, 0);
|
|
|
|
WordProperty wordProperty = binaryDictionary.getWordProperty("ddd", false);
|
|
|
|
assertEquals(true, wordProperty.mIsPossiblyOffensive);
|
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
|
2014-05-09 06:17:01 +00:00
|
|
|
public void testDictMigration() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testDictMigration(FormatSpec.VERSION4_ONLY_FOR_TESTING, formatVersion);
|
|
|
|
}
|
2014-05-09 06:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testDictMigration(final int fromFormatVersion, final int toFormatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(fromFormatVersion);
|
2014-05-09 06:17:01 +00:00
|
|
|
final int unigramProbability = 100;
|
|
|
|
addUnigramWord(binaryDictionary, "aaa", unigramProbability);
|
|
|
|
addUnigramWord(binaryDictionary, "bbb", unigramProbability);
|
2014-05-15 07:02:58 +00:00
|
|
|
final int bigramProbability = 150;
|
2014-05-09 06:17:01 +00:00
|
|
|
addBigramWords(binaryDictionary, "aaa", "bbb", bigramProbability);
|
2014-05-13 06:16:30 +00:00
|
|
|
final int shortcutProbability = 10;
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry("ccc", unigramProbability, "xxx", shortcutProbability,
|
2014-05-23 10:58:58 +00:00
|
|
|
false /* isBeginningOfSentence */, false /* isNotAWord */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isPossiblyOffensive */, 0 /* timestamp */);
|
2014-05-21 02:15:38 +00:00
|
|
|
binaryDictionary.addUnigramEntry("ddd", unigramProbability, null /* shortcutTarget */,
|
2014-05-23 10:58:58 +00:00
|
|
|
Dictionary.NOT_A_PROBABILITY, false /* isBeginningOfSentence */,
|
2014-10-14 03:13:11 +00:00
|
|
|
true /* isNotAWord */, true /* isPossiblyOffensive */, 0 /* timestamp */);
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.addNgramEntry(NgramContext.BEGINNING_OF_SENTENCE,
|
2014-06-24 03:37:07 +00:00
|
|
|
"aaa", bigramProbability, 0 /* timestamp */);
|
2014-05-09 06:17:01 +00:00
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("aaa"));
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("bbb"));
|
2014-05-21 02:15:38 +00:00
|
|
|
assertTrue(isValidBigram(binaryDictionary, "aaa", "bbb"));
|
2014-05-09 06:17:01 +00:00
|
|
|
assertEquals(fromFormatVersion, binaryDictionary.getFormatVersion());
|
|
|
|
assertTrue(binaryDictionary.migrateTo(toFormatVersion));
|
|
|
|
assertTrue(binaryDictionary.isValidDictionary());
|
|
|
|
assertEquals(toFormatVersion, binaryDictionary.getFormatVersion());
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("aaa"));
|
|
|
|
assertEquals(unigramProbability, binaryDictionary.getFrequency("bbb"));
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals(bigramProbability, getBigramProbability(binaryDictionary, "aaa", "bbb"));
|
|
|
|
assertEquals(bigramProbability, binaryDictionary.getNgramProbability(
|
|
|
|
NgramContext.BEGINNING_OF_SENTENCE, "aaa"));
|
2014-05-21 02:15:38 +00:00
|
|
|
assertTrue(isValidBigram(binaryDictionary, "aaa", "bbb"));
|
2014-06-24 03:37:07 +00:00
|
|
|
WordProperty wordProperty = binaryDictionary.getWordProperty("ccc",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-05-13 06:16:30 +00:00
|
|
|
assertEquals(1, wordProperty.mShortcutTargets.size());
|
|
|
|
assertEquals("xxx", wordProperty.mShortcutTargets.get(0).mWord);
|
2014-06-24 03:37:07 +00:00
|
|
|
wordProperty = binaryDictionary.getWordProperty("ddd",
|
|
|
|
false /* isBeginningOfSentence */);
|
2014-10-14 03:13:11 +00:00
|
|
|
assertTrue(wordProperty.mIsPossiblyOffensive);
|
2014-05-13 06:16:30 +00:00
|
|
|
assertTrue(wordProperty.mIsNotAWord);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testLargeDictMigration() {
|
2014-05-14 11:37:19 +00:00
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testLargeDictMigration(FormatSpec.VERSION4_ONLY_FOR_TESTING, formatVersion);
|
|
|
|
}
|
2014-05-13 06:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testLargeDictMigration(final int fromFormatVersion, final int toFormatVersion) {
|
|
|
|
final int UNIGRAM_COUNT = 3000;
|
|
|
|
final int BIGRAM_COUNT = 3000;
|
|
|
|
final int codePointSetSize = 50;
|
|
|
|
final long seed = System.currentTimeMillis();
|
|
|
|
final Random random = new Random(seed);
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(fromFormatVersion);
|
2014-05-13 06:16:30 +00:00
|
|
|
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> words = new ArrayList<>();
|
|
|
|
final ArrayList<Pair<String, String>> bigrams = new ArrayList<>();
|
2014-05-13 06:16:30 +00:00
|
|
|
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashMap<String, Integer> unigramProbabilities = new HashMap<>();
|
|
|
|
final HashMap<Pair<String, String>, Integer> bigramProbabilities = new HashMap<>();
|
2014-05-13 06:16:30 +00:00
|
|
|
|
|
|
|
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(true /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
words.add(word);
|
|
|
|
unigramProbabilities.put(word, unigramProbability);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < BIGRAM_COUNT; i++) {
|
|
|
|
final int word0Index = random.nextInt(words.size());
|
|
|
|
final int word1Index = random.nextInt(words.size());
|
|
|
|
if (word0Index == word1Index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String word0 = words.get(word0Index);
|
|
|
|
final String word1 = words.get(word1Index);
|
2014-05-15 07:02:58 +00:00
|
|
|
final int unigramProbability = unigramProbabilities.get(word1);
|
|
|
|
final int bigramProbability =
|
|
|
|
random.nextInt(0xFF - unigramProbability) + unigramProbability;
|
2014-05-21 02:15:38 +00:00
|
|
|
addBigramWords(binaryDictionary, word0, word1, bigramProbability);
|
2014-05-13 06:16:30 +00:00
|
|
|
if (binaryDictionary.needsToRunGC(true /* mindsBlockByGC */)) {
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
}
|
2014-05-23 11:18:17 +00:00
|
|
|
final Pair<String, String> bigram = new Pair<>(word0, word1);
|
2014-05-13 06:16:30 +00:00
|
|
|
bigrams.add(bigram);
|
|
|
|
bigramProbabilities.put(bigram, bigramProbability);
|
|
|
|
}
|
|
|
|
assertTrue(binaryDictionary.migrateTo(toFormatVersion));
|
|
|
|
|
|
|
|
for (final String word : words) {
|
|
|
|
assertEquals((int)unigramProbabilities.get(word), binaryDictionary.getFrequency(word));
|
|
|
|
}
|
|
|
|
assertEquals(unigramProbabilities.size(), Integer.parseInt(
|
2014-09-19 11:58:07 +00:00
|
|
|
binaryDictionary.getPropertyForGettingStats(BinaryDictionary.UNIGRAM_COUNT_QUERY)));
|
2014-05-13 06:16:30 +00:00
|
|
|
|
|
|
|
for (final Pair<String, String> bigram : bigrams) {
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
assertEquals((int)bigramProbabilities.get(bigram),
|
|
|
|
getBigramProbability(binaryDictionary, bigram.first, bigram.second));
|
2014-05-21 02:15:38 +00:00
|
|
|
assertTrue(isValidBigram(binaryDictionary, bigram.first, bigram.second));
|
2014-05-13 06:16:30 +00:00
|
|
|
}
|
|
|
|
assertEquals(bigramProbabilities.size(), Integer.parseInt(
|
2014-09-19 11:58:07 +00:00
|
|
|
binaryDictionary.getPropertyForGettingStats(BinaryDictionary.BIGRAM_COUNT_QUERY)));
|
2014-05-09 06:17:01 +00:00
|
|
|
}
|
2014-05-23 10:58:58 +00:00
|
|
|
|
|
|
|
public void testBeginningOfSentence() {
|
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
Update v4 format version from 402 to 403.
Without personalization:
Total words: 1134774, Success Num: 899230, Success Percentage: 79.243%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1871, Bad Failure Percentage: 0.165%
Failures, with auto-correction (F-C): 29084, F-C Percentage: 2.563%
Max Keystrokes: 6072959, Min Keystrokes: 4436090, Keystroke Saving Percentage:26.953%
Before:
Total words: 1134646, Success Num: 925194, Success Percentage: 81.540%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1316, Bad Failure Percentage: 0.116%
Failures, with auto-correction (F-C): 28288, F-C Percentage: 2.493%
Max Keystrokes: 6072831, Min Keystrokes: 3946188, Keystroke Saving Percentage:35.019%
After
Total words: 1134659, Success Num: 944746, Success Percentage: 83.263%
Bad Failures, with auto-correction (typed word == expected word, output word != expected word): 1258, Bad Failure Percentage: 0.111%
Failures, with auto-correction (F-C): 28016, F-C Percentage: 2.469%
Max Keystrokes: 6072844, Min Keystrokes: 3387333, Keystroke Saving Percentage:44.222%
Change-Id: I3af42ec37a11847c0429c28616e726f6a339247f
2014-10-30 03:54:06 +00:00
|
|
|
testBeginningOfSentence(formatVersion);
|
2014-05-23 10:58:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testBeginningOfSentence(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2014-05-23 10:58:58 +00:00
|
|
|
final int dummyProbability = 0;
|
2014-09-29 01:52:18 +00:00
|
|
|
final NgramContext beginningOfSentenceContext = NgramContext.BEGINNING_OF_SENTENCE;
|
2014-05-23 10:58:58 +00:00
|
|
|
final int bigramProbability = 200;
|
|
|
|
addUnigramWord(binaryDictionary, "aaa", dummyProbability);
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.addNgramEntry(beginningOfSentenceContext, "aaa", bigramProbability,
|
2014-05-23 10:58:58 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
|
|
|
|
assertEquals(bigramProbability,
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.getNgramProbability(beginningOfSentenceContext, "aaa"));
|
|
|
|
binaryDictionary.addNgramEntry(beginningOfSentenceContext, "aaa", bigramProbability,
|
2014-05-23 10:58:58 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
|
|
|
|
addUnigramWord(binaryDictionary, "bbb", dummyProbability);
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.addNgramEntry(beginningOfSentenceContext, "bbb", bigramProbability,
|
2014-05-23 10:58:58 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP /* timestamp */);
|
|
|
|
binaryDictionary.flushWithGC();
|
|
|
|
assertEquals(bigramProbability,
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.getNgramProbability(beginningOfSentenceContext, "aaa"));
|
2014-05-23 10:58:58 +00:00
|
|
|
assertEquals(bigramProbability,
|
2014-09-29 01:52:18 +00:00
|
|
|
binaryDictionary.getNgramProbability(beginningOfSentenceContext, "bbb"));
|
2014-05-23 10:58:58 +00:00
|
|
|
}
|
2014-06-06 08:37:46 +00:00
|
|
|
|
|
|
|
public void testGetMaxFrequencyOfExactMatches() {
|
|
|
|
for (final int formatVersion : DICT_FORMAT_VERSIONS) {
|
|
|
|
testGetMaxFrequencyOfExactMatches(formatVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testGetMaxFrequencyOfExactMatches(final int formatVersion) {
|
2014-10-03 10:59:15 +00:00
|
|
|
final BinaryDictionary binaryDictionary = getEmptyBinaryDictionary(formatVersion);
|
2014-06-06 08:37:46 +00:00
|
|
|
addUnigramWord(binaryDictionary, "abc", 10);
|
|
|
|
addUnigramWord(binaryDictionary, "aBc", 15);
|
|
|
|
assertEquals(15, binaryDictionary.getMaxFrequencyOfExactMatches("abc"));
|
|
|
|
addUnigramWord(binaryDictionary, "ab'c", 20);
|
|
|
|
assertEquals(20, binaryDictionary.getMaxFrequencyOfExactMatches("abc"));
|
|
|
|
addUnigramWord(binaryDictionary, "a-b-c", 25);
|
|
|
|
assertEquals(25, binaryDictionary.getMaxFrequencyOfExactMatches("abc"));
|
|
|
|
addUnigramWord(binaryDictionary, "ab-'-'-'-c", 30);
|
|
|
|
assertEquals(30, binaryDictionary.getMaxFrequencyOfExactMatches("abc"));
|
|
|
|
addUnigramWord(binaryDictionary, "ab c", 255);
|
|
|
|
assertEquals(30, binaryDictionary.getMaxFrequencyOfExactMatches("abc"));
|
|
|
|
}
|
2013-09-10 10:16:33 +00:00
|
|
|
}
|