Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 4 | "fmt" |
Akron | 15bb13d | 2021-10-30 11:57:41 +0200 | [diff] [blame] | 5 | "io" |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 6 | "os" |
| 7 | |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 8 | "log" |
| 9 | |
Akron | 7f1097f | 2021-09-21 16:00:29 +0200 | [diff] [blame] | 10 | datok "github.com/KorAP/datok" |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 11 | "github.com/alecthomas/kong" |
| 12 | ) |
| 13 | |
| 14 | var cli struct { |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 15 | Convert struct { |
Akron | 15bb13d | 2021-10-30 11:57:41 +0200 | [diff] [blame] | 16 | Foma string `kong:"required,short='i',help='The Foma FST file'"` |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 17 | Tokenizer string `kong:"required,short='o',help='The Tokenizer file'"` |
| 18 | DoubleArray bool `kong:"optional,short='d',help='Convert to Double Array instead of Matrix representation'"` |
| 19 | } `kong:"cmd, help='Convert a foma file to a Matrix or Double Array tokenizer'"` |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 20 | Tokenize struct { |
Akron | fceddb6 | 2021-10-27 19:27:54 +0200 | [diff] [blame] | 21 | Tokenizer string `kong:"required,short='t',help='The Matrix or Double Array Tokenizer file'"` |
Akron | 15bb13d | 2021-10-30 11:57:41 +0200 | [diff] [blame] | 22 | Input string `kong:"required,arg='',type='existingfile',help='Input file to tokenize (use - for STDIN)'"` |
Akron | 2612f99 | 2021-10-27 20:12:15 +0200 | [diff] [blame] | 23 | Tokens bool `kong:"optional,negatable,default=true,help='Print token surfaces (defaults to ${default})'"` |
| 24 | Sentences bool `kong:"optional,negatable,default=true,help='Print sentence boundaries (defaults to ${default})'"` |
| 25 | TokenPositions bool `kong:"optional,default=false,short='p',help='Print token offsets (defaults to ${default})'"` |
| 26 | SentencePositions bool `kong:"optional,default=false,help='Print sentence offsets (defaults to ${default})'"` |
| 27 | NewlineAfterEOT bool `kong:"optional,default=false,help='Ignore newline after EOT (defaults to ${default})'"` |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 28 | } `kong:"cmd, help='Tokenize a text'"` |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // Main method for command line handling |
| 32 | func main() { |
| 33 | |
| 34 | // Parse command line parameters |
| 35 | parser := kong.Must( |
| 36 | &cli, |
| 37 | kong.Name("datok"), |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 38 | kong.Description("FSA based tokenizer"), |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 39 | kong.UsageOnError(), |
| 40 | ) |
| 41 | |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 42 | ctx, err := parser.Parse(os.Args[1:]) |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 43 | |
| 44 | parser.FatalIfErrorf(err) |
| 45 | |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 46 | if ctx.Command() == "convert" { |
| 47 | tok := datok.LoadFomaFile(cli.Convert.Foma) |
| 48 | if tok == nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 49 | log.Fatalln("Unable to load foma file") |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 50 | } |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 51 | if cli.Convert.DoubleArray { |
| 52 | dat := tok.ToDoubleArray() |
| 53 | _, err := dat.Save(cli.Convert.Tokenizer) |
| 54 | if err != nil { |
| 55 | log.Fatalln(err) |
| 56 | } |
| 57 | } else { |
| 58 | mat := tok.ToMatrix() |
| 59 | _, err := mat.Save(cli.Convert.Tokenizer) |
| 60 | if err != nil { |
| 61 | log.Fatalln(err) |
| 62 | } |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 63 | } |
| 64 | fmt.Println("File successfully converted.") |
| 65 | os.Exit(0) |
| 66 | } |
| 67 | |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 68 | // Load the Datok or Matrix file |
| 69 | dat := datok.LoadTokenizerFile(cli.Tokenize.Tokenizer) |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 70 | |
| 71 | // Unable to load the datok file |
| 72 | if dat == nil { |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 73 | log.Fatalln("Unable to load file") |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 74 | os.Exit(1) |
| 75 | } |
| 76 | |
Akron | 0f087ea | 2021-10-27 19:40:15 +0200 | [diff] [blame] | 77 | // Create flags parameter based on command line parameters |
| 78 | var flags datok.Bits |
| 79 | if cli.Tokenize.Tokens { |
| 80 | flags |= datok.TOKENS |
| 81 | } |
| 82 | |
| 83 | if cli.Tokenize.TokenPositions { |
| 84 | flags |= datok.TOKEN_POS |
| 85 | } |
| 86 | |
| 87 | if cli.Tokenize.Sentences { |
| 88 | flags |= datok.SENTENCES |
| 89 | } |
| 90 | |
| 91 | if cli.Tokenize.SentencePositions { |
| 92 | flags |= datok.SENTENCE_POS |
| 93 | } |
| 94 | |
| 95 | if cli.Tokenize.NewlineAfterEOT { |
| 96 | flags |= datok.NEWLINE_AFTER_EOT |
| 97 | } |
| 98 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 99 | // Create token writer based on the options defined |
Akron | 96fdc9b | 2021-10-27 21:11:17 +0200 | [diff] [blame] | 100 | tw := datok.NewTokenWriter(os.Stdout, flags) |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 101 | |
Akron | 15bb13d | 2021-10-30 11:57:41 +0200 | [diff] [blame] | 102 | var r io.Reader |
| 103 | |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 104 | // Program is running in a pipe |
Akron | 15bb13d | 2021-10-30 11:57:41 +0200 | [diff] [blame] | 105 | if cli.Tokenize.Input == "-" { |
| 106 | fileInfo, _ := os.Stdin.Stat() |
| 107 | if fileInfo.Mode()&os.ModeCharDevice == 0 { |
| 108 | r = os.Stdin |
| 109 | } else { |
| 110 | log.Fatalln("Unable to read from STDIN") |
| 111 | os.Exit(1) |
| 112 | return |
| 113 | } |
| 114 | } else { |
| 115 | f, err := os.Open(cli.Tokenize.Input) |
| 116 | if err != nil { |
| 117 | log.Fatalln(err) |
| 118 | os.Exit(1) |
| 119 | return |
| 120 | } |
| 121 | defer f.Close() |
| 122 | r = f |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 123 | } |
Akron | 15bb13d | 2021-10-30 11:57:41 +0200 | [diff] [blame] | 124 | |
| 125 | dat.TransduceTokenWriter(r, tw) |
| 126 | tw.Flush() |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 127 | } |