Add symmetry and pre-emptively improve performance (A4)

The test for binary dictionary will soon entail decrypting and
uncompressing files if necessary to test for their headers, and will
become much slower than it is. It's better to be able to detect the
XML format too, and leave the slower test for last.

Bug: 7388852
Change-Id: I6b9a7944de80217e1571cab65dcd1cff347b3046
main
Jean Chalard 2012-10-25 16:28:17 +09:00
parent 0044df6cf2
commit 3862b9e819
2 changed files with 39 additions and 3 deletions

View File

@ -172,12 +172,15 @@ public class DictionaryMaker {
String filename = args.get(0);
args.remove(0);
if (OPTION_INPUT_SOURCE.equals(arg)) {
if (BinaryDictInputOutput.isBinaryDictionary(filename)) {
inputBinary = filename;
if (XmlDictInputOutput.isXmlUnigramDictionary(filename)) {
inputUnigramXml = filename;
} else if (CombinedInputOutput.isCombinedDictionary(filename)) {
inputCombined = filename;
} else if (BinaryDictInputOutput.isBinaryDictionary(filename)) {
inputBinary = filename;
} else {
inputUnigramXml = filename;
throw new IllegalArgumentException(
"Unknown format for file " + filename);
}
} else if (OPTION_INPUT_SHORTCUT_XML.equals(arg)) {
inputShortcutXml = filename;

View File

@ -22,6 +22,10 @@ import com.android.inputmethod.latin.makedict.FusionDictionary.Node;
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import com.android.inputmethod.latin.makedict.Word;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
@ -263,6 +267,35 @@ public class XmlDictInputOutput {
}
}
/**
* Basic test to find out whether the file is in the unigram XML format or not.
*
* Concretely this only tests the header line.
*
* @param filename The name of the file to test.
* @return true if the file is in the unigram XML format, false otherwise
*/
public static boolean isXmlUnigramDictionary(final String filename) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File(filename)));
final String firstLine = reader.readLine();
return firstLine.matches("^\\s*<wordlist .*>\\s*$");
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// do nothing
}
}
}
}
/**
* Reads a dictionary from an XML file.
*