blob: 4b87ddb3a3992a30c4168821395ee47c27a962d5 [file] [log] [blame]
Nils Diewald2db9ad02013-10-29 19:26:43 +00001#!/usr/bin/env perl
2use strict;
3use warnings;
Nils Diewald2db9ad02013-10-29 19:26:43 +00004use lib 'lib', '../lib';
Nils Diewald7364d1f2013-11-05 19:26:35 +00005use Getopt::Long;
6use Benchmark qw/:hireswallclock/;
7use IO::Compress::Gzip qw/$GzipError/;
Nils Diewald2db9ad02013-10-29 19:26:43 +00008use Log::Log4perl;
Nils Diewald2db9ad02013-10-29 19:26:43 +00009use KorAP::Document;
10use KorAP::Tokenizer;
11
Nils Diewald5b4865f2014-11-05 18:20:50 +000012our $VERSION = 0.03;
Nils Diewald7364d1f2013-11-05 19:26:35 +000013
14# Merges foundry data to create indexer friendly documents
Nils Diewald79a355c2014-10-30 00:52:36 +000015# ndiewald, 2014/10/29
Nils Diewald7364d1f2013-11-05 19:26:35 +000016
17sub printhelp {
18 print <<'EOHELP';
19
20Merge foundry data based on a tokenization and create indexer friendly documents.
21
22Call:
Nils Diewald092178e2013-11-26 16:18:48 +000023prepare_index.pl -z --input <directory> --output <filename>
Nils Diewald7364d1f2013-11-05 19:26:35 +000024
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 Diewald5b4865f2014-11-05 18:20:50 +000028--overwrite|-w Overwrite files that already exist
Nils Diewald7364d1f2013-11-05 19:26:35 +000029--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 Diewald5b4865f2014-11-05 18:20:50 +000049diewald@ids-mannheim.de, 2014/11/05
Nils Diewald7364d1f2013-11-05 19:26:35 +000050
51EOHELP
52 exit(defined $_[0] ? $_[0] : 0);
53};
54
55# Options from the command line
Nils Diewald5b4865f2014-11-05 18:20:50 +000056my ($input, $output, $text, $gzip, $log_level, @skip, $token_base,
57 $primary, @allow, $pretty, $overwrite);
Nils Diewald7364d1f2013-11-05 19:26:35 +000058GetOptions(
Nils Diewald092178e2013-11-26 16:18:48 +000059 'input|i=s' => \$input,
Nils Diewald7364d1f2013-11-05 19:26:35 +000060 'output|o=s' => \$output,
Nils Diewald5b4865f2014-11-05 18:20:50 +000061 'overwrite|w' => \$overwrite,
Nils Diewald7364d1f2013-11-05 19:26:35 +000062 '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
73printhelp(1) if !$input || ($gzip && !$output);
74
75$log_level //= 'ERROR';
76
77my %skip;
78$skip{lc($_)} = 1 foreach @skip;
79
80Log::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
87my $log = Log::Log4perl->get_logger('main');
88
Nils Diewald5b4865f2014-11-05 18:20:50 +000089# Ignore processing
90if (!$overwrite && $output && -e $output) {
91 $log->trace($output . ' already exists');
92 exit(0);
93};
94
Nils Diewald7364d1f2013-11-05 19:26:35 +000095BEGIN {
96 $main::TIME = Benchmark->new;
97 $main::LAST_STOP = Benchmark->new;
98};
99
100sub 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 Diewald2db9ad02013-10-29 19:26:43 +0000109
110# Call perl script/prepare_index.pl WPD/AAA/00001
111
Nils Diewald7364d1f2013-11-05 19:26:35 +0000112# Create and parse new document
113$input =~ s{([^/])$}{$1/};
114my $doc = KorAP::Document->new( path => $input );
Nils Diewald5b4865f2014-11-05 18:20:50 +0000115
116unless ($doc->parse) {
Nils Diewald34926b42014-11-05 18:22:17 +0000117 $log->warn($output . " can't be processed - no document data");
Nils Diewald5b4865f2014-11-05 18:20:50 +0000118 exit(0);
119};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000120
Nils Diewald7364d1f2013-11-05 19:26:35 +0000121my ($token_base_foundry, $token_base_layer) = (qw/OpenNLP Tokens/);
122if ($token_base) {
123 ($token_base_foundry, $token_base_layer) = split /#/, $token_base;
124};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000125
Nils Diewald7364d1f2013-11-05 19:26:35 +0000126# Get tokenization
127my $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 Diewald5b4865f2014-11-05 18:20:50 +0000134
135# Unable to process base tokenization
136unless ($tokens->parse) {
Nils Diewald34926b42014-11-05 18:22:17 +0000137 $log->error($output . " can't be processed - no base tokenization");
Nils Diewald5b4865f2014-11-05 18:20:50 +0000138 exit(0);
139};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000140
Nils Diewald7364d1f2013-11-05 19:26:35 +0000141my @layers;
Nils Diewald37e5b572013-11-20 20:26:03 +0000142push(@layers, ['Base', 'Sentences']);
Nils Diewald7364d1f2013-11-05 19:26:35 +0000143push(@layers, ['Base', 'Paragraphs']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000144
Nils Diewald7364d1f2013-11-05 19:26:35 +0000145# OpenNLP
146push(@layers, ['OpenNLP', 'Morpho']);
Nils Diewald7b847222014-04-23 11:14:00 +0000147push(@layers, ['OpenNLP', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000148
Nils Diewald7364d1f2013-11-05 19:26:35 +0000149# CoreNLP
Nils Diewald7ed12c82014-10-31 17:51:19 +0000150push(@layers, ['CoreNLP', 'NamedEntities']);
Nils Diewald7b847222014-04-23 11:14:00 +0000151push(@layers, ['CoreNLP', 'Sentences']);
Nils Diewald7ed12c82014-10-31 17:51:19 +0000152push(@layers, ['CoreNLP', 'Morpho']);
153push(@layers, ['CoreNLP', 'Constituency']);
154
155# Glemm
156push(@layers, ['Glemm', 'Morpho']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000157
Nils Diewald7364d1f2013-11-05 19:26:35 +0000158# Connexor
159push(@layers, ['Connexor', 'Morpho']);
160push(@layers, ['Connexor', 'Syntax']);
161push(@layers, ['Connexor', 'Phrase']);
Nils Diewald7b847222014-04-23 11:14:00 +0000162push(@layers, ['Connexor', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000163
Nils Diewald7364d1f2013-11-05 19:26:35 +0000164# TreeTagger
165push(@layers, ['TreeTagger', 'Morpho']);
Nils Diewald7b847222014-04-23 11:14:00 +0000166push(@layers, ['TreeTagger', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000167
Nils Diewald7364d1f2013-11-05 19:26:35 +0000168# Mate
169push(@layers, ['Mate', 'Morpho']);
Nils Diewald7ed12c82014-10-31 17:51:19 +0000170# push(@layers, ['Mate', 'Dependency']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000171
Nils Diewald7364d1f2013-11-05 19:26:35 +0000172# XIP
173push(@layers, ['XIP', 'Morpho']);
174push(@layers, ['XIP', 'Constituency']);
Nils Diewald7b847222014-04-23 11:14:00 +0000175push(@layers, ['XIP', 'Sentences']);
Nils Diewald7ed12c82014-10-31 17:51:19 +0000176# push(@layers, ['XIP', 'Dependency']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000177
178
Nils Diewald7364d1f2013-11-05 19:26:35 +0000179if ($skip{'#all'}) {
180 foreach (@allow) {
181 $tokens->add(split('#', $_));
182 stop_time;
183 };
184}
185else {
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 Diewald2db9ad02013-10-29 19:26:43 +0000191 };
192 };
Nils Diewald7364d1f2013-11-05 19:26:35 +0000193};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000194
Nils Diewald7364d1f2013-11-05 19:26:35 +0000195my $file;
196
Nils Diewald5b4865f2014-11-05 18:20:50 +0000197my $print_text = $text ? $tokens->to_string($primary) :
198 ($pretty ? $tokens->to_pretty_json($primary) : $tokens->to_json($primary));
Nils Diewald7364d1f2013-11-05 19:26:35 +0000199
200if ($output) {
Nils Diewald5b4865f2014-11-05 18:20:50 +0000201
Nils Diewald7364d1f2013-11-05 19:26:35 +0000202 if ($gzip) {
203 $file = IO::Compress::Gzip->new($output, Minimal => 1);
204 }
205 else {
206 $file = IO::File->new($output, "w");
Nils Diewald2db9ad02013-10-29 19:26:43 +0000207 };
208
Nils Diewald7364d1f2013-11-05 19:26:35 +0000209 $file->print($print_text);
210 $file->close;
211}
Nils Diewald5b4865f2014-11-05 18:20:50 +0000212
Nils Diewald7364d1f2013-11-05 19:26:35 +0000213else {
Nils Diewald7364d1f2013-11-05 19:26:35 +0000214 print $print_text . "\n";
Nils Diewald2db9ad02013-10-29 19:26:43 +0000215};
216
Nils Diewald7364d1f2013-11-05 19:26:35 +0000217stop_time;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000218
219__END__