Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 4 | use lib 'lib', '../lib'; |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 5 | use Getopt::Long; |
| 6 | use Benchmark qw/:hireswallclock/; |
| 7 | use IO::Compress::Gzip qw/$GzipError/; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 8 | use Log::Log4perl; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 9 | use KorAP::Document; |
| 10 | use KorAP::Tokenizer; |
| 11 | |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 12 | our $VERSION = 0.03; |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 13 | |
| 14 | # Merges foundry data to create indexer friendly documents |
Nils Diewald | 79a355c | 2014-10-30 00:52:36 +0000 | [diff] [blame] | 15 | # ndiewald, 2014/10/29 |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 16 | |
| 17 | sub printhelp { |
| 18 | print <<'EOHELP'; |
| 19 | |
| 20 | Merge foundry data based on a tokenization and create indexer friendly documents. |
| 21 | |
| 22 | Call: |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 23 | prepare_index.pl -z --input <directory> --output <filename> |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 24 | |
| 25 | --input|-i <directory> Directory of the document to index |
| 26 | --output|-o <filename> Document name for output (optional), |
| 27 | Writes to <STDOUT> by default |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 28 | --overwrite|-w Overwrite files that already exist |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 29 | --token|-t <foundry>[#<layer>] Define the default tokenization by specifying |
| 30 | the name of the foundry and optionally the name |
| 31 | of the layer. Defaults to OpenNLP#tokens. |
| 32 | --skip|-s <foundry>[#<layer>] Skip specific foundries by specifying the name |
| 33 | or specific layers by defining the name |
| 34 | with a # in front of the foundry, |
| 35 | e.g. Mate#Morpho. Alternatively you can skip #ALL. |
| 36 | Can be set multiple times. |
| 37 | --allow|-a <foundry>#<layer> Allow specific foundries and layers by defining them |
| 38 | combining the foundry name with a # and the layer name. |
| 39 | --primary|-p Output primary data or not. Defaults to true. |
| 40 | Can be flagged using --no-primary as well. |
| 41 | --human|-m Represent the data human friendly, |
| 42 | while the output defaults to JSON |
| 43 | --pretty|-y Pretty print json output |
| 44 | --gzip|-z Compress the output |
| 45 | (expects a defined output file) |
| 46 | --log|-l The Log4perl log level, defaults to ERROR. |
| 47 | --help|-h Print this document (optional) |
| 48 | |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 49 | diewald@ids-mannheim.de, 2014/11/05 |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 50 | |
| 51 | EOHELP |
| 52 | exit(defined $_[0] ? $_[0] : 0); |
| 53 | }; |
| 54 | |
| 55 | # Options from the command line |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 56 | my ($input, $output, $text, $gzip, $log_level, @skip, $token_base, |
| 57 | $primary, @allow, $pretty, $overwrite); |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 58 | GetOptions( |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 59 | 'input|i=s' => \$input, |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 60 | 'output|o=s' => \$output, |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 61 | 'overwrite|w' => \$overwrite, |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 62 | 'human|m' => \$text, |
| 63 | 'token|t=s' => \$token_base, |
| 64 | 'gzip|z' => \$gzip, |
| 65 | 'skip|s=s' => \@skip, |
| 66 | 'log|l=s' => \$log_level, |
| 67 | 'allow|a=s' => \@allow, |
| 68 | 'primary|p!' => \$primary, |
| 69 | 'pretty|y' => \$pretty, |
| 70 | 'help|h' => sub { printhelp } |
| 71 | ); |
| 72 | |
| 73 | printhelp(1) if !$input || ($gzip && !$output); |
| 74 | |
| 75 | $log_level //= 'ERROR'; |
| 76 | |
| 77 | my %skip; |
| 78 | $skip{lc($_)} = 1 foreach @skip; |
| 79 | |
| 80 | Log::Log4perl->init({ |
| 81 | 'log4perl.rootLogger' => uc($log_level) . ', STDERR', |
| 82 | 'log4perl.appender.STDERR' => 'Log::Log4perl::Appender::ScreenColoredLevels', |
| 83 | 'log4perl.appender.STDERR.layout' => 'PatternLayout', |
| 84 | 'log4perl.appender.STDERR.layout.ConversionPattern' => '[%r] %F %L %c - %m%n' |
| 85 | }); |
| 86 | |
| 87 | my $log = Log::Log4perl->get_logger('main'); |
| 88 | |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 89 | # Ignore processing |
| 90 | if (!$overwrite && $output && -e $output) { |
| 91 | $log->trace($output . ' already exists'); |
| 92 | exit(0); |
| 93 | }; |
| 94 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 95 | BEGIN { |
| 96 | $main::TIME = Benchmark->new; |
| 97 | $main::LAST_STOP = Benchmark->new; |
| 98 | }; |
| 99 | |
| 100 | sub stop_time { |
| 101 | my $new = Benchmark->new; |
| 102 | $log->trace( |
| 103 | 'The code took: '. |
| 104 | timestr(timediff($new, $main::LAST_STOP)) . |
| 105 | ' (overall: ' . timestr(timediff($new, $main::TIME)) . ')' |
| 106 | ); |
| 107 | $main::LAST_STOP = $new; |
| 108 | }; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 109 | |
| 110 | # Call perl script/prepare_index.pl WPD/AAA/00001 |
| 111 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 112 | # Create and parse new document |
| 113 | $input =~ s{([^/])$}{$1/}; |
| 114 | my $doc = KorAP::Document->new( path => $input ); |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 115 | |
| 116 | unless ($doc->parse) { |
Nils Diewald | 34926b4 | 2014-11-05 18:22:17 +0000 | [diff] [blame] | 117 | $log->warn($output . " can't be processed - no document data"); |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 118 | exit(0); |
| 119 | }; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 120 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 121 | my ($token_base_foundry, $token_base_layer) = (qw/OpenNLP Tokens/); |
| 122 | if ($token_base) { |
| 123 | ($token_base_foundry, $token_base_layer) = split /#/, $token_base; |
| 124 | }; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 125 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 126 | # Get tokenization |
| 127 | my $tokens = KorAP::Tokenizer->new( |
| 128 | path => $doc->path, |
| 129 | doc => $doc, |
| 130 | foundry => $token_base_foundry, |
| 131 | layer => $token_base_layer, |
| 132 | name => 'tokens' |
| 133 | ); |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 134 | |
| 135 | # Unable to process base tokenization |
| 136 | unless ($tokens->parse) { |
Nils Diewald | 34926b4 | 2014-11-05 18:22:17 +0000 | [diff] [blame] | 137 | $log->error($output . " can't be processed - no base tokenization"); |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 138 | exit(0); |
| 139 | }; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 140 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 141 | my @layers; |
Nils Diewald | 37e5b57 | 2013-11-20 20:26:03 +0000 | [diff] [blame] | 142 | push(@layers, ['Base', 'Sentences']); |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 143 | push(@layers, ['Base', 'Paragraphs']); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 144 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 145 | # OpenNLP |
| 146 | push(@layers, ['OpenNLP', 'Morpho']); |
Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 147 | push(@layers, ['OpenNLP', 'Sentences']); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 148 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 149 | # CoreNLP |
Nils Diewald | 7ed12c8 | 2014-10-31 17:51:19 +0000 | [diff] [blame] | 150 | push(@layers, ['CoreNLP', 'NamedEntities']); |
Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 151 | push(@layers, ['CoreNLP', 'Sentences']); |
Nils Diewald | 7ed12c8 | 2014-10-31 17:51:19 +0000 | [diff] [blame] | 152 | push(@layers, ['CoreNLP', 'Morpho']); |
| 153 | push(@layers, ['CoreNLP', 'Constituency']); |
| 154 | |
| 155 | # Glemm |
| 156 | push(@layers, ['Glemm', 'Morpho']); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 157 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 158 | # Connexor |
| 159 | push(@layers, ['Connexor', 'Morpho']); |
| 160 | push(@layers, ['Connexor', 'Syntax']); |
| 161 | push(@layers, ['Connexor', 'Phrase']); |
Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 162 | push(@layers, ['Connexor', 'Sentences']); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 163 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 164 | # TreeTagger |
| 165 | push(@layers, ['TreeTagger', 'Morpho']); |
Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 166 | push(@layers, ['TreeTagger', 'Sentences']); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 167 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 168 | # Mate |
| 169 | push(@layers, ['Mate', 'Morpho']); |
Nils Diewald | 7ed12c8 | 2014-10-31 17:51:19 +0000 | [diff] [blame] | 170 | # push(@layers, ['Mate', 'Dependency']); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 171 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 172 | # XIP |
| 173 | push(@layers, ['XIP', 'Morpho']); |
| 174 | push(@layers, ['XIP', 'Constituency']); |
Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 175 | push(@layers, ['XIP', 'Sentences']); |
Nils Diewald | 7ed12c8 | 2014-10-31 17:51:19 +0000 | [diff] [blame] | 176 | # push(@layers, ['XIP', 'Dependency']); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 177 | |
| 178 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 179 | if ($skip{'#all'}) { |
| 180 | foreach (@allow) { |
| 181 | $tokens->add(split('#', $_)); |
| 182 | stop_time; |
| 183 | }; |
| 184 | } |
| 185 | else { |
| 186 | # Add to index file - respect skipping |
| 187 | foreach my $info (@layers) { |
| 188 | unless ($skip{lc($info->[0]) . '#' . lc($info->[1])}) { |
| 189 | $tokens->add(@$info); |
| 190 | stop_time; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 191 | }; |
| 192 | }; |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 193 | }; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 194 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 195 | my $file; |
| 196 | |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 197 | my $print_text = $text ? $tokens->to_string($primary) : |
| 198 | ($pretty ? $tokens->to_pretty_json($primary) : $tokens->to_json($primary)); |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 199 | |
| 200 | if ($output) { |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 201 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 202 | if ($gzip) { |
| 203 | $file = IO::Compress::Gzip->new($output, Minimal => 1); |
| 204 | } |
| 205 | else { |
| 206 | $file = IO::File->new($output, "w"); |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 209 | $file->print($print_text); |
| 210 | $file->close; |
| 211 | } |
Nils Diewald | 5b4865f | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 212 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 213 | else { |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 214 | print $print_text . "\n"; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 217 | stop_time; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 218 | |
| 219 | __END__ |