Akron | 414ec95 | 2020-08-03 15:48:43 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use utf8; |
| 5 | use Test::More; |
| 6 | use JSON::XS; |
| 7 | use Data::Dumper; |
| 8 | |
| 9 | use File::Basename 'dirname'; |
| 10 | use File::Spec::Functions 'catdir'; |
| 11 | |
| 12 | if ($ENV{SKIP_REAL}) { |
| 13 | plan skip_all => 'Skip real tests'; |
| 14 | }; |
| 15 | |
| 16 | sub _t2h { |
| 17 | my $string = shift; |
| 18 | $string =~ s/^\[\(\d+?-\d+?\)(.+?)\]$/$1/; |
| 19 | my %hash = (); |
| 20 | foreach (split(qr!\|!, $string)) { |
| 21 | $hash{$_} = 1; |
| 22 | }; |
| 23 | return \%hash; |
| 24 | }; |
| 25 | |
| 26 | use_ok('KorAP::XML::Krill'); |
| 27 | |
| 28 | my $path = catdir(dirname(__FILE__), qw/corpus WPD 00001/); |
| 29 | ok(my $doc = KorAP::XML::Krill->new( path => $path ), 'Load Korap::Document'); |
| 30 | like($doc->path, qr!\Q$path\E/$!, 'Path'); |
| 31 | ok($doc->parse, 'Parse document'); |
| 32 | is($doc->text_sigle, 'WPD/AAA/00001', 'ID'); |
| 33 | |
| 34 | |
| 35 | # Get tokens |
| 36 | use_ok('KorAP::XML::Tokenizer'); |
| 37 | |
| 38 | # Get tokenization |
| 39 | ok(my $tokens = KorAP::XML::Tokenizer->new( |
| 40 | path => $doc->path, |
| 41 | doc => $doc, |
| 42 | foundry => 'OpenNLP', |
| 43 | layer => 'Tokens', |
| 44 | name => 'tokens' |
| 45 | ), 'New Tokenizer'); |
| 46 | ok($tokens->parse, 'Parse'); |
| 47 | |
| 48 | like($tokens->stream->pos(12)->to_string, qr/s:Vokal/); |
| 49 | like($tokens->stream->pos(13)->to_string, qr/s:Der/); |
| 50 | |
| 51 | |
| 52 | # Get tokenization with non word tokens |
| 53 | ok($tokens = KorAP::XML::Tokenizer->new( |
| 54 | path => $doc->path, |
| 55 | doc => $doc, |
| 56 | foundry => 'OpenNLP', |
| 57 | layer => 'Tokens', |
| 58 | name => 'tokens', |
| 59 | non_word_tokens => 1 |
| 60 | ), 'New Tokenizer'); |
| 61 | ok($tokens->parse, 'Parse'); |
| 62 | |
| 63 | like($tokens->stream->pos(12)->to_string, qr/s:Vokal/); |
| 64 | like($tokens->stream->pos(13)->to_string, qr/s:\./); |
| 65 | like($tokens->stream->pos(14)->to_string, qr/s:Der/); |
| 66 | |
| 67 | |
| 68 | my $json = decode_json $tokens->to_json; |
| 69 | is($json->{docSigle}, 'WPD/AAA', 'DocSigle old'); |
| 70 | is($json->{author}, 'Ruru; Jens.Ol; Aglarech; u.a.', 'author old'); |
| 71 | |
| 72 | $json = decode_json $tokens->to_json(0.4); |
| 73 | is($json->{fields}->[0]->{key}, 'corpusSigle'); |
| 74 | is($json->{fields}->[0]->{value}, 'WPD'); |
| 75 | is($json->{fields}->[7]->{key}, 'creationDate'); |
| 76 | is($json->{fields}->[7]->{value}, '2005'); |
| 77 | |
| 78 | is($json->{data}->{name}, 'tokens'); |
| 79 | is($json->{data}->{tokenSource}, 'opennlp#tokens'); |
| 80 | is($json->{data}->{stream}->[0]->[0], '-:tokens$<i>1068'); |
| 81 | is($json->{data}->{stream}->[0]->[1], '<>:base/s:t$<b>64<i>0<i>6083<i>1068<b>0'); |
| 82 | |
| 83 | done_testing; |
| 84 | |
| 85 | __END__ |