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 | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 21 | Positions bool `kong:"optional,negatable,default=false,short='p',help='Print token offsets'"` |
| 22 | Tokens bool `kong:"optional,negatable,default=true,help="Print token surfaces""` |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 23 | } `kong:"cmd, help='Tokenize a text'"` |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // Main method for command line handling |
| 27 | func main() { |
| 28 | |
| 29 | // Parse command line parameters |
| 30 | parser := kong.Must( |
| 31 | &cli, |
| 32 | kong.Name("datok"), |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 33 | kong.Description("FSA based tokenizer"), |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 34 | kong.UsageOnError(), |
| 35 | ) |
| 36 | |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 37 | ctx, err := parser.Parse(os.Args[1:]) |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 38 | |
| 39 | parser.FatalIfErrorf(err) |
| 40 | |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 41 | if ctx.Command() == "convert" { |
| 42 | tok := datok.LoadFomaFile(cli.Convert.Foma) |
| 43 | if tok == nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 44 | log.Fatalln("Unable to load foma file") |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 45 | } |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 46 | if cli.Convert.DoubleArray { |
| 47 | dat := tok.ToDoubleArray() |
| 48 | _, err := dat.Save(cli.Convert.Tokenizer) |
| 49 | if err != nil { |
| 50 | log.Fatalln(err) |
| 51 | } |
| 52 | } else { |
| 53 | mat := tok.ToMatrix() |
| 54 | _, err := mat.Save(cli.Convert.Tokenizer) |
| 55 | if err != nil { |
| 56 | log.Fatalln(err) |
| 57 | } |
Akron | 7e269d4 | 2021-08-12 23:18:05 +0200 | [diff] [blame] | 58 | } |
| 59 | fmt.Println("File successfully converted.") |
| 60 | os.Exit(0) |
| 61 | } |
| 62 | |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 63 | // Load the Datok or Matrix file |
| 64 | dat := datok.LoadTokenizerFile(cli.Tokenize.Tokenizer) |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 65 | |
| 66 | // Unable to load the datok file |
| 67 | if dat == nil { |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 68 | log.Fatalln("Unable to load file") |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 69 | os.Exit(1) |
| 70 | } |
| 71 | |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 72 | // Create token writer based on the options defined |
| 73 | tw := datok.NewTokenWriterFromOptions(os.Stdout, cli.Tokenize.Positions) |
| 74 | |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 75 | // Program is running in a pipe |
| 76 | fileInfo, _ := os.Stdin.Stat() |
| 77 | if fileInfo.Mode()&os.ModeCharDevice == 0 { |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 78 | dat.TransduceTokenWriter(os.Stdin, tw) |
| 79 | tw.Flush() |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 80 | } |
| 81 | } |