1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin; 18 19 import android.content.Context; 20 21 import com.android.inputmethod.keyboard.ProximityInfo; 22 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 23 import com.android.inputmethod.latin.makedict.DictEncoder; 24 import com.android.inputmethod.latin.makedict.FormatSpec; 25 import com.android.inputmethod.latin.makedict.FusionDictionary; 26 import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray; 27 import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; 28 import com.android.inputmethod.latin.makedict.UnsupportedFormatException; 29 import com.android.inputmethod.latin.utils.CollectionUtils; 30 31 import java.io.IOException; 32 import java.util.ArrayList; 33 import java.util.HashMap; 34 import java.util.Map; 35 36 /** 37 * An in memory dictionary for memorizing entries and writing a binary dictionary. 38 */ 39 public class DictionaryWriter extends AbstractDictionaryWriter { 40 private static final int BINARY_DICT_VERSION = 3; 41 private static final FormatSpec.FormatOptions FORMAT_OPTIONS = 42 new FormatSpec.FormatOptions(BINARY_DICT_VERSION, true /* supportsDynamicUpdate */); 43 44 private FusionDictionary mFusionDictionary; 45 46 public DictionaryWriter(final Context context, final String dictType) { 47 super(context, dictType); 48 clear(); 49 } 50 51 @Override 52 public void clear() { 53 final HashMap<String, String> attributes = CollectionUtils.newHashMap(); 54 mFusionDictionary = new FusionDictionary(new PtNodeArray(), 55 new FusionDictionary.DictionaryOptions(attributes, false, false)); 56 } 57 58 /** 59 * Adds a word unigram to the fusion dictionary. 60 */ 61 // TODO: Create "cache dictionary" to cache fresh words for frequently updated dictionaries, 62 // considering performance regression. 63 @Override 64 public void addUnigramWord(final String word, final String shortcutTarget, final int frequency, 65 final int shortcutFreq, final boolean isNotAWord) { 66 if (shortcutTarget == null) { 67 mFusionDictionary.add(word, frequency, null, isNotAWord); 68 } else { 69 // TODO: Do this in the subclass, with this class taking an arraylist. 70 final ArrayList<WeightedString> shortcutTargets = CollectionUtils.newArrayList(); 71 shortcutTargets.add(new WeightedString(shortcutTarget, shortcutFreq)); 72 mFusionDictionary.add(word, frequency, shortcutTargets, isNotAWord); 73 } 74 } 75 76 @Override 77 public void addBigramWords(final String word0, final String word1, final int frequency, 78 final boolean isValid, final long lastModifiedTime) { 79 mFusionDictionary.setBigram(word0, word1, frequency); 80 } 81 82 @Override 83 public void removeBigramWords(final String word0, final String word1) { 84 // This class don't support removing bigram words. 85 } 86 87 @Override 88 protected void writeDictionary(final DictEncoder dictEncoder, 89 final Map<String, String> attributeMap) throws IOException, UnsupportedFormatException { 90 for (final Map.Entry<String, String> entry : attributeMap.entrySet()) { 91 mFusionDictionary.addOptionAttribute(entry.getKey(), entry.getValue()); 92 } 93 dictEncoder.writeDictionary(mFusionDictionary, FORMAT_OPTIONS); 94 } 95 96 @Override 97 public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, 98 final String prevWord, final ProximityInfo proximityInfo, 99 boolean blockOffensiveWords, final int[] additionalFeaturesOptions) { 100 // This class doesn't support suggestion. 101 return null; 102 } 103 104 @Override 105 public boolean isValidWord(String word) { 106 // This class doesn't support dictionary retrieval. 107 return false; 108 } 109 } 110