Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use FindBin; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 5 | use Getopt::Long; |
| 6 | use Directory::Iterator; |
| 7 | |
| 8 | my $local = $FindBin::Bin; |
| 9 | |
Akron | 9a04c71 | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 10 | # Changes |
| 11 | # 2013/11/25 |
| 12 | # - Initial release |
| 13 | # |
| 14 | # 2016/02/04 |
| 15 | # - Rename to korapxml2krill_dir |
Akron | 9078bb9 | 2016-02-12 19:09:06 +0100 | [diff] [blame^] | 16 | # |
| 17 | # 2016/02/12 |
| 18 | # - Support overwrite |
Akron | 9a04c71 | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 19 | |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 20 | sub printhelp { |
| 21 | print <<'EOHELP'; |
| 22 | |
| 23 | Merge foundry data based on a tokenization and create indexer friendly documents |
| 24 | for whole directories. |
| 25 | |
| 26 | Call: |
Akron | 9a04c71 | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 27 | korapxml2krill_dir -z --input <directory> --output <directory> |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 28 | |
Akron | 9078bb9 | 2016-02-12 19:09:06 +0100 | [diff] [blame^] | 29 | --input|-i <directory> Directory of documents to index |
| 30 | --output|-o <directory> Name of output folder |
| 31 | --overwrite|-w Overwrite files that already exist |
| 32 | --token|-t <foundry>[#<layer>] Define the default tokenization by specifying |
| 33 | the name of the foundry and optionally the name |
| 34 | of the layer. Defaults to OpenNLP#tokens. |
| 35 | --skip|-s <foundry>[#<layer>] Skip specific foundries by specifying the name |
| 36 | or specific layers by defining the name |
| 37 | with a # in front of the foundry, |
| 38 | e.g. Mate#Morpho. Alternatively you can skip #ALL. |
| 39 | Can be set multiple times. |
| 40 | --allow|-a <foundry>#<layer> Allow specific foundries and layers by defining them |
| 41 | combining the foundry name with a # and the layer name. |
| 42 | --primary|-p Output primary data or not. Defaults to true. |
| 43 | Can be flagged using --no-primary as well. |
| 44 | --human|-m Represent the data human friendly, |
| 45 | while the output defaults to JSON |
| 46 | --pretty|-y Pretty print json output |
| 47 | --gzip|-z Compress the output |
| 48 | (expects a defined output file) |
| 49 | --log|-l The Log4perl log level, defaults to ERROR. |
| 50 | --help|-h Print this document (optional) |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 51 | |
Akron | 9078bb9 | 2016-02-12 19:09:06 +0100 | [diff] [blame^] | 52 | diewald@ids-mannheim.de, 2016/02/12 |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 53 | |
| 54 | EOHELP |
| 55 | |
| 56 | exit(defined $_[0] ? $_[0] : 0); |
| 57 | }; |
| 58 | |
Akron | 9078bb9 | 2016-02-12 19:09:06 +0100 | [diff] [blame^] | 59 | my ($input, $output, $text, $gzip, $log_level, @skip, |
| 60 | $token_base, $primary, @allow, $pretty, $overwrite); |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 61 | GetOptions( |
| 62 | 'input|i=s' => \$input, |
| 63 | 'output|o=s' => \$output, |
| 64 | 'human|m' => \$text, |
Akron | 9078bb9 | 2016-02-12 19:09:06 +0100 | [diff] [blame^] | 65 | 'overwrite|w' => \$overwrite, |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 66 | 'token|t=s' => \$token_base, |
| 67 | 'gzip|z' => \$gzip, |
| 68 | 'skip|s=s' => \@skip, |
| 69 | 'log|l=s' => \$log_level, |
| 70 | 'allow|a=s' => \@allow, |
| 71 | 'primary|p!' => \$primary, |
| 72 | 'pretty|y' => \$pretty, |
| 73 | 'help|h' => sub { printhelp } |
| 74 | ); |
| 75 | |
| 76 | printhelp(1) if !$input || !$output; |
| 77 | |
| 78 | |
| 79 | sub write_file { |
| 80 | my $anno = shift; |
| 81 | my $file = $anno; |
| 82 | $file =~ s/^?\/?$input//; |
| 83 | $file =~ tr/\//-/; |
| 84 | $file =~ s{^-+}{}; |
| 85 | |
Akron | 9a04c71 | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 86 | my $call = 'perl ' . $local . '/korapxml2krill -i ' . $anno . ' -o ' . $output . '/' . $file . '.json'; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 87 | $call .= '.gz -z' if $gzip; |
| 88 | $call .= ' -m' if $text; |
Akron | 9078bb9 | 2016-02-12 19:09:06 +0100 | [diff] [blame^] | 89 | $call .= ' -w' if $overwrite; |
Akron | 508c18e | 2016-02-07 23:54:15 +0100 | [diff] [blame] | 90 | $call .= ' -t ' . $token_base if $token_base; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 91 | $call .= ' -l ' . $log_level if $log_level; |
| 92 | $call .= ' --no-primary ' if $primary; |
| 93 | $call .= ' -y ' . $pretty if $pretty; |
| 94 | $call .= ' -a ' . $_ foreach @allow; |
| 95 | $call .= ' -s ' . $_ foreach @skip; |
Akron | 05df218 | 2016-02-12 16:28:39 +0100 | [diff] [blame] | 96 | print "$file "; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 97 | system($call); |
Akron | 0fe59d7 | 2016-02-11 22:13:36 +0100 | [diff] [blame] | 98 | print "\n"; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | |
| 102 | my $it = Directory::Iterator->new($input); |
Akron | 0fe59d7 | 2016-02-11 22:13:36 +0100 | [diff] [blame] | 103 | my @dirs; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 104 | my $dir; |
| 105 | while (1) { |
| 106 | |
| 107 | if (!$it->is_directory && ($dir = $it->get) && $dir =~ s{/data\.xml$}{}) { |
Akron | 0fe59d7 | 2016-02-11 22:13:36 +0100 | [diff] [blame] | 108 | push @dirs, $dir; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 109 | $it->prune; |
| 110 | }; |
| 111 | last unless $it->next; |
| 112 | }; |
| 113 | |
Akron | 0fe59d7 | 2016-02-11 22:13:36 +0100 | [diff] [blame] | 114 | my $count = scalar @dirs; |
| 115 | for (my $i = 0; $i < $count; $i++) { |
Akron | 05df218 | 2016-02-12 16:28:39 +0100 | [diff] [blame] | 116 | print 'Convert [' . ($i + 1) . "/$count] "; |
Akron | 0fe59d7 | 2016-02-11 22:13:36 +0100 | [diff] [blame] | 117 | write_file($dirs[$i]); |
| 118 | }; |
| 119 | |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 120 | |
| 121 | __END__ |