2013-04-01 06:23:24 +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.makedict;
|
|
|
|
|
2014-03-07 12:58:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions;
|
2013-04-01 06:23:24 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary;
|
2013-08-22 02:07:52 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode;
|
2013-08-16 05:51:37 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray;
|
2014-02-06 06:13:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.WordProperty;
|
2013-04-01 06:23:24 +00:00
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
2013-08-15 10:11:09 +00:00
|
|
|
* Unit tests for FusionDictionary.
|
2013-04-01 06:23:24 +00:00
|
|
|
*/
|
|
|
|
public class FusionDictionaryTest extends TestCase {
|
|
|
|
private static final ArrayList<String> sWords = new ArrayList<String>();
|
|
|
|
private static final int MAX_UNIGRAMS = 1000;
|
|
|
|
|
|
|
|
private void prepare(final long seed) {
|
|
|
|
System.out.println("Seed is " + seed);
|
|
|
|
final Random random = new Random(seed);
|
|
|
|
sWords.clear();
|
|
|
|
generateWords(MAX_UNIGRAMS, random);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a random word.
|
|
|
|
*/
|
|
|
|
private String generateWord(final Random random) {
|
|
|
|
StringBuilder builder = new StringBuilder("a");
|
|
|
|
int count = random.nextInt() % 30;
|
|
|
|
while (count > 0) {
|
|
|
|
final long r = Math.abs(random.nextInt());
|
|
|
|
if (r < 0) continue;
|
|
|
|
// Don't insert 0~20, but insert any other code point.
|
|
|
|
// Code points are in the range 0~0x10FFFF.
|
|
|
|
if (builder.length() < 7)
|
|
|
|
builder.appendCodePoint((int)(20 +r % (0x10FFFF - 20)));
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
if (builder.length() == 1) return generateWord(random);
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void generateWords(final int number, final Random random) {
|
|
|
|
while (sWords.size() < number) {
|
|
|
|
sWords.add(generateWord(random));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDictionary(final FusionDictionary dict, final ArrayList<String> words,
|
|
|
|
int limit) {
|
|
|
|
assertNotNull(dict);
|
|
|
|
for (final String word : words) {
|
|
|
|
if (--limit < 0) return;
|
2013-08-22 02:07:52 +00:00
|
|
|
final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word);
|
|
|
|
assertNotNull(ptNode);
|
2013-04-01 06:23:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String dumpWord(final String word) {
|
|
|
|
final StringBuilder sb = new StringBuilder("");
|
|
|
|
for (int i = 0; i < word.length(); i = word.offsetByCodePoints(i, 1)) {
|
|
|
|
sb.append(word.codePointAt(i));
|
|
|
|
sb.append(" ");
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void dumpDict(final FusionDictionary dict) {
|
2014-02-06 06:13:33 +00:00
|
|
|
for (WordProperty wordProperty : dict) {
|
|
|
|
System.out.println("Word " + dumpWord(wordProperty.mWord));
|
2013-04-01 06:23:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the flattened array contains the expected number of nodes, and
|
|
|
|
// that it does not contain any duplicates.
|
|
|
|
public void testFusion() {
|
2013-08-16 05:51:37 +00:00
|
|
|
final FusionDictionary dict = new FusionDictionary(new PtNodeArray(),
|
2013-12-10 14:08:29 +00:00
|
|
|
new DictionaryOptions(new HashMap<String, String>()));
|
2013-04-01 06:23:24 +00:00
|
|
|
final long time = System.currentTimeMillis();
|
|
|
|
prepare(time);
|
|
|
|
for (int i = 0; i < sWords.size(); ++i) {
|
|
|
|
System.out.println("Adding in pos " + i + " : " + dumpWord(sWords.get(i)));
|
2014-02-10 06:05:08 +00:00
|
|
|
dict.add(sWords.get(i), new ProbabilityInfo(180), null, false);
|
2013-04-01 06:23:24 +00:00
|
|
|
dumpDict(dict);
|
|
|
|
checkDictionary(dict, sWords, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|