blob: 7b048cf6959a928984ae007471fd296760c6a0c6 [file] [log] [blame]
Nils Diewald092178e2013-11-26 16:18:48 +00001#!/usr/bin/env perl
2use strict;
3use warnings;
4use FindBin;
Nils Diewald092178e2013-11-26 16:18:48 +00005use Getopt::Long;
6use Directory::Iterator;
7
8my $local = $FindBin::Bin;
9
Akron93d620e2016-02-05 19:40:05 +010010# Changes
11# 2013/11/25
12# - Initial release
13#
14# 2016/02/04
15# - Rename to korapxml2krill_dir
Akron069bd712016-02-12 19:09:06 +010016#
17# 2016/02/12
18# - Support overwrite
Akron93d620e2016-02-05 19:40:05 +010019
Nils Diewald092178e2013-11-26 16:18:48 +000020sub printhelp {
21 print <<'EOHELP';
22
23Merge foundry data based on a tokenization and create indexer friendly documents
24for whole directories.
25
26Call:
Akron93d620e2016-02-05 19:40:05 +010027korapxml2krill_dir -z --input <directory> --output <directory>
Nils Diewald092178e2013-11-26 16:18:48 +000028
Akron069bd712016-02-12 19:09:06 +010029 --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 Diewald092178e2013-11-26 16:18:48 +000051
Akron069bd712016-02-12 19:09:06 +010052diewald@ids-mannheim.de, 2016/02/12
Nils Diewald092178e2013-11-26 16:18:48 +000053
54EOHELP
55
56 exit(defined $_[0] ? $_[0] : 0);
57};
58
Akron069bd712016-02-12 19:09:06 +010059my ($input, $output, $text, $gzip, $log_level, @skip,
60 $token_base, $primary, @allow, $pretty, $overwrite);
Nils Diewald092178e2013-11-26 16:18:48 +000061GetOptions(
62 'input|i=s' => \$input,
63 'output|o=s' => \$output,
64 'human|m' => \$text,
Akron069bd712016-02-12 19:09:06 +010065 'overwrite|w' => \$overwrite,
Nils Diewald092178e2013-11-26 16:18:48 +000066 '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
76printhelp(1) if !$input || !$output;
77
78
79sub write_file {
80 my $anno = shift;
81 my $file = $anno;
82 $file =~ s/^?\/?$input//;
83 $file =~ tr/\//-/;
84 $file =~ s{^-+}{};
85
Akron93d620e2016-02-05 19:40:05 +010086 my $call = 'perl ' . $local . '/korapxml2krill -i ' . $anno . ' -o ' . $output . '/' . $file . '.json';
Nils Diewald092178e2013-11-26 16:18:48 +000087 $call .= '.gz -z' if $gzip;
88 $call .= ' -m' if $text;
Akron069bd712016-02-12 19:09:06 +010089 $call .= ' -w' if $overwrite;
Akrona9d47722016-02-07 23:54:15 +010090 $call .= ' -t ' . $token_base if $token_base;
Nils Diewald092178e2013-11-26 16:18:48 +000091 $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;
Akrona98a14c2016-02-12 16:28:39 +010096 print "$file ";
Nils Diewald092178e2013-11-26 16:18:48 +000097 system($call);
Akronfd0707e2016-02-11 22:13:36 +010098 print "\n";
Nils Diewald092178e2013-11-26 16:18:48 +000099};
100
101
102my $it = Directory::Iterator->new($input);
Akronfd0707e2016-02-11 22:13:36 +0100103my @dirs;
Nils Diewald092178e2013-11-26 16:18:48 +0000104my $dir;
105while (1) {
106
107 if (!$it->is_directory && ($dir = $it->get) && $dir =~ s{/data\.xml$}{}) {
Akronfd0707e2016-02-11 22:13:36 +0100108 push @dirs, $dir;
Nils Diewald092178e2013-11-26 16:18:48 +0000109 $it->prune;
110 };
111 last unless $it->next;
112};
113
Akronfd0707e2016-02-11 22:13:36 +0100114my $count = scalar @dirs;
115for (my $i = 0; $i < $count; $i++) {
Akrona98a14c2016-02-12 16:28:39 +0100116 print 'Convert [' . ($i + 1) . "/$count] ";
Akronfd0707e2016-02-11 22:13:36 +0100117 write_file($dirs[$i]);
118};
119
Nils Diewald092178e2013-11-26 16:18:48 +0000120
121__END__