1 /** 2 * Copyright (C) 2012 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.dicttool; 18 19 import com.android.inputmethod.latin.makedict.FormatSpec; 20 import com.android.inputmethod.latin.makedict.FusionDictionary; 21 import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode; 22 import com.android.inputmethod.latin.makedict.WeightedString; 23 import com.android.inputmethod.latin.makedict.WordProperty; 24 25 import java.util.Arrays; 26 import java.util.ArrayList; 27 28 public class Info extends Dicttool.Command { 29 public static final String COMMAND = "info"; 30 31 public Info() { 32 } 33 34 @Override 35 public String getHelp() { 36 return COMMAND + " <filename>: prints various information about a dictionary file"; 37 } 38 39 private static void showInfo(final FusionDictionary dict, final boolean plumbing) { 40 System.out.println("Header attributes :"); 41 System.out.print(dict.mOptions.toString(2, plumbing)); 42 int wordCount = 0; 43 int bigramCount = 0; 44 int shortcutCount = 0; 45 int whitelistCount = 0; 46 for (final WordProperty wordProperty : dict) { 47 ++wordCount; 48 if (wordProperty.mHasNgrams) { 49 bigramCount += wordProperty.mNgrams.size(); 50 } 51 if (null != wordProperty.mShortcutTargets) { 52 shortcutCount += wordProperty.mShortcutTargets.size(); 53 for (WeightedString shortcutTarget : wordProperty.mShortcutTargets) { 54 if (FormatSpec.SHORTCUT_WHITELIST_FREQUENCY 55 == shortcutTarget.getProbability()) { 56 ++whitelistCount; 57 } 58 } 59 } 60 } 61 System.out.println("Words in the dictionary : " + wordCount); 62 System.out.println("Bigram count : " + bigramCount); 63 System.out.println("Shortcuts : " + shortcutCount + " (out of which " + whitelistCount 64 + " whitelist entries)"); 65 } 66 67 private static void showWordInfo(final FusionDictionary dict, final String word) { 68 final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word); 69 if (null == ptNode) { 70 System.out.println(word + " is not in the dictionary"); 71 return; 72 } 73 System.out.println("Word: " + word); 74 System.out.println(" Freq: " + ptNode.getProbability()); 75 if (ptNode.getIsNotAWord()) { 76 System.out.println(" Is not a word"); 77 } 78 if (ptNode.getIsPossiblyOffensive()) { 79 System.out.println(" Is possibly offensive"); 80 } 81 final ArrayList<WeightedString> shortcutTargets = ptNode.getShortcutTargets(); 82 if (null == shortcutTargets || shortcutTargets.isEmpty()) { 83 System.out.println(" No shortcuts"); 84 } else { 85 for (final WeightedString shortcutTarget : shortcutTargets) { 86 System.out.println(" Shortcut target: " + shortcutTarget.mWord + " (" 87 + (FormatSpec.SHORTCUT_WHITELIST_FREQUENCY 88 == shortcutTarget.getProbability() ? 89 "whitelist" : shortcutTarget.getProbability()) + ")"); 90 } 91 } 92 final ArrayList<WeightedString> bigrams = ptNode.getBigrams(); 93 if (null == bigrams || bigrams.isEmpty()) { 94 System.out.println(" No bigrams"); 95 } else { 96 for (final WeightedString bigram : bigrams) { 97 System.out.println( 98 " Bigram: " + bigram.mWord + " (" + bigram.getProbability() + ")"); 99 } 100 } 101 } 102 103 @Override 104 public void run() { 105 if (mArgs.length < 1) { 106 throw new RuntimeException("Not enough arguments for command " + COMMAND); 107 } 108 final boolean plumbing; 109 if ("-p".equals(mArgs[0])) { 110 plumbing = true; 111 mArgs = Arrays.copyOfRange(mArgs, 1, mArgs.length); 112 if (mArgs.length != 1) { // There should be only 1 argument left 113 throw new RuntimeException("Wrong number of arguments for command " + COMMAND); 114 } 115 } else { 116 plumbing = false; 117 } 118 final String filename = mArgs[0]; 119 final boolean hasWordArguments = (1 == mArgs.length); 120 final FusionDictionary dict = BinaryDictOffdeviceUtils.getDictionary(filename, 121 hasWordArguments /* report */); 122 if (hasWordArguments) { 123 showInfo(dict, plumbing); 124 } else { 125 for (int i = 1; i < mArgs.length; ++i) { 126 showWordInfo(dict, mArgs[i]); 127 } 128 } 129 } 130 } 131