| 1 |
package morfologik.tools; |
| 2 |
|
| 3 |
import java.io.BufferedInputStream; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.nio.file.Files; |
| 6 |
import java.nio.file.Path; |
| 7 |
import java.util.Locale; |
| 8 |
|
| 9 |
import morfologik.fsa.CFSA; |
| 10 |
import morfologik.fsa.CFSA2; |
| 11 |
import morfologik.fsa.FSA; |
| 12 |
import morfologik.fsa.FSA5; |
| 13 |
|
| 14 |
import com.beust.jcommander.Parameter; |
| 15 |
import com.beust.jcommander.Parameters; |
| 16 |
|
| 17 |
/** |
| 18 |
* Print extra information about a compiled automaton file. |
| 19 |
*/ |
| 20 |
@Parameters( |
| 21 |
commandNames = "fsa_info", |
| 22 |
commandDescription = "Print extra information about a compiled automaton file.") |
| 23 |
public class FSAInfo extends CliTool { |
| 24 |
@Parameter( |
| 25 |
names = {"-i", "--input"}, |
| 26 |
description = "The input automaton.", |
| 27 |
required = true, |
| 28 |
validateValueWith = ValidateFileExists.class) |
| 29 |
private Path input; |
| 30 |
|
| 31 |
FSAInfo() { |
| 32 |
} |
| 33 |
|
| 34 |
public FSAInfo(Path input) { |
| 35 |
this.input = checkNotNull(input); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public ExitStatus call() throws Exception { |
| 40 |
final FSA fsa; |
| 41 |
try (InputStream is = new BufferedInputStream(Files.newInputStream(input))) { |
| 42 |
fsa = FSA.read(is); |
| 43 |
} |
| 44 |
|
| 45 |
printf("%-25s : %s", "FSA implementation", fsa.getClass().getName()); |
| 46 |
printf("%-25s : %s", "Compiled with flags", fsa.getFlags().toString()); |
| 47 |
|
| 48 |
final morfologik.fsa.builders.FSAInfo info = new morfologik.fsa.builders.FSAInfo(fsa); |
| 49 |
printf("%-25s : %,d", "Number of arcs (merged)", info.arcsCount); |
| 50 |
printf("%-25s : %,d", "Number of arcs (total)", info.arcsCountTotal); |
| 51 |
printf("%-25s : %,d", "Number of nodes", info.nodeCount); |
| 52 |
printf("%-25s : %,d", "Number of final states", info.finalStatesCount); |
| 53 |
printf(""); |
| 54 |
|
| 55 |
if (fsa instanceof FSA5) { |
| 56 |
FSA5 fsa5 = (FSA5) fsa; |
| 57 |
printf("%-25s : %d", "Goto length (GTL)", fsa5.gtl); |
| 58 |
printf("%-25s : %d", "Node extra data", fsa5.nodeDataLength); |
| 59 |
printf("%-25s : %s", "Annotation separator", byteAsChar(fsa5.annotation)); |
| 60 |
printf("%-25s : %s", "Filler character", byteAsChar(fsa5.filler)); |
| 61 |
} |
| 62 |
|
| 63 |
if (fsa instanceof CFSA) { |
| 64 |
CFSA cfsa = (CFSA) fsa; |
| 65 |
printf("%-25s : %d", "Goto length (GTL)", cfsa.gtl); |
| 66 |
printf("%-25s : %d", "Node extra data", cfsa.nodeDataLength); |
| 67 |
} |
| 68 |
|
| 69 |
if (fsa instanceof CFSA2) { |
| 70 |
CFSA2 cfsa2 = (CFSA2) fsa; |
| 71 |
|
| 72 |
byte [] labelMapping = cfsa2.labelMapping; |
| 73 |
if (labelMapping != null && labelMapping.length > 0) { |
| 74 |
printf("%-25s :", "Label mapping"); |
| 75 |
for (int i = 0; i < labelMapping.length; i++) { |
| 76 |
printf("%-25s %2d -> %s", "", i, byteAsChar(labelMapping[i])); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return ExitStatus.SUCCESS; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Convert a byte to an informative string. |
| 86 |
*/ |
| 87 |
static String byteAsChar(byte v) { |
| 88 |
int chr = v & 0xff; |
| 89 |
return String.format(Locale.ROOT, |
| 90 |
"%s (0x%02x)", |
| 91 |
(Character.isWhitespace(chr) || chr > 127) ? "[non-printable]" : Character.toString((char) chr), |
| 92 |
v & 0xFF); |
| 93 |
} |
| 94 |
|
| 95 |
public static void main(String[] args) { |
| 96 |
main(args, new FSAInfo()); |
| 97 |
} |
| 98 |
} |