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