| 1 |
package morfologik.tools; |
| 2 |
|
| 3 |
import java.io.BufferedInputStream; |
| 4 |
import java.io.BufferedOutputStream; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.io.OutputStream; |
| 7 |
import java.nio.ByteBuffer; |
| 8 |
import java.nio.file.Files; |
| 9 |
import java.nio.file.Path; |
| 10 |
|
| 11 |
import morfologik.fsa.FSA; |
| 12 |
|
| 13 |
import com.beust.jcommander.Parameter; |
| 14 |
import com.beust.jcommander.Parameters; |
| 15 |
|
| 16 |
/** |
| 17 |
* Dump all byte sequences encoded in a finite state automaton. |
| 18 |
*/ |
| 19 |
@Parameters( |
| 20 |
commandNames = "fsa_decompile", |
| 21 |
commandDescription = "Dumps all sequences encoded in an automaton.") |
| 22 |
public class FSADecompile extends CliTool { |
| 23 |
@Parameter( |
| 24 |
names = {"-i", "--input"}, |
| 25 |
description = "The input automaton.", |
| 26 |
required = true, |
| 27 |
validateValueWith = ValidateFileExists.class) |
| 28 |
private Path input; |
| 29 |
|
| 30 |
@Parameter( |
| 31 |
names = {"-o", "--output"}, |
| 32 |
description = "The output file for byte sequences.", |
| 33 |
required = true, |
| 34 |
validateValueWith = ValidateParentDirExists.class) |
| 35 |
private Path output; |
| 36 |
|
| 37 |
FSADecompile() { |
| 38 |
} |
| 39 |
|
| 40 |
public FSADecompile(Path input, |
| 41 |
Path output) { |
| 42 |
this.input = checkNotNull(input); |
| 43 |
this.output = checkNotNull(output); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public ExitStatus call() throws Exception { |
| 48 |
final FSA fsa; |
| 49 |
try (InputStream is = new BufferedInputStream(Files.newInputStream(input))) { |
| 50 |
fsa = FSA.read(is); |
| 51 |
} |
| 52 |
|
| 53 |
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(output))) { |
| 54 |
for (ByteBuffer bb : fsa) { |
| 55 |
int o = bb.arrayOffset(); |
| 56 |
os.write(bb.array(), o + bb.position(), o + bb.remaining()); |
| 57 |
os.write('\n'); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return ExitStatus.SUCCESS; |
| 62 |
} |
| 63 |
|
| 64 |
public static void main(String[] args) { |
| 65 |
main(args, new FSADecompile()); |
| 66 |
} |
| 67 |
} |