Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.latin.utils;
     18 
     19 import com.android.inputmethod.latin.makedict.DictionaryHeader;
     20 import com.android.inputmethod.latin.makedict.ProbabilityInfo;
     21 import com.android.inputmethod.latin.makedict.WeightedString;
     22 import com.android.inputmethod.latin.makedict.WordProperty;
     23 
     24 import java.util.HashMap;
     25 
     26 public class CombinedFormatUtils {
     27     public static final String DICTIONARY_TAG = "dictionary";
     28     public static final String BIGRAM_TAG = "bigram";
     29     public static final String SHORTCUT_TAG = "shortcut";
     30     public static final String PROBABILITY_TAG = "f";
     31     public static final String HISTORICAL_INFO_TAG = "historicalInfo";
     32     public static final String HISTORICAL_INFO_SEPARATOR = ":";
     33     public static final String WORD_TAG = "word";
     34     public static final String BEGINNING_OF_SENTENCE_TAG = "beginning_of_sentence";
     35     public static final String NOT_A_WORD_TAG = "not_a_word";
     36     public static final String BLACKLISTED_TAG = "blacklisted";
     37 
     38     public static String formatAttributeMap(final HashMap<String, String> attributeMap) {
     39         final StringBuilder builder = new StringBuilder();
     40         builder.append(DICTIONARY_TAG + "=");
     41         if (attributeMap.containsKey(DictionaryHeader.DICTIONARY_ID_KEY)) {
     42             builder.append(attributeMap.get(DictionaryHeader.DICTIONARY_ID_KEY));
     43         }
     44         for (final String key : attributeMap.keySet()) {
     45             if (key.equals(DictionaryHeader.DICTIONARY_ID_KEY)) {
     46                 continue;
     47             }
     48             final String value = attributeMap.get(key);
     49             builder.append("," + key + "=" + value);
     50         }
     51         builder.append("\n");
     52         return builder.toString();
     53     }
     54 
     55     public static String formatWordProperty(final WordProperty wordProperty) {
     56         final StringBuilder builder = new StringBuilder();
     57         builder.append(" " + WORD_TAG + "=" + wordProperty.mWord);
     58         builder.append(",");
     59         builder.append(formatProbabilityInfo(wordProperty.mProbabilityInfo));
     60         if (wordProperty.mIsBeginningOfSentence) {
     61             builder.append("," + BEGINNING_OF_SENTENCE_TAG + "=true");
     62         }
     63         if (wordProperty.mIsNotAWord) {
     64             builder.append("," + NOT_A_WORD_TAG + "=true");
     65         }
     66         if (wordProperty.mIsBlacklistEntry) {
     67             builder.append("," + BLACKLISTED_TAG + "=true");
     68         }
     69         builder.append("\n");
     70         if (wordProperty.mShortcutTargets != null) {
     71             for (final WeightedString shortcutTarget : wordProperty.mShortcutTargets) {
     72                 builder.append("  " + SHORTCUT_TAG + "=" + shortcutTarget.mWord);
     73                 builder.append(",");
     74                 builder.append(formatProbabilityInfo(shortcutTarget.mProbabilityInfo));
     75                 builder.append("\n");
     76             }
     77         }
     78         if (wordProperty.mBigrams != null) {
     79             for (final WeightedString bigram : wordProperty.mBigrams) {
     80                 builder.append("  " + BIGRAM_TAG + "=" + bigram.mWord);
     81                 builder.append(",");
     82                 builder.append(formatProbabilityInfo(bigram.mProbabilityInfo));
     83                 builder.append("\n");
     84             }
     85         }
     86         return builder.toString();
     87     }
     88 
     89     public static String formatProbabilityInfo(final ProbabilityInfo probabilityInfo) {
     90         final StringBuilder builder = new StringBuilder();
     91         builder.append(PROBABILITY_TAG + "=" + probabilityInfo.mProbability);
     92         if (probabilityInfo.hasHistoricalInfo()) {
     93             builder.append(",");
     94             builder.append(HISTORICAL_INFO_TAG + "=");
     95             builder.append(probabilityInfo.mTimestamp);
     96             builder.append(HISTORICAL_INFO_SEPARATOR);
     97             builder.append(probabilityInfo.mLevel);
     98             builder.append(HISTORICAL_INFO_SEPARATOR);
     99             builder.append(probabilityInfo.mCount);
    100         }
    101         return builder.toString();
    102     }
    103 }
    104