Tag/Tagger.pm

filter: Statementstmt 91.5% 97 / 106 Branchbran 77.7% 14 / 18 Conditioncond 40.0% 8 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 76.4% 13 / 17 Podpod 20.0% 1 / 5 totaltotal 75.2% 134 / 178 SCAR 31.0
CC
20
Cov
83%
get_wordlist
24.8
add_human_tags
3.0
new
2.0
CRAP 22.1
« Serialise/Simple.pm ↑ Summary Dict.pm »
1 package WordLists::Tag::Tagger;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use WordLists::Common qw(/generic/);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Lingua::EN::Tagger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use WordLists::WordList;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use WordLists::Sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use WordLists::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our $VERSION = $WordLists::Base::VERSION;
11
12 2 our $AUTOLOAD;
13 2 our @ignore_pos_codes = qw(cd to prp prps sym pp pps ppr lrb rrb ppc ppl );
14 sub _norm_word($)
15 {
16 11 ▶ my $s = shift;
Subroutine _norm_word: called CC=1 SCAR=0.0
17 11 $s=~s/\.//g;
18 11 $s=~s/\/.*//;
19 11 $s=~s/['’]s$//;
20 11 $s=~s/['‘’`"“”]$//;
21 11 $s=~s/^['‘’`"“”]//;
22 11 $s=~tr/…–\xA0\*//d;
23 11 return lc $s;
24 }
25 sub human_pos($)
26 {
27 29 ▶ my $sPos = shift;
Subroutine human_pos: called CC=1 SCAR=0.0
Pod undocumented
28 29 $sPos =~ s<^nn.*$>
29 <noun>;
30 29 $sPos =~ s<^in$>
31 <preposition>;
32 29 $sPos =~ s<^to$>
33 <preposition>;
34 29 $sPos =~ s<^jj.*$>
35 <adjective>;
36 29 $sPos =~ s<^md.*$>
37 <modal verb>;
38 29 $sPos =~ s<^vb.*$>
39 <verb>;
40 29 $sPos =~ s<^rb.*$>
41 <adverb>;
42 29 $sPos =~ s<^det$>
43 <determiner>;
44 29 $sPos =~ s<^cc$>
45 <conjunction>;
46 29 $sPos =~ s<^wrb$>
47 <pronoun>;
48 29 $sPos =~ s<^wdt$>
49 <pronoun>;
50 29 $sPos =~ s<^wp$>
51 <pronoun>;
52 29 $sPos =~ s<^prp.*$>
53 <pronoun>;
54 29 $sPos =~ s<^cd.*$>
55 <number>;
56 29 $sPos =~ s<^uh$>
57 <interjection>;
58 29 return $sPos;
59 }
60
61
62 sub new
63 {
64 1 ▶ my ($class, $args) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
65 1 ▶ $args ||={};
Condition50%1/2
$args ||= {}
l!l
01
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
66 1 my $self = {
67 tagger=>Lingua::EN::Tagger->new(%$args),
68 };
69 1 bless ($self, $class);
70 }
71
72 2 1;
73
74 sub add_tags
75 {
76 0 ▶ my ($self, $sMS) = @_;
Subroutine add_tags: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my $taggedMS = $self->{tagger}->add_tags( $sMS );
78 0 return $taggedMS;
79 }
80
81 sub add_human_tags
82 {
83 3 ▶ my ($self, $sMS) = @_;
Subroutine add_human_tags: called CC=3 SCAR=11.0
Pod undocumented
84 3 my $sMSOUT = '';
85 3 foreach my $sSentence (@{$self->{tagger}->get_sentences($sMS)})
86 {
87 3 $sSentence =~ tr/<>&//d;
88 3 my $taggedSentence = $self->{tagger}->add_tags( $sSentence );
89 3 ▶ $taggedSentence =~ s`<([a-z]+)>([^<]+)</\1>`human_pos($1) ne $1 ? qq(<span pos=").human_pos($1).qq(">$2</span>) : $2;`ge;
Branch: 1 branch
BranchTrueFalseTotal
&human_pos($1) ne $1 ? : 9 2 11
90 3 $sMSOUT .= "<p>$taggedSentence</p> ";
91 }
92 3 return $sMSOUT;
93 }
94
95 sub get_wordlist
96 {
97 1 ▶ my ($self, $sUntagged, $args) = @_;
Subroutine get_wordlist: called CC=17 SCAR=32.1
Pod documented
98
99 1 my $wl;
100 1 ▶ if (defined $args->{'wl'} and ref $args->{'wl'} eq 'WordLists::WordList')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'wl'} and ref $args->{'wl'} eq 'WordLists::WordList') { } 0 1 1
Condition33%1/3
defined $args->{"wl"} and ref $args->{"wl"} eq "WordLists::WordList"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $args->{"wl"} and ref $args->{"wl"} eq "WordLists::WordList"
defined $args->{"wl"} ref $args->{"wl"} eq "WordLists::WordList"
Truth table33%1/3
A and B
A= defined $args->{"wl"}B= ref $args->{"wl"} eq "WordLists::WordList"
ABresult
0X0
100
111
101 {
102 0 $wl=$args->{'wl'};
103 }
104 else
105 {
106 1 $wl = WordLists::WordList->new;
107 }
108 1 foreach my $sSentence (@{$self->{tagger}->get_sentences($sUntagged)})
109 {
110 1 my $taggedMS;
111 1 $taggedMS = $self->{tagger}->add_tags( $sSentence );
112 1 ▶ while ($taggedMS =~ m`<([a-z]+)>([^<]+)</\1>`g)
Branch: 1 branch
BranchTrueFalseTotal
if ($taggedMS =~ m[<([a-z]+)>([^<]+)</\1>]g) 11 1 12
113 {
114 #print "\n$1\t$2";
115 11 my $sHW = _norm_word($2);
116 11 my $sPosCode = $1;
117 11 my $bNext;
118 11 foreach (@ignore_pos_codes) #
119 {
120 125 ▶ if ($sPosCode eq $_)
Branch: 1 branch
BranchTrueFalseTotal
if ($sPosCode eq $_) 2 123 125
121 {
122 2 $bNext++;
123 2 last;
124 }
125 }
126 11 ▶ next if $bNext;
Branch: 1 branch
BranchTrueFalseTotal
if $bNext 2 9 11
127 9 my $sPos = human_pos($sPosCode);
128 9 my $sense = WordLists::Sense->new();
129 9 $sense->set_hw($sHW);
130 9 $sense->set_pos($sPos);
131 9 $sense->set_eg($sSentence);
132 9 $sense->set_poscode($sPosCode);
133 9 ▶ if (defined $args->{'callback_on_make_sense'} and ref $args->{'callback_on_make_sense'} eq ref sub{})
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"callback_on_make_sense"} and ref $args->{"callback_on_make_sense"} eq ref sub { } ) 0 9 9
Condition33%1/3
defined $args->{"callback_on_make_sense"} and ref $args->{"callback_on_make_sense"} eq ref sub { }
!ll&&!rl&&r
900
MC/DC0%0/2
defined $args->{"callback_on_make_sense"} and ref $args->{"callback_on_make_sense"} eq ref sub { }
defined $args->{"callback_on_make_sense"} ref $args->{"callback_on_make_sense"} eq ref sub { }
Truth table33%1/3
A and B
A= defined $args->{"callback_on_make_sense"}B= ref $args->{"callback_on_make_sense"} eq ref sub { }
ABresult
0X0
100
111
134 {
135 0 &{$args->{'callback_on_make_sense'}}($sense);
136 }
137 9 my @senses = $wl->get_senses_for($sense->get_hw, $sense->get_pos);
138 9 ▶ if (@senses and !$args->{'keep_repeats'})
Branch: 1 branch
BranchTrueFalseTotal
if (@senses and not $args->{'keep_repeats'}) { } 1 8 9
Condition66%2/3
@senses and not $args->{"keep_repeats"}
!ll&&!rl&&r
801
MC/DC50%1/2
@senses and not $args->{"keep_repeats"}
@senses not $args->{"keep_repeats"}
Truth table66%2/3
A and not(B)
A= @sensesB= not $args->{"keep_repeats"}
ABresult
0X0
100
111
139 {
140
141 }
142 else
143 {
144 8 ▶ if (defined $args->{'test_sense_before_add'} and ref $args->{'test_sense_before_add'} eq ref sub {} and !&{$args->{'test_sense_before_add'}}($sense))
Subroutine __ANON__: not called CC=1 SCAR=2.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'test_sense_before_add'} and ref $args->{'test_sense_before_add'} eq ref sub { } and not &{$args->{'test_sense_before_add'};}($sense)) { } 0 8 8
Condition33%2/6
defined $args->{"test_sense_before_add"} and ref $args->{"test_sense_before_add"} eq ref sub { } and not &{$args->{"test_sense_before_add"};}($sense)
!ll&&!rl&&r
800
defined $args->{"test_sense_before_add"} and ref $args->{"test_sense_before_add"} eq ref sub { }
!ll&&!rl&&r
800
MC/DC0%0/3
defined $args->{"test_sense_before_add"} and ref $args->{"test_sense_before_add"} eq ref sub { } and not &{$args->{"test_sense_before_add"};}($sense)
defined $args->{"test_sense_before_add"} ref $args->{"test_sense_before_add"} eq ref sub { } not &{$args->{"test_sense_before_add"};}($sense)
Truth table25%1/4
A and B and not(C)
A= defined $args->{"test_sense_before_add"}B= ref $args->{"test_sense_before_add"} eq ref sub { } C= not &{$args->{"test_sense_before_add"};}($sense)
ABCresult
0XX0
10X0
1100
1111
145 {
146 }
147 else
148 {
149 8 ▶ if (defined $args->{'callback_on_add_sense'} and ref $args->{'callback_on_add_sense'} eq ref sub{})
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"callback_on_add_sense"} and ref $args->{"callback_on_add_sense"} eq ref sub { } ) 0 8 8
Condition33%1/3
defined $args->{"callback_on_add_sense"} and ref $args->{"callback_on_add_sense"} eq ref sub { }
!ll&&!rl&&r
800
MC/DC0%0/2
defined $args->{"callback_on_add_sense"} and ref $args->{"callback_on_add_sense"} eq ref sub { }
defined $args->{"callback_on_add_sense"} ref $args->{"callback_on_add_sense"} eq ref sub { }
Truth table33%1/3
A and B
A= defined $args->{"callback_on_add_sense"}B= ref $args->{"callback_on_add_sense"} eq ref sub { }
ABresult
0X0
100
111
150 {
151 0 &{$args->{'callback_on_add_sense'}}($sense);
152 }
153 8 $wl->add_sense($sense);
154 }
155 }
156 };
157 }
158 1 return $wl;
159 }
160 =pod
161
162 =head1 NAME
163
164 WordLists::Tag::Tagger
165
166 =head1 SYNOPSIS
167
168 my $tagger = WordLists::Tag::Tagger->new();
169 my $wl = $tagger->get_wordlist('The quick brown fox jumped over the lazy dog');
170
171 =head1 DESCRIPTION
172
173 Uses L<Lingua::EN::Tagger> to do various things with strings, chielfly to create a L<WordLists::WordList> out of a document, e.g. to use as a basis for a glossary.
174
175 =head1 METHODS
176
177 =head3 get_wordlist
178
179 Uses L<Lingua::EN::Tagger> to create a L<WordLists::WordList> out of a string (e.g. a manuscript).
180
181 L<Lingua::EN::Tagger> allows splitting into sentences, and these sentences become C<eg> fields in the L<WordLists::Sense>s generated.
182
183 Only the first instance of each headword / part of speech combination is entered into the list, unless the third argument has a key C<keep_repeats> with a true value.
184
185 The fields populated are: C<hw>, C<pos>, C<eg>, and C<poscode>, which is the original part of speech code outputted by the tagger.
186
187 The third argument is a hashref which allows you to configure several options.
188
189 C<callback_on_add_sense> should be a coderef. It is passed the sense immediately before it is added to the wordlist, e.g. if you want to add a unit.
190
191 C<callback_on_make_sense> should be a coderef. It is passed the sense before the wordlist is tested for inclusion. This is an opportunity to further normalise parts of speech.
192
193 C<keep_repeats> is a flag, which, if set, prevents the code from removing repetitions.
194
195 =head1 TODO
196
197 C<ignore> is a wordlist or arrayref whose elements are L<WordLists::Sense> objects or plain hashrefs of the form C<< {hw=>'head', pos =>'n'} >>, or headwords as strings. If these words are found, they are not added to the list. (not yet implemented!)
198
199 =head1 BUGS
200
201 Please use the Github issues tracker.
202
203 =head1 LICENSE
204
205 Copyright 2011-2012 © Cambridge University Press. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
206
207 =cut
« Serialise/Simple.pm ↑ Summary Dict.pm »