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 | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 5 | "os" |
| 6 | |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 7 | "log" |
| 8 | |
Akron | 7f1097f | 2021-09-21 16:00:29 +0200 | [diff] [blame] | 9 | datok "github.com/KorAP/datok" |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 10 | "github.com/alecthomas/kong" |
| 11 | ) |
| 12 | |
| 13 | var cli struct { |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 14 | Convert struct { |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 15 | Foma string `kong:"required,short='i',help='The Foma file'"` |
| 16 | Tokenizer string `kong:"required,short='o',help='The Tokenizer file'"` |
| 17 | DoubleArray bool `kong:"optional,short='d',help='Convert to Double Array instead of Matrix representation'"` |
| 18 | } `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] | 19 | Tokenize struct { |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 20 | Tokenizer string `kong:"required,short='t',help='The Matrix or Double Array Tokenizer file'"` |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 21 | } `kong:"cmd, help='Tokenize a text'"` |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | // Main method for command line handling |
| 25 | func main() { |
| 26 | |
| 27 | // Parse command line parameters |
| 28 | parser := kong.Must( |
| 29 | &cli, |
| 30 | kong.Name("datok"), |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 31 | kong.Description("FSA based tokenizer"), |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 32 | kong.UsageOnError(), |
| 33 | ) |
| 34 | |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 35 | ctx, err := parser.Parse(os.Args[1:]) |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 36 | |
| 37 | parser.FatalIfErrorf(err) |
| 38 | |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 39 | if ctx.Command() == "convert" { |
| 40 | tok := datok.LoadFomaFile(cli.Convert.Foma) |
| 41 | if tok == nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 42 | log.Fatalln("Unable to load foma file") |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 43 | } |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 44 | if cli.Convert.DoubleArray { |
| 45 | dat := tok.ToDoubleArray() |
| 46 | _, err := dat.Save(cli.Convert.Tokenizer) |
| 47 | if err != nil { |
| 48 | log.Fatalln(err) |
| 49 | } |
| 50 | } else { |
| 51 | mat := tok.ToMatrix() |
| 52 | _, err := mat.Save(cli.Convert.Tokenizer) |
| 53 | if err != nil { |
| 54 | log.Fatalln(err) |
| 55 | } |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 56 | } |
| 57 | fmt.Println("File successfully converted.") |
| 58 | os.Exit(0) |
| 59 | } |
| 60 | |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 61 | // Load the Datok or Matrix file |
| 62 | dat := datok.LoadTokenizerFile(cli.Tokenize.Tokenizer) |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 63 | |
| 64 | // Unable to load the datok file |
| 65 | if dat == nil { |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 66 | log.Fatalln("Unable to load file") |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 67 | os.Exit(1) |
| 68 | } |
| 69 | |
| 70 | // Program is running in a pipe |
| 71 | fileInfo, _ := os.Stdin.Stat() |
| 72 | if fileInfo.Mode()&os.ModeCharDevice == 0 { |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 73 | // Transduce from STDIN and write to STDOUT |
| 74 | dat.Transduce(os.Stdin, os.Stdout) |
| 75 | } |
| 76 | } |