blob: 22f4875ad26b01e868a275ce60549baeca7031a5 [file] [log] [blame]
Akron8e1d69b2021-08-12 17:38:49 +02001package main
2
3import (
Akron7e269d42021-08-12 23:18:05 +02004 "fmt"
Akron8e1d69b2021-08-12 17:38:49 +02005 "os"
6
Akron527c10c2021-08-13 01:45:18 +02007 "log"
8
Akron7f1097f2021-09-21 16:00:29 +02009 datok "github.com/KorAP/datok"
Akron8e1d69b2021-08-12 17:38:49 +020010 "github.com/alecthomas/kong"
11)
12
13var cli struct {
Akron7e269d42021-08-12 23:18:05 +020014 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'"`
Akron8e1d69b2021-08-12 17:38:49 +020021}
22
23// Main method for command line handling
24func 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
Akron7e269d42021-08-12 23:18:05 +020034 ctx, err := parser.Parse(os.Args[1:])
Akron8e1d69b2021-08-12 17:38:49 +020035
36 parser.FatalIfErrorf(err)
37
Akron7e269d42021-08-12 23:18:05 +020038 if ctx.Command() == "convert" {
39 tok := datok.LoadFomaFile(cli.Convert.Foma)
40 if tok == nil {
Akron527c10c2021-08-13 01:45:18 +020041 log.Fatalln("Unable to load foma file")
Akron7e269d42021-08-12 23:18:05 +020042 }
43 dat := tok.ToDoubleArray()
44 _, err := dat.Save(cli.Convert.Tokenizer)
45 if err != nil {
Akron527c10c2021-08-13 01:45:18 +020046 log.Fatalln(err)
Akron7e269d42021-08-12 23:18:05 +020047 }
48 fmt.Println("File successfully converted.")
49 os.Exit(0)
50 }
51
Akron8e1d69b2021-08-12 17:38:49 +020052 // Load the Datok file
Akron7e269d42021-08-12 23:18:05 +020053 dat := datok.LoadDatokFile(cli.Tokenize.Tokenizer)
Akron8e1d69b2021-08-12 17:38:49 +020054
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}