Tool/Tagger/MorphoDiTa.pm

filter: Statementstmt 33.3% 15 / 45 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 5 / 10 Podpod 33.3% 1 / 3 totaltotal 33.8% 21 / 62 CC 5 SCAR 17.8
CC
5
Cov
31%
_build_tool
12.0
tag_sentence
12.0
_build__model_absolute_path
2.0
CRAP 5.9
« Tool/Tagger/Featurama.pm ↑ Summary Block/Read/AlignedSentences.pm »
1 package Treex::Tool::Tagger::MorphoDiTa;
2 $Treex::Tool::Tagger::MorphoDiTa::VERSION = '2.20151102';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Treex::Core::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 with 'Treex::Tool::Tagger::Role';
9  
10 # This block terminates Tagger::MorphoDiTa and any other dependent modules, if Ufal::MorphoDiTa is not installed
11 # We want to include this wrapper into Treex-Unilang package, but remove the hard dependency on Ufal::MorphoDiTa package
12 # TODO: solve this problem in a better (more Treex-like) way
13 eval {
14 require Ufal::MorphoDiTa;
15 1;
16 };
17 if (my $dep = $@) {
18 log_warn('missing module: Ufal::MorphoDiTa');
19 exit 0;
20 }
21  
22  
23 # Path to the model data file
24 has model => ( is => 'ro', isa => 'Str', required => 1 );
25 has _model_absolute_path => ( is => 'ro', isa => 'Str', lazy_build => 1 );
26  
27 0 ▶sub _build__model_absolute_path {
Subroutine _build__model_absolute_path: not called CC=1 SCAR=6.9
28 0 my ($self) = @_;
29 0 return Treex::Core::Resource::require_file_from_share($self->model);
30 }
31  
32 # Instance of Ufal::MorphoDiTa::Tagger
33 has tool => (is=>'ro', lazy_build=>1);
34  
35 # tool can be shared by more instances (if the dictionary file is the same)
36 my %TOOL_FOR_PATH;
37 0 ▶sub _build_tool {
Subroutine _build_tool: not called CC=3 SCAR=24.8
38 0 my ($self) = @_;
39 0 my $path = $self->_model_absolute_path;
40 0 my $tool = $TOOL_FOR_PATH{$path};
41 0 ▶ return $tool if $tool;
Branch: 1 branch
BranchTrueFalseTotal
if $tool 0 0 0
42 0 log_info("Loading Ufal::MorphoDiTa::Tagger with model '$path'");
43 0 ▶ $tool = Ufal::MorphoDiTa::Tagger::load($path)
Branch: 1 branch
BranchTrueFalseTotal
$tool = Ufal::MorphoDiTa::Tagger::load($path) or log_fatal("Cannot load Ufal::MorphoDiTa::Tagger with model from file '${path}'") 0 0 0
44 or log_fatal("Cannot load Ufal::MorphoDiTa::Tagger with model from file '$path'");
45 0 $TOOL_FOR_PATH{$path} = $tool;
46 0 return $tool;
47 }
48  
49 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my ($self) = @_;
51 # The tool is lazy_build, so load it now
52 0 $self->tool;
53 0 return;
54 }
55  
56 0 ▶sub tag_sentence {
Subroutine tag_sentence: not called CC=3 SCAR=24.8
Pod documented
57 0 my ( $self, $tokens_rf ) = @_;
58 0 my $forms = Ufal::MorphoDiTa::Forms->new();
59 0 $forms->push($_) for @$tokens_rf;
60  
61 # The main work. Tags and lemmas will be saved to $tagged_lemmas.
62 0 my $tagged_lemmas = Ufal::MorphoDiTa::TaggedLemmas->new();
63 0 $self->tool->tag($forms, $tagged_lemmas);
64  
65 # Extract the result into @tags and @lemmas.
66 0 my (@tags, @lemmas);
67 0 for my $i (0 .. $tagged_lemmas->size()-1){
68 0 my $tag_and_lemma = $tagged_lemmas->get($i);
69 0 push @tags, $tag_and_lemma->{tag};
70 0 push @lemmas, $tag_and_lemma->{lemma};
71 }
72  
73 0 return (\@tags, \@lemmas);
74 }
75  
76 0 ▶sub is_guessed {
Subroutine is_guessed: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ($self, $tokens_rf) = @_;
78 0 my $tagged_lemmas = Ufal::MorphoDiTa::TaggedLemmas->new();
79 0 my $morpho = $self->tool->getMorpho();
80 0 my @guessed = map {$morpho->analyze($_, $Ufal::MorphoDiTa::Morpho::GUESSER, $tagged_lemmas)} @$tokens_rf;
81 0 return \@guessed;
82 }
83  
84 1;
85  
86 =encoding utf-8
87  
88 =head1 NAME
89  
90 Treex::Tool::Tagger::MorphoDiTa - wrapper for Ufal::MorphoDiTa
91  
92 =head1 VERSION
93  
94 version 2.20151102
95  
96 =head1 SYNOPSIS
97  
98 use Treex::Tool::Tagger::MorphoDiTa;
99 my $tagger = Treex::Tool::Tagger::MorphoDiTa->new(
100 model => 'data/models/morphodita/cs/czech-morfflex-pdt-131112.tagger-fast',
101 );
102 # or czech-morfflex-pdt-131112.tagger-best_accuracy
103 my @tokens = qw(Jak to jde ?);
104 my ($tags_rf, $lemmas_rf) = $tagger->tag_sentence(\@tokens);
105  
106 =head1 DESCRIPTION
107  
108 Wrapper for state-of-the-art part-of-speech (morphological) tagger MorphoDiTa
109 by Milan Straka and Jana Straková.
110  
111 =head1 PARAMETERS
112  
113 =over
114  
115 =item model
116  
117 Path to the model file within Treex share
118 (or relative path starting with "./" or absolute path starting with "/").
119  
120 =back
121  
122 =head1 METHODS
123  
124 =over
125  
126 =item ($tags_rf, $lemmas_rf) = $tagger->tag_sentence(\@tokens);
127  
128 Returns a list of tags and lemmas for tokenized input.
129  
130 =back
131  
132 =head1 SEE ALSO
133  
134 L<http://ufal.mff.cuni.cz/morphodita>
135  
136 L<https://metacpan.org/pod/Ufal::MorphoDiTa>
137  
138 =head1 AUTHOR
139  
140 Martin Popel <popel@ufal.mff.cuni.cz>
141  
142 =head1 COPYRIGHT AND LICENSE
143  
144 Copyright © 2014-2015 by Institute of Formal and Applied Linguistics, Charles University in Prague
145 The development of this resource is partly funded by the European Commision, project QTLeap FP7-ICT-2013.4.1-610516 L<http://qtleap.eu>
146  
147 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Tool/Tagger/Featurama.pm ↑ Summary Block/Read/AlignedSentences.pm »