File Coverage

blib/lib/Wikibase/Datatype/Struct/Lexeme.pm
Criterion Covered Total %
statement 106 106 100.0
branch 36 36 100.0
condition 7 9 77.7
subroutine 14 14 100.0
pod 2 2 100.0
total 165 167 98.8


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Lexeme;
2              
3 4     4   1055867 use base qw(Exporter);
  4         48  
  4         416  
4 4     4   27 use strict;
  4         11  
  4         81  
5 4     4   22 use warnings;
  4         8  
  4         103  
6              
7 4     4   1388 use Error::Pure qw(err);
  4         23838  
  4         104  
8 4     4   172 use Readonly;
  4         15  
  4         135  
9 4     4   1445 use Wikibase::Datatype::Form;
  4         16367  
  4         134  
10 4     4   1487 use Wikibase::Datatype::Lexeme;
  4         1647311  
  4         169  
11 4     4   1940 use Wikibase::Datatype::Struct::Form;
  4         12  
  4         198  
12 4     4   31 use Wikibase::Datatype::Struct::Language;
  4         10  
  4         121  
13 4     4   1837 use Wikibase::Datatype::Struct::Sense;
  4         11  
  4         219  
14 4     4   30 use Wikibase::Datatype::Struct::Statement;
  4         10  
  4         106  
15 4     4   21 use Wikibase::Datatype::Value::Item;
  4         8  
  4         3198  
16              
17             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
18              
19             our $VERSION = 0.11;
20              
21             sub obj2struct {
22 7     7 1 34021 my ($obj, $base_uri) = @_;
23              
24 7 100       24 if (! defined $obj) {
25 1         5 err "Object doesn't exist.";
26             }
27 6 100       34 if (! $obj->isa('Wikibase::Datatype::Lexeme')) {
28 1         8 err "Object isn't 'Wikibase::Datatype::Lexeme'.";
29             }
30 5 100       13 if (! defined $base_uri) {
31 1         25 err 'Base URI is required.';
32             }
33              
34 4         11 my $struct_hr = {
35             'type' => 'lexeme',
36             };
37              
38             # Forms.
39 4         7 foreach my $form (@{$obj->forms}) {
  4         20  
40 1   50     21 $struct_hr->{'forms'} //= [];
41 1         3 push @{$struct_hr->{'forms'}},
  1         6  
42             Wikibase::Datatype::Struct::Form::obj2struct($form, $base_uri);
43             }
44              
45             # Id.
46 4 100       52 if (defined $obj->id) {
47 1         8 $struct_hr->{'id'} = $obj->id;
48             }
49              
50             # Last revision id.
51 4 100       48 if (defined $obj->lastrevid) {
52 1         18 $struct_hr->{'lastrevid'} = $obj->lastrevid;
53             }
54              
55             # Language.
56 4 100       40 if (defined $obj->language) {
57 1         8 $struct_hr->{'language'} = $obj->language;
58             }
59              
60             # Lemmas.
61 4         30 foreach my $lemma (@{$obj->lemmas}) {
  4         12  
62 1         12 $struct_hr->{'lemmas'}->{$lemma->language}
63             = Wikibase::Datatype::Struct::Language::obj2struct($lemma);
64             }
65              
66             # Lexical category.
67 4 100       46 if (defined $obj->lexical_category) {
68 1         9 $struct_hr->{'lexicalCategory'} = $obj->lexical_category;
69             }
70              
71             # Modified date.
72 4 100       40 if (defined $obj->modified) {
73 1         10 $struct_hr->{'modified'} = $obj->modified;
74             }
75              
76             # Namespace.
77 4 100       31 if (defined $obj->ns) {
78 3         33 $struct_hr->{'ns'} = $obj->ns;
79             }
80              
81             # Page id.
82 4 100       38 if (defined $obj->page_id) {
83 1         9 $struct_hr->{'pageid'} = $obj->page_id;
84             }
85              
86             # Senses.
87 4         27 foreach my $sense (@{$obj->senses}) {
  4         22  
88 1   50     24 $struct_hr->{'senses'} //= [];
89 1         3 push @{$struct_hr->{'senses'}},
  1         11  
90             Wikibase::Datatype::Struct::Sense::obj2struct($sense, $base_uri);
91             }
92              
93             # Statements.
94 4         28 foreach my $statement (@{$obj->statements}) {
  4         12  
95 3   100     28 $struct_hr->{'claims'}->{$statement->snak->property} //= [];
96 3         53 push @{$struct_hr->{'claims'}->{$statement->snak->property}},
  3         11  
97             Wikibase::Datatype::Struct::Statement::obj2struct($statement, $base_uri);
98             }
99              
100             # Title.
101 4 100       32 if (defined $obj->title) {
102 1         13 $struct_hr->{'title'} = $obj->title;
103             }
104              
105 4         51 return $struct_hr;
106             }
107              
108             sub struct2obj {
109 5     5 1 13366 my $struct_hr = shift;
110              
111 5 100 100     33 if (! exists $struct_hr->{'type'} || $struct_hr->{'type'} ne 'lexeme') {
112 2         10 err "Structure isn't for 'lexeme' type.";
113             }
114              
115             # Forms.
116 3         8 my $forms_ar = [];
117 3         6 foreach my $form_hr (@{$struct_hr->{'forms'}}) {
  3         13  
118 1         4 push @{$forms_ar}, Wikibase::Datatype::Struct::Form::struct2obj($form_hr);
  1         5  
119             }
120              
121             # Lemmas.
122 3         7 my $lemmas_ar = [];
123 3         14 foreach my $lang (keys %{$struct_hr->{'lemmas'}}) {
  3         12  
124 1         4 push @{$lemmas_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
125 1         3 $struct_hr->{'lemmas'}->{$lang},
126             );
127             }
128              
129             # Senses.
130 3         8 my $senses_ar = [];
131 3         5 foreach my $sense_hr (@{$struct_hr->{'senses'}}) {
  3         10  
132 1         3 push @{$senses_ar}, Wikibase::Datatype::Struct::Sense::struct2obj($sense_hr);
  1         6  
133             }
134              
135             # Statements.
136 3         7 my $statements_ar = [];
137 3         7 foreach my $property (keys %{$struct_hr->{'claims'}}) {
  3         10  
138 1         4 foreach my $claim_hr (@{$struct_hr->{'claims'}->{$property}}) {
  1         3  
139 1         3 push @{$statements_ar}, Wikibase::Datatype::Struct::Statement::struct2obj(
  1         4  
140             $claim_hr,
141             );
142             }
143             }
144              
145             my $obj = Wikibase::Datatype::Lexeme->new(
146             'forms' => $forms_ar,
147             'id' => $struct_hr->{'id'},
148             'language' => $struct_hr->{'language'},
149             defined $struct_hr->{'lastrevid'} ? ('lastrevid' => $struct_hr->{'lastrevid'}) : (),
150             'lemmas' => $lemmas_ar,
151             defined $struct_hr->{'lexicalCategory'} ? ('lexical_category' => $struct_hr->{'lexicalCategory'}) : (),
152             defined $struct_hr->{'modified'} ? ('modified' => $struct_hr->{'modified'}) : (),
153             defined $struct_hr->{'ns'} ? ('ns' => $struct_hr->{'ns'}) : (),
154             defined $struct_hr->{'pageid'} ? ('page_id' => $struct_hr->{'pageid'}) : (),
155             'senses' => $senses_ar,
156             'statements' => $statements_ar,
157 3 100       52 defined $struct_hr->{'title'} ? ('title' => $struct_hr->{'title'}) : (),
    100          
    100          
    100          
    100          
    100          
158             );
159              
160 3         572 return $obj;
161             }
162              
163             1;
164              
165             __END__
166              
167             =pod
168              
169             =encoding utf8
170              
171             =head1 NAME
172              
173             Wikibase::Datatype::Struct::Lexeme - Wikibase lexeme structure serialization.
174              
175             =head1 SYNOPSIS
176              
177             use Wikibase::Datatype::Struct::Lexeme qw(obj2struct struct2obj);
178              
179             my $struct_hr = obj2struct($obj, $base_uri);
180             my $obj = struct2obj($struct_hr);
181              
182             =head1 DESCRIPTION
183              
184             This conversion is between objects defined in Wikibase::Datatype and structures
185             serialized via JSON to MediaWiki.
186              
187             =head1 SUBROUTINES
188              
189             =head2 C<obj2struct>
190              
191             my $struct_hr = obj2struct($obj, $base_uri);
192              
193             Convert Wikibase::Datatype::Lexeme instance to structure.
194             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
195              
196             Returns reference to hash with structure.
197              
198             =head2 C<struct2obj>
199              
200             my $obj = struct2obj($struct_hr);
201              
202             Convert structure of lexeme to object.
203              
204             Returns Wikibase::Datatype::Lexeme instance.
205              
206             =head1 ERRORS
207              
208             obj2struct():
209             Base URI is required.
210             Object doesn't exist.
211             Object isn't 'Wikibase::Datatype::Form'.
212              
213             struct2obj():
214             Structure isn't for 'lexeme' type.
215              
216             =head1 EXAMPLE1
217              
218             =for comment filename=obj2struct_lexeme.pl
219              
220             use strict;
221             use warnings;
222              
223             use Data::Printer;
224             use Wikibase::Datatype::Form;
225             use Wikibase::Datatype::Lexeme;
226             use Wikibase::Datatype::Statement;
227             use Wikibase::Datatype::Struct::Lexeme qw(obj2struct);
228             use Wikibase::Datatype::Value::Monolingual;
229              
230             # Statement.
231             my $statement = Wikibase::Datatype::Statement->new(
232             # instance of (P31) human (Q5)
233             'snak' => Wikibase::Datatype::Snak->new(
234             'datatype' => 'wikibase-item',
235             'datavalue' => Wikibase::Datatype::Value::Item->new(
236             'value' => 'Q5',
237             ),
238             'property' => 'P31',
239             ),
240             );
241              
242             # Form.
243             my $form = Wikibase::Datatype::Form->new(
244             'grammatical_features' => [
245             Wikibase::Datatype::Value::Item->new(
246             'value' => 'Q163012',
247             ),
248             Wikibase::Datatype::Value::Item->new(
249             'value' => 'Q163014',
250             ),
251             ],
252             'id' => 'ID',
253             'representations' => [
254             Wikibase::Datatype::Value::Monolingual->new(
255             'language' => 'en',
256             'value' => 'Representation en',
257             ),
258             Wikibase::Datatype::Value::Monolingual->new(
259             'language' => 'cs',
260             'value' => 'Representation cs',
261             ),
262             ],
263             'statements' => [
264             $statement,
265             ],
266             );
267              
268             # Sense.
269             my $sense = Wikibase::Datatype::Sense->new(
270             'glosses' => [
271             Wikibase::Datatype::Value::Monolingual->new(
272             'language' => 'en',
273             'value' => 'Glosse en',
274             ),
275             Wikibase::Datatype::Value::Monolingual->new(
276             'language' => 'cs',
277             'value' => 'Glosse cs',
278             ),
279             ],
280             'id' => 'ID',
281             'statements' => [
282             $statement,
283             ],
284             );
285              
286             my $lexeme = Wikibase::Datatype::Lexeme->new(
287             'forms' => [
288             $form,
289             ],
290             'senses' => [
291             $sense,
292             ],
293             'statements' => [
294             $statement,
295             ],
296             );
297              
298             # Get structure.
299             my $struct_hr = obj2struct($lexeme, 'http://test.wikidata.org/entity/');
300              
301             # Dump to output.
302             p $struct_hr;
303              
304             # Output:
305             # \ {
306             # claims {
307             # P31 [
308             # [0] {
309             # mainsnak {
310             # datatype "wikibase-item",
311             # datavalue {
312             # type "wikibase-entityid",
313             # value {
314             # entity-type "item",
315             # id "Q5",
316             # numeric-id 5
317             # }
318             # },
319             # property "P31",
320             # snaktype "value"
321             # },
322             # rank "normal",
323             # type "statement"
324             # }
325             # ]
326             # },
327             # forms [
328             # [0] {
329             # claims {
330             # P31 [
331             # [0] {
332             # mainsnak {
333             # datatype "wikibase-item",
334             # datavalue {
335             # type "wikibase-entityid",
336             # value {
337             # entity-type "item",
338             # id "Q5",
339             # numeric-id 5
340             # }
341             # },
342             # property "P31",
343             # snaktype "value"
344             # },
345             # rank "normal",
346             # type "statement"
347             # }
348             # ]
349             # },
350             # grammaticalFeatures [
351             # [0] "Q163012",
352             # [1] "Q163014"
353             # ],
354             # id "ID",
355             # representations {
356             # cs {
357             # language "cs",
358             # value "Representation cs"
359             # },
360             # en {
361             # language "en",
362             # value "Representation en"
363             # }
364             # }
365             # }
366             # ],
367             # ns 0,
368             # senses [
369             # [0] {
370             # claims {
371             # P31 [
372             # [0] {
373             # mainsnak {
374             # datatype "wikibase-item",
375             # datavalue {
376             # type "wikibase-entityid",
377             # value {
378             # entity-type "item",
379             # id "Q5",
380             # numeric-id 5
381             # }
382             # },
383             # property "P31",
384             # snaktype "value"
385             # },
386             # rank "normal",
387             # type "statement"
388             # }
389             # ]
390             # },
391             # glosses {
392             # cs {
393             # language "cs",
394             # value "Glosse cs"
395             # },
396             # en {
397             # language "en",
398             # value "Glosse en"
399             # }
400             # },
401             # id "ID"
402             # }
403             # ],
404             # type "lexeme"
405             # }
406              
407             =head1 EXAMPLE2
408              
409             =for comment filename=struct2obj_lexeme.pl
410              
411             use strict;
412             use warnings;
413              
414             use Data::Printer;
415             use Unicode::UTF8 qw(decode_utf8);
416             use Wikibase::Datatype::Struct::Lexeme qw(struct2obj);
417              
418             # Lexeme structure.
419             my $struct_hr = {
420             'claims' => {
421             'P5185' => [{
422             'mainsnak' => {
423             'datavalue' => {
424             'type' => 'wikibase-entityid',
425             'value' => {
426             'entity-type' => 'item',
427             'id' => 'Q499327',
428             'numeric-id' => 499327,
429             },
430             },
431             'datatype' => 'wikibase-item',
432             'property' => 'P5185',
433             'snaktype' => 'value',
434             },
435             'rank' => 'normal',
436             'references' => [{
437             'snaks' => {
438             'P214' => [{
439             'datavalue' => {
440             'type' => 'string',
441             'value' => '113230702',
442             },
443             'datatype' => 'external-id',
444             'property' => 'P214',
445             'snaktype' => 'value',
446             }],
447             'P248' => [{
448             'datavalue' => {
449             'type' => 'wikibase-entityid',
450             'value' => {
451             'entity-type' => 'item',
452             'id' => 'Q53919',
453             'numeric-id' => 53919,
454             },
455             },
456             'datatype' => 'wikibase-item',
457             'property' => 'P248',
458             'snaktype' => 'value',
459             }],
460             'P813' => [{
461             'datavalue' => {
462             'type' => 'time',
463             'value' => {
464             'after' => 0,
465             'before' => 0,
466             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
467             'precision' => 11,
468             'time' => '+2013-12-07T00:00:00Z',
469             'timezone' => 0,
470             },
471             },
472             'datatype' => 'time',
473             'property' => 'P813',
474             'snaktype' => 'value',
475             }],
476             },
477             'snaks-order' => [
478             'P248',
479             'P214',
480             'P813',
481             ],
482             }],
483             'type' => 'statement',
484             }],
485             },
486             'forms' => [{
487             'claims' => {
488             'P898' => [{
489             'mainsnak' => {
490             'datavalue' => {
491             'type' => 'string',
492             'value' => decode_utf8('pɛs'),
493             },
494             'datatype' => 'string',
495             'property' => 'P898',
496             'snaktype' => 'value',
497             },
498             'rank' => 'normal',
499             'type' => 'statement',
500             }],
501             },
502             'grammaticalFeatures' => [
503             'Q110786',
504             'Q131105',
505             ],
506             'id' => 'L469-F1',
507             'representations' => {
508             'cs' => {
509             'language' => 'cs',
510             'value' => 'pes',
511             },
512             },
513             }],
514             'id' => 'L469',
515             'language' => 'Q9056',
516             'lastrevid' => 1428556087,
517             'lemmas' => {
518             'cs' => {
519             'language' => 'cs',
520             'value' => 'pes',
521             },
522             },
523             'lexicalCategory' => 'Q1084',
524             'modified' => '2022-06-24T12:42:10Z',
525             'ns' => 146,
526             'pageid' => 54393954,
527             'senses' => [{
528             'claims' => {
529             'P18' => [{
530             'mainsnak' => {
531             'datavalue' => {
532             'type' => 'string',
533             'value' => 'Canadian Inuit Dog.jpg',
534             },
535             'datatype' => 'commonsMedia',
536             'property' => 'P18',
537             'snaktype' => 'value',
538             },
539             'rank' => 'normal',
540             'type' => 'statement',
541             }],
542             'P5137' => [{
543             'mainsnak' => {
544             'datavalue' => {
545             'type' => 'wikibase-entityid',
546             'value' => {
547             'entity-type' => 'item',
548             'id' => 'Q144',
549             'numeric-id' => 144,
550             },
551             },
552             'datatype' => 'wikibase-item',
553             'property' => 'P5137',
554             'snaktype' => 'value',
555             },
556             'rank' => 'normal',
557             'type' => 'statement',
558             }],
559             },
560             'glosses' => {
561             'cs' => {
562             'language' => 'cs',
563             'value' => decode_utf8('psovitá šelma chovaná jako domácí zvíře'),
564             },
565             'en' => {
566             'language' => 'en',
567             'value' => 'domesticated mammal related to the wolf',
568             },
569             },
570             'id' => 'L469-S1',
571             }],
572             'title' => 'Lexeme:L469',
573             'type' => 'lexeme',
574             };
575              
576             # Get object.
577             my $obj = struct2obj($struct_hr);
578              
579             # Dump object.
580             p $obj;
581              
582             # Output:
583             # Wikibase::Datatype::Lexeme {
584             # parents: Mo::Object
585             # public methods (5):
586             # BUILD
587             # Error::Pure:
588             # err
589             # Mo::utils:
590             # check_array_object, check_number
591             # Wikibase::Datatype::Utils:
592             # check_entity
593             # private methods (0)
594             # internals: {
595             # forms [
596             # [0] Wikibase::Datatype::Form
597             # ],
598             # id "L469",
599             # language "Q9056",
600             # lastrevid 1428556087,
601             # lemmas [
602             # [0] Wikibase::Datatype::Value::Monolingual
603             # ],
604             # lexical_category "Q1084",
605             # modified "2022-06-24T12:42:10Z" (dualvar: 2022),
606             # ns 146,
607             # page_id 54393954,
608             # senses [
609             # [0] Wikibase::Datatype::Sense
610             # ],
611             # statements [
612             # [0] Wikibase::Datatype::Statement
613             # ],
614             # title "Lexeme:L469"
615             # }
616             # }
617              
618             =head1 DEPENDENCIES
619              
620             L<Error::Pure>,
621             L<Exporter>,
622             L<Readonly>,
623             L<Wikibase::Datatype::Form>,
624             L<Wikibase::Datatype::Lexeme>,
625             L<Wikibase::Datatype::Struct::Form>,
626             L<Wikibase::Datatype::Struct::Language>,
627             L<Wikibase::Datatype::Struct::Sense>,
628             L<Wikibase::Datatype::Struct::Statement>,
629             L<Wikibase::Datatype::Value::Item>.
630              
631             =head1 SEE ALSO
632              
633             =over
634              
635             =item L<Wikibase::Datatype::Form>
636              
637             Wikibase form datatype.
638              
639             =item L<Wikibase::Datatype::Sense>
640              
641             Wikibase sense datatype.
642              
643             =item L<Wikibase::Datatype::Struct>
644              
645             Wikibase structure serialization.
646              
647             =back
648              
649             =head1 REPOSITORY
650              
651             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
652              
653             =head1 AUTHOR
654              
655             Michal Josef Špaček L<mailto:skim@cpan.org>
656              
657             L<http://skim.cz>
658              
659             =head1 LICENSE AND COPYRIGHT
660              
661             © 2020-2023 Michal Josef Špaček
662              
663             BSD 2-Clause License
664              
665             =head1 VERSION
666              
667             0.11
668              
669             =cut