Preliminary support for MDParser annotations

Change-Id: I12456ebc820fe54798295fb15fb35d4d701b71b7
diff --git a/lib/KorAP/XML/Annotation/MDParser/Dependency.pm b/lib/KorAP/XML/Annotation/MDParser/Dependency.pm
new file mode 100644
index 0000000..f8a14b6
--- /dev/null
+++ b/lib/KorAP/XML/Annotation/MDParser/Dependency.pm
@@ -0,0 +1,108 @@
+package KorAP::XML::Annotation::MDParser::Dependency;
+use KorAP::XML::Annotation::Base;
+use strict;
+use warnings;
+
+sub parse {
+  my $self = shift;
+
+  # TODO: Create XIP tree here - for indirect dependency
+  # >>:xip/d:SUBJ<i>566<i>789
+
+  # Relation data
+  $$self->add_tokendata(
+    foundry => 'mdparser',
+    layer => 'dependency',
+    cb => sub {
+      my ($stream, $source, $tokens) = @_;
+
+      # Get MultiTermToken from stream for source
+      my $mtt = $stream->pos($source->pos);
+
+      # Serialized information from token
+      my $content = $source->hash;
+
+      # Get relation information
+      my $rel = $content->{rel};
+      $rel = [$rel] unless ref $rel eq 'ARRAY';
+
+      # Iterate over relations
+      foreach (@$rel) {
+	my $label = $_->{-label};
+
+	#my $target = $stream->tui($source->pos);
+	my $from = $_->{span}->{-from};
+	my $to   = $_->{span}->{-to};
+
+	# Target
+	my $target = $tokens->token($from, $to);
+
+	# Relation is term-to-term with a found target!
+	if ($target) {
+
+	  # Unary means, it refers to itself!
+	  $mtt->add(
+	    term => '>:mdp/d:' . $label,
+	    pti => 32, # term-to-term relation
+	    payload =>
+	      '<i>' . $target->pos # . # right part token position
+		# '<s>0' . # $source_term->tui . # left part tui
+		# '<s>0' # . $target_term->tui # right part tui
+	    );
+
+	  my $target_mtt = $stream->pos($target->pos);
+
+	  $target_mtt->add(
+	    term => '<:mdp/d:' . $label,
+	    pti => 32, # term-to-term relation
+	    payload =>
+	      '<i>' . $source->pos # . # left part token position
+		# '<s>0' . # $source_term->tui . # left part tui
+		# '<s>0' # . $target_term->tui # right part tui
+	    );
+	}
+
+	# Relation is possibly term-to-element with a found target!
+	elsif ($target = $tokens->span($from, $to)) {
+	  $mtt->add(
+	    term => '>:mdp/d:' . $label,
+	    pti => 33, # term-to-element relation
+	    payload =>
+	      '<i>' . $target->o_start . # end position
+		'<i>' . $target->o_end . # end position
+		  '<i>' . $target->p_start . # right part start position
+		    '<i>' . $target->p_end # . # right part end position
+		      # '<s>0' . # $source_term->tui . # left part tui
+		      # '<s>0' # . $target_span->tui # right part tui
+	    );
+
+	  my $target_mtt = $stream->pos($target->p_start);
+	  $target_mtt->add(
+	    term => '<:mdp/d:' . $label,
+	    pti => 34, # element-to-term relation
+	    payload =>
+	      '<i>' . $target->o_start . # end position
+		'<i>' . $target->o_end . # end position
+		  '<i>' . $target->p_end . # right part end position
+		    '<i>' . $source->pos # . # left part token position
+		      #	'<s>0' . # $source_term->tui . # left part tui
+		      # '<s>0' # . $target_span->tui # right part tui
+
+	    );
+	}
+	else {
+	  use Data::Dumper;
+	  $$self->log->warn('Relation currently not supported: ' . Dumper($content));
+	};
+      };
+    }) or return;
+
+  return 1;
+};
+
+sub layer_info {
+  ['mdp/d=rels']
+};
+
+
+1;
diff --git a/t/annotation/mdp_dependency.t b/t/annotation/mdp_dependency.t
new file mode 100644
index 0000000..919552c
--- /dev/null
+++ b/t/annotation/mdp_dependency.t
@@ -0,0 +1,69 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use utf8;
+use Log::Log4perl;
+use Data::Dumper;
+
+Log::Log4perl->init({
+  'log4perl.rootLogger' => 'ERROR, STDERR',
+  'log4perl.appender.STDERR' => 'Log::Log4perl::Appender::ScreenColoredLevels',
+  'log4perl.appender.STDERR.layout' => 'PatternLayout',
+  'log4perl.appender.STDERR.layout.ConversionPattern' => '[%r] %F %L %c - %m%n'
+});
+
+use File::Basename 'dirname';
+use File::Spec::Functions qw/catdir catfile/;
+use Test::More;
+use Scalar::Util qw/weaken/;
+use Data::Dumper;
+use lib 't/annotation';
+use File::Temp qw/tempdir/;
+
+use_ok('KorAP::XML::Annotation::MDParser::Dependency');
+use_ok('KorAP::XML::Archive');
+use_ok('KorAP::XML::Krill');
+use_ok('KorAP::XML::Tokenizer');
+
+my $name = 'wpd15-single';
+my @path = (dirname(__FILE__), '..', 'corpus','archives');
+
+my $file = catfile(@path, $name . '.zip');
+ok(my $archive = KorAP::XML::Archive->new($file), 'Create archive');
+
+ok($archive->attach('#' . catfile(@path, $name . '.mdparser.zip')), 'Attach mdparser archive');
+
+my $dir = tempdir();
+
+my $f_path = 'WPD15/A00/00081';
+$archive->extract($f_path, $dir);
+
+ok(my $doc = KorAP::XML::Krill->new( path => $dir . '/' . $f_path));
+
+ok($doc->parse, 'Krill parser works');
+
+my $tokens = KorAP::XML::Tokenizer->new(
+  path => $doc->path,
+  doc => $doc,
+  foundry => 'Base',
+  layer => 'Tokens',
+  name => 'tokens'
+) or return;
+
+$tokens->parse or return;
+
+ok($tokens->add('MDParser', 'Dependency'), 'Add Dependency');
+
+my $data = $tokens->to_data->{data};
+
+
+is($data->{tokenSource}, 'base#tokens', 'TokenSource');
+like($data->{foundries}, qr!mdparser/dependency!, 'foundries');
+like($data->{layerInfos}, qr!mdp/d=rels!, 'foundries');
+
+my $stream = $data->{stream};
+
+# diag Dumper $stream->[0];
+
+done_testing;
+__END__