Fixed span position offset bug and fixed milestones behind last token bug

Change-Id: I6250d515ec132e9024b1eaca82523887b7d6f643
diff --git a/t/real/agd.t b/t/real/agd.t
index 6ba7ebe..7745bea 100644
--- a/t/real/agd.t
+++ b/t/real/agd.t
@@ -118,12 +118,12 @@
 $first_token = join('||', @{$output->{data}->{stream}->[0]});
 like($first_token, qr!<>:base/s:s\$<b>64<i>0<i>16<i>2<b>1!);
 
-my $token = join('||', @{$output->{data}->{stream}->[1]});
+my $token = join('||', @{$output->{data}->{stream}->[2]});
 like($token, qr!<>:base/s:s\$<b>64<i>16<i>23<i>4<b>1!);
-$token = join('||', @{$output->{data}->{stream}->[2]});
+$token = join('||', @{$output->{data}->{stream}->[3]});
 unlike($token, qr!<>:base/s:s!);
 
-$token = join('||', @{$output->{data}->{stream}->[3]});
+$token = join('||', @{$output->{data}->{stream}->[4]});
 like($token, qr!<>:base/s:s\$<b>64<i>23<i>27<i>5<b>1!);
 
 $token = join('||', @{$output->{data}->{stream}->[5]});
diff --git a/t/real/redew.t b/t/real/redew.t
new file mode 100644
index 0000000..25f369c
--- /dev/null
+++ b/t/real/redew.t
@@ -0,0 +1,155 @@
+use strict;
+use warnings;
+use Test::More;
+use Data::Dumper;
+use JSON::XS;
+use Log::Log4perl;
+
+use utf8;
+use lib 'lib', '../lib';
+
+use File::Basename 'dirname';
+use File::Spec::Functions 'catdir';
+
+# Initialize log4perl object
+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_ok('KorAP::XML::Krill');
+
+my $path = catdir(dirname(__FILE__), '../corpus/REDEW/DOC1/00000');
+
+ok(my $doc = KorAP::XML::Krill->new( path => $path . '/' ), 'Load Korap::Document');
+ok($doc->parse, 'Parse document');
+
+is($doc->text_sigle, 'REDEW/DOC1/00000', 'Correct text sigle');
+is($doc->doc_sigle, 'REDEW/DOC1', 'Correct document sigle');
+is($doc->corpus_sigle, 'REDEW', 'Correct corpus sigle');
+
+my $meta = $doc->meta;
+ok(!$meta->{T_title}, 'Title');               # ???
+ok(!$meta->{T_sub_title}, 'SubTitle');
+ok(!$meta->{T_author}, 'Author');
+ok(!$meta->{A_editor}, 'Editor');
+ok(!$meta->{S_pub_place}, 'PubPlace');
+is($meta->{A_publisher}, '...', 'Publisher'); # ???
+
+is($meta->{S_text_type}, '?', 'Text Type');   # ???
+ok(!$meta->{S_text_type_art}, 'No Text Type Art');
+ok(!$meta->{S_text_type_ref}, 'No Text Type Ref');
+ok(!$meta->{S_text_domain}, 'No Text Domain');
+ok(!$meta->{S_text_column}, 'No Text Column');
+
+ok(!$meta->{K_text_class}->[0], 'Correct Text Class');
+
+is($meta->{D_pub_date}, '20200000', 'Creation date');
+is($meta->{D_creation_date}, '20200000', 'Creation date');
+is($meta->{S_availability}, 'QAO-NC', 'License');           # ???
+ok(!$meta->{A_pages}, 'Pages');
+
+ok(!$meta->{A_file_edition_statement}, 'File Statement');
+ok(!$meta->{A_bibl_edition_statement}, 'Bibl Statement');
+
+ok(!$meta->{A_reference}, 'Reference');
+ok(!$meta->{S_language}, 'Language'); # ???
+
+is($meta->{T_corpus_title}, 'Redewiedergabe', 'Correct Corpus title');
+ok(!$meta->{T_corpus_sub_title}, 'Correct Corpus sub title');
+is($meta->{T_corpus_author}, '...', 'Correct Corpus author'); # ???
+is($meta->{A_corpus_editor}, '...', 'Correct Corpus editor'); # ???
+
+is($meta->{T_doc_title}, 'Redewiedergabe Dokument 1', 'Correct Doc title');
+ok(!$meta->{T_doc_sub_title}, 'Correct Doc sub title');
+ok(!$meta->{T_doc_author}, 'Correct Doc author');
+ok(!$meta->{A_doc_editor}, 'Correct doc editor');
+
+# Tokenization
+use_ok('KorAP::XML::Tokenizer');
+
+my ($token_base_foundry, $token_base_layer) = (qw/drukola Morpho/);
+
+# Get tokenization
+my $tokens = KorAP::XML::Tokenizer->new(
+  path => $doc->path,
+  doc => $doc,
+  foundry => $token_base_foundry,
+  layer => $token_base_layer,
+  name => 'tokens'
+);
+
+ok($tokens, 'Token Object is fine');
+ok($tokens->parse, 'Token parsing is fine');
+
+my $output = decode_json( $tokens->to_json );
+
+is(substr($output->{data}->{text}, 0, 100), 'Cechov,_Anton_Pavlovic_Gram.tg4_1.xml 1886 1880 Gram Čechov, Anton Pavlovič yes yes Erzähltext digbi', 'Primary Data');
+
+is($output->{data}->{name}, 'tokens', 'tokenName');
+is($output->{data}->{tokenSource}, 'drukola#morpho', 'tokenSource');
+is($output->{version}, '0.03', 'version');
+
+is($output->{data}->{foundries}, '', 'Foundries');
+is($output->{data}->{layerInfos}, '', 'layerInfos');
+is($output->{data}->{stream}->[0]->[4], 's:Hörst', 'data');
+
+is($output->{textSigle}, 'REDEW/DOC1/00000', 'Correct text sigle');
+is($output->{docSigle}, 'REDEW/DOC1', 'Correct document sigle');
+is($output->{corpusSigle}, 'REDEW', 'Correct corpus sigle');
+
+ok(!$output->{title}, 'Title');
+ok(!$output->{subTitle}, 'Correct SubTitle');
+ok(!$output->{author}, 'Author');
+ok(!exists $output->{editor}, 'Publisher');
+
+# Add annotations
+$tokens->add('DRuKoLa', 'Morpho');
+$tokens->add('DeReKo', 'Structure');
+
+$output = decode_json( $tokens->to_json );
+
+my $first = $output->{data}->{stream}->[0];
+
+is('-:tokens$<i>13',$first->[0]);
+is('<>:base/s:t$<b>64<i>0<i>197<i>12<b>0',$first->[1]);
+is('<>:dereko/s:text$<b>64<i>0<i>197<i>12<b>0',$first->[2]);
+is('<>:dereko/s:body$<b>64<i>118<i>197<i>12<b>1',$first->[3]);
+is('<>:dereko/s:p$<b>64<i>118<i>197<i>12<b>2',$first->[4]);
+is('<>:dereko/s:said$<b>64<i>118<i>197<i>12<b>3<s>1',$first->[5]);
+is('@:dereko/s:level:1$<b>17<s>1<i>12',$first->[6]);
+is('@:dereko/s:content:speech$<b>17<s>1<i>12',$first->[7]);
+is('@:dereko/s:mode:direct$<b>17<s>1<i>12',$first->[8]);
+is('@:dereko/s:id:1$<b>17<s>1<i>12',$first->[9]);
+is('_0$<i>123<i>128',$first->[10]);
+is("drukola/l:H\x{f6}rst",$first->[11]);
+is('drukola/m:msd:rfpos',$first->[12]);
+is('drukola/m:sentstart:no',$first->[13]);
+is('drukola/m:stwr:direct.speech.1',$first->[14]);
+is('drukola/p:VVFIN',$first->[15]);
+is("i:h\x{f6}rst",$first->[16]);
+is("s:H\x{f6}rst",$first->[17]);
+
+my $nine = join(',', @{$output->{data}->{stream}->[9]});
+like($nine, qr{drukola\/l:nichts}, 'Nichts');
+like($nine, qr{_9\$<i>170<i>176}, 'Term boundaries');
+unlike($nine, qr{<>:dereko/s:said\$<b>64<i>176<i>196<i>12<b>4<s>1}, 'Term boundaries');
+
+my $ten = join(',', @{$output->{data}->{stream}->[10]});
+like($ten, qr{_10\$<i>177<i>180}, 'Term boundaries');
+like($ten, qr{<>:dereko/s:said\$<b>64<i>176<i>196<i>12<b>4<s>1}, 'Term boundaries');
+
+my $eleven = join(',', @{$output->{data}->{stream}->[11]});
+like($eleven, qr{_11\$<i>181<i>188}, 'Term boundaries');
+like($eleven, qr{<>:dereko/s:seg\$<b>64<i>180<i>188<i>12<b>5<s>1}, 'Segment');
+
+
+my $twelve = join(',', @{$output->{data}->{stream}->[12]});
+like($twelve, qr{_12\$<i>189<i>195}, 'Term boundaries');
+like($twelve, qr{drukola/l:Wort}, 'Lemma');
+like($twelve, qr{<>:dereko/s:seg\$<b>64<i>188<i>195<i>13<b>5<s>1}, 'Segment');
+
+done_testing;
+__END__
diff --git a/t/real/rei.t b/t/real/rei.t
index b375c24..d50a732 100644
--- a/t/real/rei.t
+++ b/t/real/rei.t
@@ -225,16 +225,17 @@
 
 my $last = $output->{data}->{stream}->[-1];
 
-is('<>:dereko/s:text$<b>65<i>17859<i>17859<i>2640<b>0', $last->[0]);
-is('<>:dereko/s:back$<b>65<i>17859<i>17859<i>2640<b>1', $last->[1]);
-is('>:malt/d:APP$<b>32<i>2639', $last->[2]);
-is('_2640$<i>17851<i>17859', $last->[3]);
-is('corenlp/p:NE', $last->[4]);
-is("i:schr\x{f6}der", $last->[5]);
-is('opennlp/p:NE', $last->[6]);
-is("s:Schr\x{f6}der", $last->[7]);
-is("tt/l:Schr\x{f6}der", $last->[8]);
-is('tt/p:NE', $last->[9]);
+# Milestones behind the final token are no longer indexed
+# is('<>:dereko/s:text$<b>65<i>17859<i>17859<i>2640<b>0', $last->[0]);
+# is('<>:dereko/s:back$<b>65<i>17859<i>17859<i>2640<b>1', $last->[1]);
+is('>:malt/d:APP$<b>32<i>2639', $last->[0]);
+is('_2640$<i>17851<i>17859', $last->[1]);
+is('corenlp/p:NE', $last->[2]);
+is("i:schr\x{f6}der", $last->[3]);
+is('opennlp/p:NE', $last->[4]);
+is("s:Schr\x{f6}der", $last->[5]);
+is("tt/l:Schr\x{f6}der", $last->[6]);
+is('tt/p:NE', $last->[7]);
 
 
 # REI/BNG/00071
@@ -282,7 +283,7 @@
 is('-:corenlp/sentences$<i>69', $first->[2]);
 is('-:opennlp/sentences$<i>65', $first->[3]);
 is('-:tokens$<i>1009', $first->[4]);
-is('-:tt/sentences$<i>95', $first->[5]);
+is('-:tt/sentences$<i>94', $first->[5]);
 is('<:malt/d:APP$<b>32<i>1', $first->[6]);
 is('<>:corenlp/c:MPN$<b>64<i>0<i>16<i>2<b>3', $first->[7]);
 is('<>:base/s:s$<b>64<i>0<i>48<i>5<b>2', $first->[8]);
diff --git a/t/real/wpd.t b/t/real/wpd.t
index 517f9be..e45db0a 100644
--- a/t/real/wpd.t
+++ b/t/real/wpd.t
@@ -78,10 +78,17 @@
 my $stream = $tokens->to_data->{data}->{stream};
 
 # This is not a goot relation example
-is($stream->[77]->[0],
-   '<:mate/d:--$<b>34<i>498<i>499<i>78<i>78',
-   'element to term');
-is($stream->[78]->[0], '>:mate/d:--$<b>33<i>498<i>499<i>77<i>78', 'term to element');
+ is($stream->[79]->[0],
+    '>:mate/d:CJ$<b>32<i>68',
+    'term to term');
+ is($stream->[79]->[1], '<:mate/d:PD$<b>32<i>81', 'term to term');
+
+
+# These are no longer aligned
+# is($stream->[77]->[0],
+#    '<:mate/d:--$<b>34<i>498<i>499<i>78<i>78',
+#    'element to term');
+# is($stream->[78]->[0], '>:mate/d:--$<b>33<i>498<i>499<i>77<i>78', 'term to element');
 
 $tokens->add('Base', 'Sentences');
 
diff --git a/t/real/wpf.t b/t/real/wpf.t
index 4a28c2f..ba6fb94 100644
--- a/t/real/wpf.t
+++ b/t/real/wpf.t
@@ -101,10 +101,11 @@
 $tokens->add('Talismane', 'Morpho');
 
 $stream = $tokens->to_data->{data}->{stream};
-is($stream->[1]->[10], 'talismane/l:Sigmund', 'Talismane morpho annotation');
-is($stream->[1]->[11], 'talismane/m:g:m', 'Talismane morpho annotation');
-is($stream->[1]->[12], 'talismane/m:n:s', 'Talismane morpho annotation');
-is($stream->[1]->[13], 'talismane/p:NPP', 'Talismane morpho annotation');
+
+is($stream->[1]->[9], 'talismane/l:Sigmund', 'Talismane morpho annotation');
+is($stream->[1]->[10], 'talismane/m:g:m', 'Talismane morpho annotation');
+is($stream->[1]->[11], 'talismane/m:n:s', 'Talismane morpho annotation');
+is($stream->[1]->[12], 'talismane/p:NPP', 'Talismane morpho annotation');
 
 is($stream->[300]->[5], 'talismane/l:son', 'Talismane lemma annotation');
 is($stream->[300]->[6], 'talismane/m:g:m', 'Talismane morph annotation');
@@ -116,7 +117,10 @@
 # Add Malt dependency
 $tokens->add('Malt', 'Dependency');
 $stream = $tokens->to_data->{data}->{stream};
-is($stream->[1]->[1], '>:malt/d:dep$<b>33<i>7<i>8<i>0<i>1', 'Malt dep annotation');
+
+# This is no longer indexed
+# is($stream->[1]->[1], '>:malt/d:dep$<b>33<i>7<i>8<i>0<i>1', 'Malt dep annotation');
+
 is($stream->[1]->[2], '<:malt/d:dep$<b>32<i>2', 'Malt dep annotation');
 is($stream->[300]->[1], '>:malt/d:punct$<b>32<i>302', 'Malt dep annotation');
 
@@ -124,9 +128,10 @@
 # Add TreeTagger morpho
 $tokens->add('TreeTagger', 'Morpho');
 $stream = $tokens->to_data->{data}->{stream};
-is($stream->[1]->[14], 'tt/p:ADJ$<b>129<b>26', 'TreeTagger morph annotation');
-is($stream->[1]->[15], 'tt/p:NAM$<b>129<b>200', 'TreeTagger morph annotation');
-is($stream->[1]->[16], 'tt/p:NOM$<b>129<b>27', 'TreeTagger morph annotation');
+
+is($stream->[1]->[13], 'tt/p:ADJ$<b>129<b>26', 'TreeTagger morph annotation');
+is($stream->[1]->[14], 'tt/p:NAM$<b>129<b>200', 'TreeTagger morph annotation');
+is($stream->[1]->[15], 'tt/p:NOM$<b>129<b>27', 'TreeTagger morph annotation');
 
 
 done_testing;