blob: 17a547012c666cc5ae5aec97198cd01f3fc8a90b [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 Diewald2db9ad02013-10-29 19:26:43 +0000124
Nils Diewald7364d1f2013-11-05 19:26:35 +0000125# Base information
Nils Diewald37e5b572013-11-20 20:26:03 +0000126push(@layers, ['Base', 'Sentences']);
Nils Diewald7364d1f2013-11-05 19:26:35 +0000127push(@layers, ['Base', 'Paragraphs']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000128
Nils Diewald7364d1f2013-11-05 19:26:35 +0000129# OpenNLP
130push(@layers, ['OpenNLP', 'Morpho']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000131
Nils Diewald7364d1f2013-11-05 19:26:35 +0000132# CoreNLP
133push(@layers, ['CoreNLP', 'NamedEntities', 'ne_dewac_175m_600']);
134push(@layers, ['CoreNLP', 'NamedEntities', 'ne_hgc_175m_600']);
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 Diewald2db9ad02013-10-29 19:26:43 +0000140
Nils Diewald7364d1f2013-11-05 19:26:35 +0000141# TreeTagger
142push(@layers, ['TreeTagger', 'Morpho']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000143
Nils Diewald7364d1f2013-11-05 19:26:35 +0000144# Mate
145push(@layers, ['Mate', 'Morpho']);
146push(@layers, ['Mate', 'Dependency']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000147
Nils Diewald7364d1f2013-11-05 19:26:35 +0000148# XIP
149push(@layers, ['XIP', 'Morpho']);
150push(@layers, ['XIP', 'Constituency']);
151push(@layers, ['XIP', 'Dependency']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000152
153
Nils Diewald7364d1f2013-11-05 19:26:35 +0000154if ($skip{'#all'}) {
155 foreach (@allow) {
156 $tokens->add(split('#', $_));
157 stop_time;
158 };
159}
160else {
161 # Add to index file - respect skipping
162 foreach my $info (@layers) {
163 unless ($skip{lc($info->[0]) . '#' . lc($info->[1])}) {
164 $tokens->add(@$info);
165 stop_time;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000166 };
167 };
Nils Diewald7364d1f2013-11-05 19:26:35 +0000168};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000169
Nils Diewald7364d1f2013-11-05 19:26:35 +0000170my $file;
171
172my $print_text = $text ? $tokens->to_string($primary) : ($pretty ? $tokens->to_pretty_json($primary) : $tokens->to_json($primary));
173
174if ($output) {
175 if ($gzip) {
176 $file = IO::Compress::Gzip->new($output, Minimal => 1);
177 }
178 else {
179 $file = IO::File->new($output, "w");
Nils Diewald2db9ad02013-10-29 19:26:43 +0000180 };
181
Nils Diewald7364d1f2013-11-05 19:26:35 +0000182# binmode $file, ':utf8';
Nils Diewald2db9ad02013-10-29 19:26:43 +0000183
Nils Diewald7364d1f2013-11-05 19:26:35 +0000184 $file->print($print_text);
185 $file->close;
186}
187else {
188# binmode STDOUT, ':utf8';
189 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__