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 Info()31 public Info() { 32 } 33 34 @Override getHelp()35 public String getHelp() { 36 return COMMAND + " <filename>: prints various information about a dictionary file"; 37 } 38 showInfo(final FusionDictionary dict, final boolean plumbing)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 allowlistCount = 0; 46 for (final WordProperty wordProperty : dict) { 47 ++wordCount; 48 if (wordProperty.mHasNgrams) { 49 bigramCount += wordProperty.mNgrams.size(); 50 } 51 } 52 System.out.println("Words in the dictionary : " + wordCount); 53 System.out.println("Bigram count : " + bigramCount); 54 System.out.println("Shortcuts : " + shortcutCount + " (out of which " + allowlistCount 55 + " allowlist entries)"); 56 } 57 showWordInfo(final FusionDictionary dict, final String word)58 private static void showWordInfo(final FusionDictionary dict, final String word) { 59 final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word); 60 if (null == ptNode) { 61 System.out.println(word + " is not in the dictionary"); 62 return; 63 } 64 System.out.println("Word: " + word); 65 System.out.println(" Freq: " + ptNode.getProbability()); 66 if (ptNode.getIsNotAWord()) { 67 System.out.println(" Is not a word"); 68 } 69 if (ptNode.getIsPossiblyOffensive()) { 70 System.out.println(" Is possibly offensive"); 71 } 72 final ArrayList<WeightedString> bigrams = ptNode.getBigrams(); 73 if (null == bigrams || bigrams.isEmpty()) { 74 System.out.println(" No bigrams"); 75 } else { 76 for (final WeightedString bigram : bigrams) { 77 System.out.println( 78 " Bigram: " + bigram.mWord + " (" + bigram.getProbability() + ")"); 79 } 80 } 81 } 82 83 @Override run()84 public void run() { 85 if (mArgs.length < 1) { 86 throw new RuntimeException("Not enough arguments for command " + COMMAND); 87 } 88 final boolean plumbing; 89 if ("-p".equals(mArgs[0])) { 90 plumbing = true; 91 mArgs = Arrays.copyOfRange(mArgs, 1, mArgs.length); 92 if (mArgs.length != 1) { // There should be only 1 argument left 93 throw new RuntimeException("Wrong number of arguments for command " + COMMAND); 94 } 95 } else { 96 plumbing = false; 97 } 98 final String filename = mArgs[0]; 99 final boolean hasWordArguments = (1 == mArgs.length); 100 final FusionDictionary dict = BinaryDictOffdeviceUtils.getDictionary(filename, 101 hasWordArguments /* report */); 102 if (hasWordArguments) { 103 showInfo(dict, plumbing); 104 } else { 105 for (int i = 1; i < mArgs.length; ++i) { 106 showWordInfo(dict, mArgs[i]); 107 } 108 } 109 } 110 } 111