blob: 21d8dd928889a4d88b1b939cb6130ac4ea53fed1 [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:
23prepare_index.pl -z --input <directory> --outputfile <filename>
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
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(
57 'input|x=s' => \$input,
58 '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
108
Nils Diewald7364d1f2013-11-05 19:26:35 +0000109my ($token_base_foundry, $token_base_layer) = (qw/OpenNLP Tokens/);
110if ($token_base) {
111 ($token_base_foundry, $token_base_layer) = split /#/, $token_base;
112};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000113
Nils Diewald7364d1f2013-11-05 19:26:35 +0000114# Get tokenization
115my $tokens = KorAP::Tokenizer->new(
116 path => $doc->path,
117 doc => $doc,
118 foundry => $token_base_foundry,
119 layer => $token_base_layer,
120 name => 'tokens'
121);
122$tokens->parse;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000123
Nils Diewald7364d1f2013-11-05 19:26:35 +0000124my @layers;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000125
Nils Diewald7364d1f2013-11-05 19:26:35 +0000126# Base information
Nils Diewald37e5b572013-11-20 20:26:03 +0000127push(@layers, ['Base', 'Sentences']);
Nils Diewald7364d1f2013-11-05 19:26:35 +0000128push(@layers, ['Base', 'Paragraphs']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000129
Nils Diewald7364d1f2013-11-05 19:26:35 +0000130# OpenNLP
131push(@layers, ['OpenNLP', 'Morpho']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000132
Nils Diewald7364d1f2013-11-05 19:26:35 +0000133# CoreNLP
134push(@layers, ['CoreNLP', 'NamedEntities', 'ne_dewac_175m_600']);
135push(@layers, ['CoreNLP', 'NamedEntities', 'ne_hgc_175m_600']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000136
Nils Diewald7364d1f2013-11-05 19:26:35 +0000137# Connexor
138push(@layers, ['Connexor', 'Morpho']);
139push(@layers, ['Connexor', 'Syntax']);
140push(@layers, ['Connexor', 'Phrase']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000141
Nils Diewald7364d1f2013-11-05 19:26:35 +0000142# TreeTagger
143push(@layers, ['TreeTagger', 'Morpho']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000144
Nils Diewald7364d1f2013-11-05 19:26:35 +0000145# Mate
146push(@layers, ['Mate', 'Morpho']);
147push(@layers, ['Mate', 'Dependency']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000148
Nils Diewald7364d1f2013-11-05 19:26:35 +0000149# XIP
150push(@layers, ['XIP', 'Morpho']);
151push(@layers, ['XIP', 'Constituency']);
152push(@layers, ['XIP', 'Dependency']);
Nils Diewald2db9ad02013-10-29 19:26:43 +0000153
154
Nils Diewald7364d1f2013-11-05 19:26:35 +0000155if ($skip{'#all'}) {
156 foreach (@allow) {
157 $tokens->add(split('#', $_));
158 stop_time;
159 };
160}
161else {
162 # Add to index file - respect skipping
163 foreach my $info (@layers) {
164 unless ($skip{lc($info->[0]) . '#' . lc($info->[1])}) {
165 $tokens->add(@$info);
166 stop_time;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000167 };
168 };
Nils Diewald7364d1f2013-11-05 19:26:35 +0000169};
Nils Diewald2db9ad02013-10-29 19:26:43 +0000170
Nils Diewald7364d1f2013-11-05 19:26:35 +0000171my $file;
172
173my $print_text = $text ? $tokens->to_string($primary) : ($pretty ? $tokens->to_pretty_json($primary) : $tokens->to_json($primary));
174
175if ($output) {
176 if ($gzip) {
177 $file = IO::Compress::Gzip->new($output, Minimal => 1);
178 }
179 else {
180 $file = IO::File->new($output, "w");
Nils Diewald2db9ad02013-10-29 19:26:43 +0000181 };
182
Nils Diewald7364d1f2013-11-05 19:26:35 +0000183# binmode $file, ':utf8';
Nils Diewald2db9ad02013-10-29 19:26:43 +0000184
Nils Diewald7364d1f2013-11-05 19:26:35 +0000185 $file->print($print_text);
186 $file->close;
187}
188else {
189# binmode STDOUT, ':utf8';
190 print $print_text . "\n";
Nils Diewald2db9ad02013-10-29 19:26:43 +0000191};
192
Nils Diewald7364d1f2013-11-05 19:26:35 +0000193stop_time;
Nils Diewald2db9ad02013-10-29 19:26:43 +0000194
195__END__