Add a reporting option to the getDictionary method (A7)

This is refactoring to help future changes

Change-Id: I8c4372383a2fff5805b96cc47df5012e4b231004
main
Jean Chalard 2012-10-25 18:26:10 +09:00
parent da8f4e2616
commit 6ecc50a867
5 changed files with 69 additions and 67 deletions

View File

@ -17,6 +17,11 @@
package com.android.inputmethod.latin.dicttool; package com.android.inputmethod.latin.dicttool;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput; import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.ByteBufferWrapper;
import com.android.inputmethod.latin.makedict.FusionDictionary;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import org.xml.sax.SAXException;
import java.io.File; import java.io.File;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -26,8 +31,13 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.util.ArrayList; import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
/** /**
* Class grouping utilities for offline dictionary making. * Class grouping utilities for offline dictionary making.
* *
@ -141,4 +151,54 @@ public final class BinaryDictOffdeviceUtils {
return null; return null;
} }
} }
static void crash(final String filename, final Exception e) {
throw new RuntimeException("Can't read file " + filename, e);
}
static FusionDictionary getDictionary(final String filename, final boolean report) {
final File file = new File(filename);
if (report) {
System.out.println("Dictionary : " + file.getAbsolutePath());
System.out.println("Size : " + file.length() + " bytes");
}
try {
if (XmlDictInputOutput.isXmlUnigramDictionary(filename)) {
if (report) System.out.println("Format : XML unigram list");
return XmlDictInputOutput.readDictionaryXml(
new BufferedInputStream(new FileInputStream(file)),
null /* shortcuts */, null /* bigrams */);
} else if (CombinedInputOutput.isCombinedDictionary(filename)) {
if (report) System.out.println("Format : Combined format");
return CombinedInputOutput.readDictionaryCombined(
new BufferedInputStream(new FileInputStream(file)));
} else {
final DecoderChainSpec decodedSpec = getRawBinaryDictionaryOrNull(file);
if (null == decodedSpec) {
crash(filename, new RuntimeException(
filename + " does not seem to be a dictionary file"));
} else {
final FileInputStream inStream = new FileInputStream(decodedSpec.mFile);
final ByteBuffer buffer = inStream.getChannel().map(
FileChannel.MapMode.READ_ONLY, 0, decodedSpec.mFile.length());
if (report) {
System.out.println("Format : Binary dictionary format");
System.out.println("Packaging : " + decodedSpec.describeChain());
System.out.println("Uncompressed size : " + decodedSpec.mFile.length());
}
return BinaryDictInputOutput.readDictionaryBinary(
new BinaryDictInputOutput.ByteBufferWrapper(buffer), null);
}
}
} catch (IOException e) {
crash(filename, e);
} catch (SAXException e) {
crash(filename, e);
} catch (ParserConfigurationException e) {
crash(filename, e);
} catch (UnsupportedFormatException e) {
crash(filename, e);
}
return null;
}
} }

View File

@ -45,11 +45,13 @@ public class Compress {
public Compressor() { public Compressor() {
} }
@Override
public String getHelp() { public String getHelp() {
return COMMAND + " <src_filename> <dst_filename>: " return COMMAND + " <src_filename> <dst_filename>: "
+ "Compresses a file using gzip compression"; + "Compresses a file using gzip compression";
} }
@Override
public void run() throws IOException { public void run() throws IOException {
if (mArgs.length > 2) { if (mArgs.length > 2) {
throw new RuntimeException("Too many arguments for command " + COMMAND); throw new RuntimeException("Too many arguments for command " + COMMAND);
@ -71,11 +73,13 @@ public class Compress {
public Uncompressor() { public Uncompressor() {
} }
@Override
public String getHelp() { public String getHelp() {
return COMMAND + " <src_filename> <dst_filename>: " return COMMAND + " <src_filename> <dst_filename>: "
+ "Uncompresses a file compressed with gzip compression"; + "Uncompresses a file compressed with gzip compression";
} }
@Override
public void run() throws IOException { public void run() throws IOException {
if (mArgs.length > 2) { if (mArgs.length > 2) {
throw new RuntimeException("Too many arguments for command " + COMMAND); throw new RuntimeException("Too many arguments for command " + COMMAND);

View File

@ -16,25 +16,11 @@
package com.android.inputmethod.latin.dicttool; package com.android.inputmethod.latin.dicttool;
import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils.DecoderChainSpec;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
import com.android.inputmethod.latin.makedict.FormatSpec; import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.FusionDictionary; import com.android.inputmethod.latin.makedict.FusionDictionary;
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.makedict.Word; import com.android.inputmethod.latin.makedict.Word;
import org.xml.sax.SAXException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.xml.parsers.ParserConfigurationException;
public class Info extends Dicttool.Command { public class Info extends Dicttool.Command {
public static final String COMMAND = "info"; public static final String COMMAND = "info";
@ -46,52 +32,6 @@ public class Info extends Dicttool.Command {
return COMMAND + "<filename>: prints various information about a dictionary file"; return COMMAND + "<filename>: prints various information about a dictionary file";
} }
private static void crash(final String filename, final Exception e) {
throw new RuntimeException("Can't read file " + filename, e);
}
private static FusionDictionary getDictionary(final String filename) {
final File file = new File(filename);
System.out.println("Dictionary : " + file.getAbsolutePath());
System.out.println("Size : " + file.length() + " bytes");
try {
if (XmlDictInputOutput.isXmlUnigramDictionary(filename)) {
System.out.println("Format : XML unigram list");
return XmlDictInputOutput.readDictionaryXml(
new BufferedInputStream(new FileInputStream(file)),
null /* shortcuts */, null /* bigrams */);
} else if (CombinedInputOutput.isCombinedDictionary(filename)) {
System.out.println("Format : Combined format");
return CombinedInputOutput.readDictionaryCombined(
new BufferedInputStream(new FileInputStream(file)));
} else {
final DecoderChainSpec decodedSpec =
BinaryDictOffdeviceUtils.getRawBinaryDictionaryOrNull(file);
if (null == decodedSpec) {
crash(filename, new RuntimeException(
filename + " does not seem to be a dictionary file"));
}
final FileInputStream inStream = new FileInputStream(decodedSpec.mFile);
final ByteBuffer buffer = inStream.getChannel().map(
FileChannel.MapMode.READ_ONLY, 0, decodedSpec.mFile.length());
System.out.println("Format : Binary dictionary format");
System.out.println("Packaging : " + decodedSpec.describeChain());
System.out.println("Uncompressed size : " + decodedSpec.mFile.length());
return BinaryDictInputOutput.readDictionaryBinary(
new BinaryDictInputOutput.ByteBufferWrapper(buffer), null);
}
} catch (IOException e) {
crash(filename, e);
} catch (SAXException e) {
crash(filename, e);
} catch (ParserConfigurationException e) {
crash(filename, e);
} catch (UnsupportedFormatException e) {
crash(filename, e);
}
return null;
}
private static void showInfo(final FusionDictionary dict) { private static void showInfo(final FusionDictionary dict) {
System.out.println("Header attributes :"); System.out.println("Header attributes :");
System.out.print(dict.mOptions.toString(2)); System.out.print(dict.mOptions.toString(2));
@ -125,7 +65,8 @@ public class Info extends Dicttool.Command {
throw new RuntimeException("Not enough arguments for command " + COMMAND); throw new RuntimeException("Not enough arguments for command " + COMMAND);
} }
final String filename = mArgs[0]; final String filename = mArgs[0];
final FusionDictionary dict = getDictionary(filename); final FusionDictionary dict = BinaryDictOffdeviceUtils.getDictionary(filename,
true /* report */);
showInfo(dict); showInfo(dict);
} }
} }

View File

@ -29,10 +29,12 @@ public class Makedict extends Dicttool.Command {
public Makedict() { public Makedict() {
} }
@Override
public String getHelp() { public String getHelp() {
return DictionaryMaker.Arguments.getHelp(); return DictionaryMaker.Arguments.getHelp();
} }
@Override
public void run() throws FileNotFoundException, IOException, ParserConfigurationException, public void run() throws FileNotFoundException, IOException, ParserConfigurationException,
SAXException, UnsupportedFormatException { SAXException, UnsupportedFormatException {
DictionaryMaker.main(mArgs); DictionaryMaker.main(mArgs);

View File

@ -56,8 +56,6 @@ public class XmlDictInputOutput {
private static final String WORD_ATTR = "word"; private static final String WORD_ATTR = "word";
private static final String NOT_A_WORD_ATTR = "not_a_word"; private static final String NOT_A_WORD_ATTR = "not_a_word";
private static final int SHORTCUT_ONLY_DEFAULT_FREQ = 1;
private static final String OPTIONS_KEY = "options"; private static final String OPTIONS_KEY = "options";
private static final String GERMAN_UMLAUT_PROCESSING_OPTION = "german_umlaut_processing"; private static final String GERMAN_UMLAUT_PROCESSING_OPTION = "german_umlaut_processing";
private static final String FRENCH_LIGATURE_PROCESSING_OPTION = "french_ligature_processing"; private static final String FRENCH_LIGATURE_PROCESSING_OPTION = "french_ligature_processing";
@ -67,12 +65,9 @@ public class XmlDictInputOutput {
*/ */
static private class UnigramHandler extends DefaultHandler { static private class UnigramHandler extends DefaultHandler {
// Parser states // Parser states
private static final int NONE = 0;
private static final int START = 1; private static final int START = 1;
private static final int WORD = 2; private static final int WORD = 2;
private static final int BIGRAM = 4; private static final int UNKNOWN = 3;
private static final int END = 5;
private static final int UNKNOWN = 6;
FusionDictionary mDictionary; FusionDictionary mDictionary;
int mState; // the state of the parser int mState; // the state of the parser