Home | History | Annotate | Download | only in latin
      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 import android.util.Log;
     21 
     22 import com.android.inputmethod.latin.makedict.DictEncoder;
     23 import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
     24 import com.android.inputmethod.latin.makedict.Ver3DictEncoder;
     25 
     26 import java.io.File;
     27 import java.io.IOException;
     28 import java.util.Map;
     29 
     30 // TODO: Quit extending Dictionary after implementing dynamic binary dictionary.
     31 abstract public class AbstractDictionaryWriter extends Dictionary {
     32     /** Used for Log actions from this class */
     33     private static final String TAG = AbstractDictionaryWriter.class.getSimpleName();
     34 
     35     private final Context mContext;
     36 
     37     public AbstractDictionaryWriter(final Context context, final String dictType) {
     38         super(dictType);
     39         mContext = context;
     40     }
     41 
     42     abstract public void clear();
     43 
     44     /**
     45      * Add a unigram with an optional shortcut to the dictionary.
     46      * @param word The word to add.
     47      * @param shortcutTarget A shortcut target for this word, or null if none.
     48      * @param frequency The frequency for this unigram.
     49      * @param shortcutFreq The frequency of the shortcut (0~15, with 15 = whitelist). Ignored
     50      *   if shortcutTarget is null.
     51      * @param isNotAWord true if this is not a word, i.e. shortcut only.
     52      */
     53     abstract public void addUnigramWord(final String word, final String shortcutTarget,
     54             final int frequency, final int shortcutFreq, final boolean isNotAWord);
     55 
     56     // TODO: Remove lastModifiedTime after making binary dictionary support forgetting curve.
     57     abstract public void addBigramWords(final String word0, final String word1,
     58             final int frequency, final boolean isValid,
     59             final long lastModifiedTime);
     60 
     61     abstract public void removeBigramWords(final String word0, final String word1);
     62 
     63     abstract protected void writeDictionary(final DictEncoder dictEncoder,
     64             final Map<String, String> attributeMap) throws IOException, UnsupportedFormatException;
     65 
     66     public void write(final String fileName, final Map<String, String> attributeMap) {
     67         final String tempFileName = fileName + ".temp";
     68         final File file = new File(mContext.getFilesDir(), fileName);
     69         final File tempFile = new File(mContext.getFilesDir(), tempFileName);
     70         try {
     71             final DictEncoder dictEncoder = new Ver3DictEncoder(tempFile);
     72             writeDictionary(dictEncoder, attributeMap);
     73             tempFile.renameTo(file);
     74         } catch (IOException e) {
     75             Log.e(TAG, "IO exception while writing file", e);
     76         } catch (UnsupportedFormatException e) {
     77             Log.e(TAG, "Unsupported format", e);
     78         }
     79     }
     80 }
     81