| 1 |
package morfologik.tools; |
| 2 |
|
| 3 |
import java.io.BufferedOutputStream; |
| 4 |
import java.io.OutputStream; |
| 5 |
import java.nio.file.Files; |
| 6 |
import java.nio.file.Path; |
| 7 |
import java.util.Collections; |
| 8 |
import java.util.List; |
| 9 |
|
| 10 |
import morfologik.fsa.FSA; |
| 11 |
import morfologik.fsa.builders.FSABuilder; |
| 12 |
import morfologik.fsa.builders.FSASerializer; |
| 13 |
|
| 14 |
import com.beust.jcommander.Parameter; |
| 15 |
import com.beust.jcommander.Parameters; |
| 16 |
import com.beust.jcommander.ParametersDelegate; |
| 17 |
|
| 18 |
/** |
| 19 |
* Build finite state automaton out of text input. |
| 20 |
*/ |
| 21 |
@Parameters( |
| 22 |
commandNames = {"fsa_compile"}, |
| 23 |
commandDescription = "Builds finite state automaton from \\n-delimited input.") |
| 24 |
public class FSACompile extends CliTool { |
| 25 |
@Parameter( |
| 26 |
names = {"-i", "--input"}, |
| 27 |
description = "The input sequences (one sequence per \\n-delimited line).", |
| 28 |
required = true, |
| 29 |
validateValueWith = ValidateFileExists.class) |
| 30 |
private Path input; |
| 31 |
|
| 32 |
@Parameter( |
| 33 |
names = {"-o", "--output"}, |
| 34 |
description = "The output automaton file.", |
| 35 |
required = true, |
| 36 |
validateValueWith = ValidateParentDirExists.class) |
| 37 |
private Path output; |
| 38 |
|
| 39 |
@Parameter( |
| 40 |
names = {"-f", "--format"}, |
| 41 |
description = "Automaton serialization format.") |
| 42 |
private SerializationFormat format = SerializationFormat.FSA5; |
| 43 |
|
| 44 |
@ParametersDelegate |
| 45 |
private final BinaryInput binaryInput; |
| 46 |
|
| 47 |
FSACompile() { |
| 48 |
binaryInput = new BinaryInput(); |
| 49 |
} |
| 50 |
|
| 51 |
public FSACompile(Path input, |
| 52 |
Path output, |
| 53 |
SerializationFormat format, |
| 54 |
boolean acceptBom, |
| 55 |
boolean acceptCr, |
| 56 |
boolean ignoreEmpty) { |
| 57 |
this.input = checkNotNull(input); |
| 58 |
this.output = checkNotNull(output); |
| 59 |
this.binaryInput = new BinaryInput(acceptBom, acceptCr, ignoreEmpty); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public ExitStatus call() throws Exception { |
| 64 |
final List<byte[]> sequences = binaryInput.readBinarySequences(input, (byte) '\n'); |
| 65 |
|
| 66 |
Collections.sort(sequences, FSABuilder.LEXICAL_ORDERING); |
| 67 |
FSA fsa = FSABuilder.build(sequences); |
| 68 |
|
| 69 |
FSASerializer serializer = format.getSerializer(); |
| 70 |
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(output))) { |
| 71 |
serializer.serialize(fsa, os); |
| 72 |
} |
| 73 |
|
| 74 |
return ExitStatus.SUCCESS; |
| 75 |
} |
| 76 |
|
| 77 |
public static void main(String[] args) { |
| 78 |
main(args, new FSACompile()); |
| 79 |
} |
| 80 |
} |