Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [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 | |
| 8 | use File::Basename 'dirname'; |
| 9 | use File::Spec::Functions 'catdir'; |
| 10 | |
| 11 | sub _t2h { |
| 12 | my $string = shift; |
| 13 | $string =~ s/^\[\(\d+?-\d+?\)(.+?)\]$/$1/; |
| 14 | my %hash = (); |
| 15 | foreach (split(qr!\|!, $string)) { |
| 16 | $hash{$_} = 1; |
| 17 | }; |
| 18 | return \%hash; |
| 19 | }; |
| 20 | |
| 21 | use_ok('KorAP::XML::Krill'); |
| 22 | |
| 23 | my $path = catdir(dirname(__FILE__), 'corpus/WPD/00001'); |
| 24 | ok(my $doc = KorAP::XML::Krill->new( path => $path ), 'Load Korap::Document'); |
| 25 | like($doc->path, qr!\Q$path\E/$!, 'Path'); |
| 26 | ok($doc->parse, 'Parse document'); |
| 27 | is($doc->text_sigle, 'WPD/AAA/00001', 'ID'); |
| 28 | |
| 29 | |
| 30 | # Get tokens |
| 31 | use_ok('KorAP::XML::Tokenizer'); |
| 32 | |
| 33 | # Get tokenization |
| 34 | ok(my $tokens = KorAP::XML::Tokenizer->new( |
| 35 | path => $doc->path, |
| 36 | doc => $doc, |
| 37 | foundry => 'OpenNLP', |
| 38 | layer => 'Tokens', |
| 39 | name => 'tokens' |
| 40 | ), 'New Tokenizer'); |
| 41 | ok($tokens->parse, 'Parse'); |
| 42 | |
| 43 | like($tokens->stream->pos(12)->to_string, qr/s:Vokal/); |
| 44 | like($tokens->stream->pos(13)->to_string, qr/s:Der/); |
| 45 | |
| 46 | |
| 47 | # Get tokenization with non word tokens |
| 48 | ok($tokens = KorAP::XML::Tokenizer->new( |
| 49 | path => $doc->path, |
| 50 | doc => $doc, |
| 51 | foundry => 'OpenNLP', |
| 52 | layer => 'Tokens', |
| 53 | name => 'tokens', |
| 54 | non_word_tokens => 1 |
| 55 | ), 'New Tokenizer'); |
| 56 | ok($tokens->parse, 'Parse'); |
| 57 | |
| 58 | like($tokens->stream->pos(12)->to_string, qr/s:Vokal/); |
| 59 | like($tokens->stream->pos(13)->to_string, qr/s:\./); |
| 60 | like($tokens->stream->pos(14)->to_string, qr/s:Der/); |
| 61 | |
| 62 | |
Akron | c526e75 | 2019-02-05 14:57:17 +0100 | [diff] [blame^] | 63 | my $json = decode_json $tokens->to_json; |
| 64 | is($json->{docSigle}, 'WPD/AAA', 'DocSigle old'); |
| 65 | is($json->{author}, 'Ruru; Jens.Ol; Aglarech; u.a.', 'author old'); |
| 66 | |
| 67 | $json = decode_json $tokens->to_json(0.4); |
| 68 | is($json->{fields}->[0]->{key}, 'corpusSigle'); |
| 69 | is($json->{fields}->[0]->{value}, 'WPD'); |
| 70 | is($json->{fields}->[6]->{key}, 'creationDate'); |
| 71 | is($json->{fields}->[6]->{value}, '2005'); |
| 72 | |
Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 73 | done_testing; |
| 74 | |
| 75 | __END__ |