File Coverage

blib/lib/Wikibase/Datatype/Sense.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Sense;
2              
3 25     25   676921 use strict;
  25         52  
  25         1082  
4 25     25   140 use warnings;
  25         47  
  25         1507  
5              
6 25     25   3925 use Mo qw(build default is);
  25         6014  
  25         164  
7 25     25   33300 use Mo::utils qw(check_number_of_items);
  25         126943  
  25         1510  
8 25     25   6128 use Mo::utils::Array qw(check_array_object);
  25         22508  
  25         3844  
9              
10             our $VERSION = 0.39;
11              
12             has glosses => (
13             default => [],
14             is => 'ro',
15             );
16              
17             has id => (
18             is => 'ro',
19             );
20              
21             has statements => (
22             default => [],
23             is => 'ro',
24             );
25              
26             sub BUILD {
27 27     27 0 1148260 my $self = shift;
28              
29             # Check glosses.
30 27         150 check_array_object($self, 'glosses', 'Wikibase::Datatype::Value::Monolingual');
31 25         1090 check_number_of_items($self, 'glosses', 'language', 'Glosse', 'language');
32              
33             # Check statements.
34 24         1308 check_array_object($self, 'statements', 'Wikibase::Datatype::Statement');
35              
36 22         746 return;
37             }
38              
39             1;
40              
41             __END__
42              
43             =pod
44              
45             =encoding utf8
46              
47             =head1 NAME
48              
49             Wikibase::Datatype::Sense - Wikibase sense datatype.
50              
51             =head1 SYNOPSIS
52              
53             use Wikibase::Datatype::Sense;
54              
55             my $obj = Wikibase::Datatype::Sense->new(%params);
56             my $glosses_ar = $obj->glosses;
57             my $id = $obj->id;
58             my $statements_ar = $obj->statements;
59              
60             =head1 DESCRIPTION
61              
62             This datatype is snak class for representing relation between property and value.
63              
64             =head1 METHODS
65              
66             =head2 C<new>
67              
68             my $obj = Wikibase::Datatype::Snak->new(%params);
69              
70             Constructor.
71              
72             Returns instance of object.
73              
74             =over 8
75              
76             =item * C<glosses>
77              
78             Item glosses. One per language.
79             Reference to array with Wikibase::Datatype::Value::Monolingual instances.
80             Parameter is optional.
81              
82             =item * C<id>
83              
84             Id.
85             Parameter is optional.
86              
87             =item * C<statements>
88              
89             Item statements.
90             Reference to array with Wikibase::Datatype::Statement instances.
91             Parameter is optional.
92              
93             =back
94              
95             =head2 C<glosses>
96              
97             my $glosses_ar = $obj->glosses;
98              
99             Get glosses.
100              
101             Returns reference to array with Wikibase::Datatype::Value::Monolingual instances.
102              
103             =head2 C<id>
104              
105             my $id = $obj->id;
106              
107             Get id.
108              
109             Returns string.
110              
111             =head2 C<statements>
112              
113             my $statements_ar = $obj->statements;
114              
115             Get statements.
116              
117             Returns reference to array with Wikibase::Datatype::Statement instances.
118              
119             =head1 ERRORS
120              
121             new():
122             From Mo::utils::check_number_of_items():
123             Glosse for language '%s' has multiple values.
124              
125             From Mo::utils::Array::check_array_object():
126             Parameter 'glosses' must be a array.
127             Parameter 'glosses' with array must contain 'Wikibase::Datatype::Value::Monolingual' objects.
128             Parameter 'statements' must be a array.
129             Parameter 'statements' with array must contain 'Wikibase::Datatype::Statement' objects.
130              
131             =head1 EXAMPLE
132              
133             =for comment filename=create_and_print_sense.pl
134              
135             use strict;
136             use warnings;
137              
138             use Unicode::UTF8 qw(decode_utf8);
139             use Wikibase::Datatype::Sense;
140             use Wikibase::Datatype::Snak;
141             use Wikibase::Datatype::Statement;
142             use Wikibase::Datatype::Value::Item;
143             use Wikibase::Datatype::Value::Monolingual;
144             use Wikibase::Datatype::Value::String;
145              
146             # One sense for Czech noun 'pes'.
147             # https://www.wikidata.org/wiki/Lexeme:L469
148              
149             # Statements.
150             my $statement_item = Wikibase::Datatype::Statement->new(
151             # item for this sense (P5137) dog (Q144)
152             'snak' => Wikibase::Datatype::Snak->new(
153             'datatype' => 'wikibase-item',
154             'datavalue' => Wikibase::Datatype::Value::Item->new(
155             'value' => 'Q144',
156             ),
157             'property' => 'P5137',
158             ),
159             );
160             my $statement_image = Wikibase::Datatype::Statement->new(
161             # image (P5137) 'Canadian Inuit Dog.jpg'
162             'snak' => Wikibase::Datatype::Snak->new(
163             'datatype' => 'commonsMedia',
164             'datavalue' => Wikibase::Datatype::Value::String->new(
165             'value' => 'Canadian Inuit Dog.jpg',
166             ),
167             'property' => 'P18',
168             ),
169             );
170              
171             # Object.
172             my $obj = Wikibase::Datatype::Sense->new(
173             'glosses' => [
174             Wikibase::Datatype::Value::Monolingual->new(
175             'language' => 'en',
176             'value' => 'domesticated mammal related to the wolf',
177             ),
178             Wikibase::Datatype::Value::Monolingual->new(
179             'language' => 'cs',
180             'value' => decode_utf8('psovitá šelma chovaná jako domácí zvíře'),
181             ),
182             ],
183             'id' => 'ID',
184             'statements' => [
185             $statement_item,
186             $statement_image,
187             ],
188             );
189              
190             # Get id.
191             my $id = $obj->id;
192              
193             # Get glosses.
194             my @glosses = map { $_->value.' ('.$_->language.')' } @{$obj->glosses};
195              
196             # Get statements.
197             my $statements_count = @{$obj->statements};
198              
199             # Print out.
200             print "Id: $id\n";
201             print "Glosses:\n";
202             map { print "\t$_\n"; } @glosses;
203             print "Number of statements: $statements_count\n";
204              
205             # Output:
206             # Id: ID
207             # Glosses:
208             # domesticated mammal related to the wolf (en)
209             # psovitá šelma chovaná jako domácí zvíře (cs)
210             # Number of statements: 2
211              
212             =head1 DEPENDENCIES
213              
214             L<Mo>,
215             L<Mo::utils>,
216             L<Mo::utils::Array>.
217              
218             =head1 SEE ALSO
219              
220             =over
221              
222             =item L<Wikibase::Datatype>
223              
224             Wikibase datatypes.
225              
226             =back
227              
228             =head1 REPOSITORY
229              
230             L<https://github.com/michal-josef-spacek/Wikibase-Datatype>
231              
232             =head1 AUTHOR
233              
234             Michal Josef Špaček L<mailto:skim@cpan.org>
235              
236             L<http://skim.cz>
237              
238             =head1 LICENSE AND COPYRIGHT
239              
240             © 2020-2025 Michal Josef Špaček
241              
242             BSD 2-Clause License
243              
244             =head1 VERSION
245              
246             0.39
247              
248             =cut