2012-05-28 08:30:18 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.dicttool;
|
|
|
|
|
2012-10-25 09:15:42 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2012-10-25 06:53:21 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary;
|
2013-08-22 02:07:52 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode;
|
2014-03-06 09:53:06 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.WeightedString;
|
2014-02-06 06:13:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.WordProperty;
|
2012-10-25 06:53:21 +00:00
|
|
|
|
2012-10-31 06:35:20 +00:00
|
|
|
import java.util.Arrays;
|
2012-10-29 03:28:20 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2012-05-28 08:30:18 +00:00
|
|
|
public class Info extends Dicttool.Command {
|
2012-08-02 14:15:17 +00:00
|
|
|
public static final String COMMAND = "info";
|
|
|
|
|
2012-05-28 08:30:18 +00:00
|
|
|
public Info() {
|
|
|
|
}
|
|
|
|
|
2012-10-25 09:15:42 +00:00
|
|
|
@Override
|
2012-05-28 08:30:18 +00:00
|
|
|
public String getHelp() {
|
2012-10-30 10:54:34 +00:00
|
|
|
return COMMAND + " <filename>: prints various information about a dictionary file";
|
2012-05-28 08:30:18 +00:00
|
|
|
}
|
|
|
|
|
2012-10-31 06:35:20 +00:00
|
|
|
private static void showInfo(final FusionDictionary dict, final boolean plumbing) {
|
2012-10-25 09:15:42 +00:00
|
|
|
System.out.println("Header attributes :");
|
2012-10-31 06:35:20 +00:00
|
|
|
System.out.print(dict.mOptions.toString(2, plumbing));
|
2012-10-25 09:15:42 +00:00
|
|
|
int wordCount = 0;
|
|
|
|
int bigramCount = 0;
|
|
|
|
int shortcutCount = 0;
|
2020-07-23 03:56:25 +00:00
|
|
|
int allowlistCount = 0;
|
2014-02-06 06:13:33 +00:00
|
|
|
for (final WordProperty wordProperty : dict) {
|
2012-10-25 09:15:42 +00:00
|
|
|
++wordCount;
|
2014-10-01 02:21:08 +00:00
|
|
|
if (wordProperty.mHasNgrams) {
|
|
|
|
bigramCount += wordProperty.mNgrams.size();
|
2012-10-25 09:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("Words in the dictionary : " + wordCount);
|
|
|
|
System.out.println("Bigram count : " + bigramCount);
|
2020-07-23 03:56:25 +00:00
|
|
|
System.out.println("Shortcuts : " + shortcutCount + " (out of which " + allowlistCount
|
|
|
|
+ " allowlist entries)");
|
2012-10-25 09:15:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:04:07 +00:00
|
|
|
private static void showWordInfo(final FusionDictionary dict, final String word) {
|
2013-08-22 02:07:52 +00:00
|
|
|
final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word);
|
|
|
|
if (null == ptNode) {
|
2012-10-29 03:28:20 +00:00
|
|
|
System.out.println(word + " is not in the dictionary");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
System.out.println("Word: " + word);
|
2014-02-10 06:05:08 +00:00
|
|
|
System.out.println(" Freq: " + ptNode.getProbability());
|
2013-08-22 02:07:52 +00:00
|
|
|
if (ptNode.getIsNotAWord()) {
|
2012-10-29 03:28:20 +00:00
|
|
|
System.out.println(" Is not a word");
|
|
|
|
}
|
2014-10-14 03:13:11 +00:00
|
|
|
if (ptNode.getIsPossiblyOffensive()) {
|
|
|
|
System.out.println(" Is possibly offensive");
|
2012-10-29 03:28:20 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
final ArrayList<WeightedString> bigrams = ptNode.getBigrams();
|
2012-10-29 03:28:20 +00:00
|
|
|
if (null == bigrams || bigrams.isEmpty()) {
|
|
|
|
System.out.println(" No bigrams");
|
|
|
|
} else {
|
|
|
|
for (final WeightedString bigram : bigrams) {
|
2014-02-05 12:44:55 +00:00
|
|
|
System.out.println(
|
|
|
|
" Bigram: " + bigram.mWord + " (" + bigram.getProbability() + ")");
|
2012-10-29 03:28:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-25 09:15:42 +00:00
|
|
|
@Override
|
2012-05-28 08:30:18 +00:00
|
|
|
public void run() {
|
2012-08-02 14:15:17 +00:00
|
|
|
if (mArgs.length < 1) {
|
|
|
|
throw new RuntimeException("Not enough arguments for command " + COMMAND);
|
|
|
|
}
|
2012-10-31 06:35:20 +00:00
|
|
|
final boolean plumbing;
|
|
|
|
if ("-p".equals(mArgs[0])) {
|
|
|
|
plumbing = true;
|
|
|
|
mArgs = Arrays.copyOfRange(mArgs, 1, mArgs.length);
|
|
|
|
if (mArgs.length != 1) { // There should be only 1 argument left
|
|
|
|
throw new RuntimeException("Wrong number of arguments for command " + COMMAND);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
plumbing = false;
|
|
|
|
}
|
2012-10-25 06:53:21 +00:00
|
|
|
final String filename = mArgs[0];
|
2012-10-29 03:28:20 +00:00
|
|
|
final boolean hasWordArguments = (1 == mArgs.length);
|
2012-10-25 09:26:10 +00:00
|
|
|
final FusionDictionary dict = BinaryDictOffdeviceUtils.getDictionary(filename,
|
2012-10-29 03:28:20 +00:00
|
|
|
hasWordArguments /* report */);
|
|
|
|
if (hasWordArguments) {
|
2012-10-31 06:35:20 +00:00
|
|
|
showInfo(dict, plumbing);
|
2012-10-29 03:28:20 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 1; i < mArgs.length; ++i) {
|
2014-10-22 05:04:07 +00:00
|
|
|
showWordInfo(dict, mArgs[i]);
|
2012-10-29 03:28:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-28 08:30:18 +00:00
|
|
|
}
|
|
|
|
}
|