Skip to content
Snippets Groups Projects
Commit 3862b9e8 authored by Jean Chalard's avatar Jean Chalard
Browse files

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
parent 0044df6c
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment