2011-04-26 12:49:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2011-04-26 12:49:09 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-26 12:49:09 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2011-04-26 12:49:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2013-06-23 16:11:32 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2011-08-04 03:08:22 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2012-06-12 21:57:40 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2014-07-08 07:36:06 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion;
|
2011-11-09 23:21:42 +00:00
|
|
|
|
2012-06-12 21:57:40 +00:00
|
|
|
import java.util.ArrayList;
|
2011-04-27 14:13:11 +00:00
|
|
|
import java.util.Collection;
|
2011-07-22 05:46:43 +00:00
|
|
|
import java.util.Collections;
|
2011-04-26 12:49:09 +00:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for a collection of dictionaries that behave like one dictionary.
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class DictionaryCollection extends Dictionary {
|
2011-11-09 23:21:42 +00:00
|
|
|
private final String TAG = DictionaryCollection.class.getSimpleName();
|
2012-03-16 15:50:51 +00:00
|
|
|
protected final CopyOnWriteArrayList<Dictionary> mDictionaries;
|
2011-04-26 12:49:09 +00:00
|
|
|
|
2012-06-27 08:31:09 +00:00
|
|
|
public DictionaryCollection(final String dictType) {
|
|
|
|
super(dictType);
|
2014-05-23 11:18:17 +00:00
|
|
|
mDictionaries = new CopyOnWriteArrayList<>();
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|
|
|
|
|
2013-01-06 02:10:27 +00:00
|
|
|
public DictionaryCollection(final String dictType, final Dictionary... dictionaries) {
|
2012-06-27 08:31:09 +00:00
|
|
|
super(dictType);
|
2011-07-19 09:16:34 +00:00
|
|
|
if (null == dictionaries) {
|
2014-05-23 11:18:17 +00:00
|
|
|
mDictionaries = new CopyOnWriteArrayList<>();
|
2011-07-19 09:16:34 +00:00
|
|
|
} else {
|
2014-05-23 11:18:17 +00:00
|
|
|
mDictionaries = new CopyOnWriteArrayList<>(dictionaries);
|
2011-07-22 05:46:43 +00:00
|
|
|
mDictionaries.removeAll(Collections.singleton(null));
|
2011-07-19 09:16:34 +00:00
|
|
|
}
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|
|
|
|
|
2013-01-06 02:10:27 +00:00
|
|
|
public DictionaryCollection(final String dictType, final Collection<Dictionary> dictionaries) {
|
2012-06-27 08:31:09 +00:00
|
|
|
super(dictType);
|
2014-05-23 11:18:17 +00:00
|
|
|
mDictionaries = new CopyOnWriteArrayList<>(dictionaries);
|
2011-07-22 05:46:43 +00:00
|
|
|
mDictionaries.removeAll(Collections.singleton(null));
|
2011-04-27 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
2012-07-09 09:25:27 +00:00
|
|
|
@Override
|
|
|
|
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
2014-05-19 04:55:40 +00:00
|
|
|
final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo,
|
2014-07-08 07:36:06 +00:00
|
|
|
final SettingsValuesForSuggestion settingsValuesForSuggestion,
|
2014-05-16 08:57:03 +00:00
|
|
|
final int sessionId, final float[] inOutLanguageWeight) {
|
2012-07-09 09:25:27 +00:00
|
|
|
final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
|
|
|
|
if (dictionaries.isEmpty()) return null;
|
|
|
|
// To avoid creating unnecessary objects, we get the list out of the first
|
|
|
|
// dictionary and add the rest to it if not null, hence the get(0)
|
|
|
|
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer,
|
2014-07-08 07:36:06 +00:00
|
|
|
prevWordsInfo, proximityInfo, settingsValuesForSuggestion, sessionId,
|
|
|
|
inOutLanguageWeight);
|
2014-05-23 11:18:17 +00:00
|
|
|
if (null == suggestions) suggestions = new ArrayList<>();
|
2012-07-09 09:25:27 +00:00
|
|
|
final int length = dictionaries.size();
|
2012-08-06 08:08:38 +00:00
|
|
|
for (int i = 1; i < length; ++ i) {
|
2012-07-09 09:25:27 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
|
2014-07-08 07:36:06 +00:00
|
|
|
prevWordsInfo, proximityInfo, settingsValuesForSuggestion, sessionId,
|
|
|
|
inOutLanguageWeight);
|
2012-07-09 09:25:27 +00:00
|
|
|
if (null != sugg) suggestions.addAll(sugg);
|
|
|
|
}
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2011-04-26 12:49:09 +00:00
|
|
|
@Override
|
2014-06-09 02:04:28 +00:00
|
|
|
public boolean isInDictionary(final String word) {
|
2011-06-16 11:05:46 +00:00
|
|
|
for (int i = mDictionaries.size() - 1; i >= 0; --i)
|
2014-06-09 02:04:28 +00:00
|
|
|
if (mDictionaries.get(i).isInDictionary(word)) return true;
|
2011-04-26 12:49:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-29 10:07:22 +00:00
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public int getFrequency(final String word) {
|
2012-05-29 10:07:22 +00:00
|
|
|
int maxFreq = -1;
|
|
|
|
for (int i = mDictionaries.size() - 1; i >= 0; --i) {
|
|
|
|
final int tempFreq = mDictionaries.get(i).getFrequency(word);
|
2014-06-06 10:38:44 +00:00
|
|
|
maxFreq = Math.max(tempFreq, maxFreq);
|
|
|
|
}
|
|
|
|
return maxFreq;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxFrequencyOfExactMatches(final String word) {
|
|
|
|
int maxFreq = -1;
|
|
|
|
for (int i = mDictionaries.size() - 1; i >= 0; --i) {
|
|
|
|
final int tempFreq = mDictionaries.get(i).getMaxFrequencyOfExactMatches(word);
|
|
|
|
maxFreq = Math.max(tempFreq, maxFreq);
|
2012-05-29 10:07:22 +00:00
|
|
|
}
|
|
|
|
return maxFreq;
|
|
|
|
}
|
|
|
|
|
2012-06-16 00:49:42 +00:00
|
|
|
@Override
|
|
|
|
public boolean isInitialized() {
|
|
|
|
return !mDictionaries.isEmpty();
|
2012-05-16 06:08:45 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 12:49:09 +00:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
for (final Dictionary dict : mDictionaries)
|
|
|
|
dict.close();
|
|
|
|
}
|
|
|
|
|
2011-11-09 23:21:42 +00:00
|
|
|
// Warning: this is not thread-safe. Take necessary precaution when calling.
|
|
|
|
public void addDictionary(final Dictionary newDict) {
|
|
|
|
if (null == newDict) return;
|
|
|
|
if (mDictionaries.contains(newDict)) {
|
|
|
|
Log.w(TAG, "This collection already contains this dictionary: " + newDict);
|
|
|
|
}
|
|
|
|
mDictionaries.add(newDict);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warning: this is not thread-safe. Take necessary precaution when calling.
|
|
|
|
public void removeDictionary(final Dictionary dict) {
|
|
|
|
if (mDictionaries.contains(dict)) {
|
|
|
|
mDictionaries.remove(dict);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "This collection does not contain this dictionary: " + dict);
|
|
|
|
}
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|
|
|
|
}
|