blob: 6f556b5d52ca681f0e732596f35cae7d8e8cf333 [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 Diewald7364d1f2013-11-05 19:26:35 +000012our $VERSION = 0.01;
13
14# Merges foundry data to create indexer friendly documents
15# ndiewald, 2013/11/05
16
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
28--token|-t <foundry>[#<layer>] Define the default tokenization by specifying
29 the name of the foundry and optionally the name
30 of the layer. Defaults to OpenNLP#tokens.
31--skip|-s <foundry>[#<layer>] Skip specific foundries by specifying the name
32 or specific layers by defining the name
33 with a # in front of the foundry,
34 e.g. Mate#Morpho. Alternatively you can skip #ALL.
35 Can be set multiple times.
36--allow|-a <foundry>#<layer> Allow specific foundries and layers by defining them
37 combining the foundry name with a # and the layer name.
38--primary|-p Output primary data or not. Defaults to true.
39 Can be flagged using --no-primary as well.
40--human|-m Represent the data human friendly,
41 while the output defaults to JSON
42--pretty|-y Pretty print json output
43--gzip|-z Compress the output
44 (expects a defined output file)
45--log|-l The Log4perl log level, defaults to ERROR.
46--help|-h Print this document (optional)
47
48diewald@ids-mannheim.de, 2013/11/04
49
50EOHELP
51 exit(defined $_[0] ? $_[0] : 0);
52};
53
54# Options from the command line
55my ($input, $output, $text, $gzip, $log_level, @skip, $token_base, $primary, @allow, $pretty);
56GetOptions(
Nils Diewald092178e2013-11-26 16:18:48 +000057 'input|i=s' => \$input,
Nils Diewald7364d1f2013-11-05 19:26:35 +000058 'output|o=s' => \$output,
59 'human|m' => \$text,
60 'token|t=s' => \$token_base,
61 'gzip|z' => \$gzip,
62 'skip|s=s' => \@skip,
63 'log|l=s' => \$log_level,
64 'allow|a=s' => \@allow,
65 'primary|p!' => \$primary,
66 'pretty|y' => \$pretty,
67 'help|h' => sub { printhelp }
68);
69
70printhelp(1) if !$input || ($gzip && !$output);
71
72$log_level //= 'ERROR';
73
74my %skip;
75$skip{lc($_)} = 1 foreach @skip;
76
77Log::Log4perl->init({
78 'log4perl.rootLogger' => uc($log_level) . ', STDERR',
79 'log4perl.appender.STDERR' => 'Log::Log4perl::Appender::ScreenColoredLevels',
80 'log4perl.appender.STDERR.layout' => 'PatternLayout',
81 'log4perl.appender.STDERR.layout.ConversionPattern' => '[%r] %F %L %c - %m%n'
82});
83
84my $log = Log::Log4perl->get_logger('main');
85
86BEGIN {
87 $main::TIME = Benchmark->new;
88 $main::LAST_STOP = Benchmark->new;
89};
90
91sub stop_time {
92 my $new = Benchmark->new;
93 $log->trace(
94 'The code took: '.
95 timestr(timediff($new, $main::LAST_STOP)) .
96 ' (overall: ' . timestr(timediff($new, $main::TIME)) . ')'
97 );
98 $main::LAST_STOP = $new;
99};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000100
101# Call perl script/prepare_index.pl WPD/AAA/00001
102
Nils Diewald7364d1f2013-11-05 19:26:35 +0000103# Create and parse new document
104$input =~ s{([^/])$}{$1/};
105my $doc = KorAP::Document->new( path => $input );
106$doc->parse;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000107
Nils Diewald7364d1f2013-11-05 19:26:35 +0000108my ($token_base_foundry, $token_base_layer) = (qw/OpenNLP Tokens/);
109if ($token_base) {
110 ($token_base_foundry, $token_base_layer) = split /#/, $token_base;
111};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000112
Nils Diewald7364d1f2013-11-05 19:26:35 +0000113# Get tokenization
114my $tokens = KorAP::Tokenizer->new(
115 path => $doc->path,
116 doc => $doc,
117 foundry => $token_base_foundry,
118 layer => $token_base_layer,
119 name => 'tokens'
120);
121$tokens->parse;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000122
Nils Diewald7364d1f2013-11-05 19:26:35 +0000123my @layers;
Nils Diewald37e5b572013-11-20 20:26:03 +0000124push(@layers, ['Base', 'Sentences']);
Nils Diewald7364d1f2013-11-05 19:26:35 +0000125push(@layers, ['Base', 'Paragraphs']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000126
Nils Diewald7364d1f2013-11-05 19:26:35 +0000127# OpenNLP
128push(@layers, ['OpenNLP', 'Morpho']);
Nils Diewald7b847222014-04-23 11:14:00 +0000129push(@layers, ['OpenNLP', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000130
Nils Diewald7364d1f2013-11-05 19:26:35 +0000131# CoreNLP
132push(@layers, ['CoreNLP', 'NamedEntities', 'ne_dewac_175m_600']);
133push(@layers, ['CoreNLP', 'NamedEntities', 'ne_hgc_175m_600']);
Nils Diewald7b847222014-04-23 11:14:00 +0000134push(@layers, ['CoreNLP', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000135
Nils Diewald7364d1f2013-11-05 19:26:35 +0000136# Connexor
137push(@layers, ['Connexor', 'Morpho']);
138push(@layers, ['Connexor', 'Syntax']);
139push(@layers, ['Connexor', 'Phrase']);
Nils Diewald7b847222014-04-23 11:14:00 +0000140push(@layers, ['Connexor', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000141
Nils Diewald7364d1f2013-11-05 19:26:35 +0000142# TreeTagger
143push(@layers, ['TreeTagger', 'Morpho']);
Nils Diewald7b847222014-04-23 11:14:00 +0000144push(@layers, ['TreeTagger', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000145
Nils Diewald7364d1f2013-11-05 19:26:35 +0000146# Mate
147push(@layers, ['Mate', 'Morpho']);
148push(@layers, ['Mate', 'Dependency']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000149
Nils Diewald7364d1f2013-11-05 19:26:35 +0000150# XIP
151push(@layers, ['XIP', 'Morpho']);
152push(@layers, ['XIP', 'Constituency']);
153push(@layers, ['XIP', 'Dependency']);
Nils Diewald7b847222014-04-23 11:14:00 +0000154push(@layers, ['XIP', 'Sentences']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000155
156
Nils Diewald7364d1f2013-11-05 19:26:35 +0000157if ($skip{'#all'}) {
158 foreach (@allow) {
159 $tokens->add(split('#', $_));
160 stop_time;
161 };
162}
163else {
164 # Add to index file - respect skipping
165 foreach my $info (@layers) {
166 unless ($skip{lc($info->[0]) . '#' . lc($info->[1])}) {
167 $tokens->add(@$info);
168 stop_time;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000169 };
170 };
Nils Diewald7364d1f2013-11-05 19:26:35 +0000171};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000172
Nils Diewald7364d1f2013-11-05 19:26:35 +0000173my $file;
174
175my $print_text = $text ? $tokens->to_string($primary) : ($pretty ? $tokens->to_pretty_json($primary) : $tokens->to_json($primary));
176
177if ($output) {
178 if ($gzip) {
179 $file = IO::Compress::Gzip->new($output, Minimal => 1);
180 }
181 else {
182 $file = IO::File->new($output, "w");
Nils Diewald2db9ad02013-10-29 19:26:43 +0000183 };
184
Nils Diewald7364d1f2013-11-05 19:26:35 +0000185 $file->print($print_text);
186 $file->close;
187}
188else {
Nils Diewald7364d1f2013-11-05 19:26:35 +0000189 print $print_text . "\n";
Nils Diewald2db9ad02013-10-29 19:26:43 +0000190};
191
Nils Diewald7364d1f2013-11-05 19:26:35 +0000192stop_time;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000193
194__END__