• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NgramProperty;
21 import com.android.inputmethod.latin.makedict.ProbabilityInfo;
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 NGRAM_TAG = "ngram";
30     public static final String NGRAM_PREV_WORD_TAG = "prev_word";
31     public static final String PROBABILITY_TAG = "f";
32     public static final String HISTORICAL_INFO_TAG = "historicalInfo";
33     public static final String HISTORICAL_INFO_SEPARATOR = ":";
34     public static final String WORD_TAG = "word";
35     public static final String BEGINNING_OF_SENTENCE_TAG = "beginning_of_sentence";
36     public static final String NOT_A_WORD_TAG = "not_a_word";
37     public static final String POSSIBLY_OFFENSIVE_TAG = "possibly_offensive";
38     public static final String TRUE_VALUE = "true";
39 
formatAttributeMap(final HashMap<String, String> attributeMap)40     public static String formatAttributeMap(final HashMap<String, String> attributeMap) {
41         final StringBuilder builder = new StringBuilder();
42         builder.append(DICTIONARY_TAG + "=");
43         if (attributeMap.containsKey(DictionaryHeader.DICTIONARY_ID_KEY)) {
44             builder.append(attributeMap.get(DictionaryHeader.DICTIONARY_ID_KEY));
45         }
46         for (final String key : attributeMap.keySet()) {
47             if (key.equals(DictionaryHeader.DICTIONARY_ID_KEY)) {
48                 continue;
49             }
50             final String value = attributeMap.get(key);
51             builder.append("," + key + "=" + value);
52         }
53         builder.append("\n");
54         return builder.toString();
55     }
56 
formatWordProperty(final WordProperty wordProperty)57     public static String formatWordProperty(final WordProperty wordProperty) {
58         final StringBuilder builder = new StringBuilder();
59         builder.append(" " + WORD_TAG + "=" + wordProperty.mWord);
60         builder.append(",");
61         builder.append(formatProbabilityInfo(wordProperty.mProbabilityInfo));
62         if (wordProperty.mIsBeginningOfSentence) {
63             builder.append("," + BEGINNING_OF_SENTENCE_TAG + "=" + TRUE_VALUE);
64         }
65         if (wordProperty.mIsNotAWord) {
66             builder.append("," + NOT_A_WORD_TAG + "=" + TRUE_VALUE);
67         }
68         if (wordProperty.mIsPossiblyOffensive) {
69             builder.append("," + POSSIBLY_OFFENSIVE_TAG + "=" + TRUE_VALUE);
70         }
71         builder.append("\n");
72         if (wordProperty.mHasNgrams) {
73             for (final NgramProperty ngramProperty : wordProperty.mNgrams) {
74                 builder.append(" " + NGRAM_TAG + "=" + ngramProperty.mTargetWord.mWord);
75                 builder.append(",");
76                 builder.append(formatProbabilityInfo(ngramProperty.mTargetWord.mProbabilityInfo));
77                 builder.append("\n");
78                 for (int i = 0; i < ngramProperty.mNgramContext.getPrevWordCount(); i++) {
79                     builder.append("  " + NGRAM_PREV_WORD_TAG + "[" + i + "]="
80                             + ngramProperty.mNgramContext.getNthPrevWord(i + 1));
81                     if (ngramProperty.mNgramContext.isNthPrevWordBeginningOfSentence(i + 1)) {
82                         builder.append("," + BEGINNING_OF_SENTENCE_TAG + "=true");
83                     }
84                     builder.append("\n");
85                 }
86             }
87         }
88         return builder.toString();
89     }
90 
formatProbabilityInfo(final ProbabilityInfo probabilityInfo)91     public static String formatProbabilityInfo(final ProbabilityInfo probabilityInfo) {
92         final StringBuilder builder = new StringBuilder();
93         builder.append(PROBABILITY_TAG + "=" + probabilityInfo.mProbability);
94         if (probabilityInfo.hasHistoricalInfo()) {
95             builder.append(",");
96             builder.append(HISTORICAL_INFO_TAG + "=");
97             builder.append(probabilityInfo.mTimestamp);
98             builder.append(HISTORICAL_INFO_SEPARATOR);
99             builder.append(probabilityInfo.mLevel);
100             builder.append(HISTORICAL_INFO_SEPARATOR);
101             builder.append(probabilityInfo.mCount);
102         }
103         return builder.toString();
104     }
105 
isLiteralTrue(final String value)106     public static boolean isLiteralTrue(final String value) {
107         return TRUE_VALUE.equalsIgnoreCase(value);
108     }
109 }
110