NAME

Wikibase::Datatype::Struct::Lexeme - Wikibase lexeme structure serialization.

SYNOPSIS

use Wikibase::Datatype::Struct::Lexeme qw(obj2struct struct2obj);

my $struct_hr = obj2struct($obj, $base_uri);
my $obj = struct2obj($struct_hr);

DESCRIPTION

This conversion is between objects defined in Wikibase::Datatype and structures serialized via JSON to MediaWiki.

SUBROUTINES

obj2struct

my $struct_hr = obj2struct($obj, $base_uri);

Convert Wikibase::Datatype::Lexeme instance to structure. $base_uri is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).

Returns reference to hash with structure.

struct2obj

my $obj = struct2obj($struct_hr);

Convert structure of lexeme to object.

Returns Wikibase::Datatype::Lexeme instance.

ERRORS

obj2struct():
        Base URI is required.
        Object doesn't exist.
        Object isn't 'Wikibase::Datatype::Form'.

struct2obj():
        Structure isn't for 'lexeme' type.

EXAMPLE1

use strict;
use warnings;

use Data::Printer;
use Wikibase::Datatype::Form;
use Wikibase::Datatype::Lexeme;
use Wikibase::Datatype::Statement;
use Wikibase::Datatype::Struct::Lexeme qw(obj2struct);
use Wikibase::Datatype::Value::Monolingual;

# Statement.
my $statement = Wikibase::Datatype::Statement->new(
        # instance of (P31) human (Q5)
        'snak' => Wikibase::Datatype::Snak->new(
                'datatype' => 'wikibase-item',
                'datavalue' => Wikibase::Datatype::Value::Item->new(
                        'value' => 'Q5',
                ),
                'property' => 'P31',
        ),
);

# Form.
my $form = Wikibase::Datatype::Form->new(
        'grammatical_features' => [
                Wikibase::Datatype::Value::Item->new(
                        'value' => 'Q163012',
                ),
                Wikibase::Datatype::Value::Item->new(
                        'value' => 'Q163014',
                ),
        ],
        'id' => 'ID',
        'representations' => [
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'en',
                        'value' => 'Representation en',
                ),
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'cs',
                        'value' => 'Representation cs',
                ),
        ],
        'statements' => [
                $statement,
        ],
);

# Sense.
my $sense = Wikibase::Datatype::Sense->new(
        'glosses' => [
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'en',
                        'value' => 'Glosse en',
                ),
                Wikibase::Datatype::Value::Monolingual->new(
                        'language' => 'cs',
                        'value' => 'Glosse cs',
                ),
        ],
        'id' => 'ID',
        'statements' => [
                $statement,
        ],
);

my $lexeme = Wikibase::Datatype::Lexeme->new(
        'forms' => [
                $form,
        ],
        'senses' => [
                $sense,
        ],
        'statements' => [
                $statement,
        ],
);

# Get structure.
my $struct_hr = obj2struct($lexeme, 'http://test.wikidata.org/entity/');

# Dump to output.
p $struct_hr;

# Output:
# \ {
#     claims   {
#         P31   [
#             [0] {
#                 mainsnak   {
#                     datatype    "wikibase-item",
#                     datavalue   {
#                         type    "wikibase-entityid",
#                         value   {
#                             entity-type   "item",
#                             id            "Q5",
#                             numeric-id    5
#                         }
#                     },
#                     property    "P31",
#                     snaktype    "value"
#                 },
#                 rank       "normal",
#                 type       "statement"
#             }
#         ]
#     },
#     forms    [
#         [0] {
#             claims                {
#                 P31   [
#                     [0] {
#                         mainsnak   {
#                             datatype    "wikibase-item",
#                             datavalue   {
#                                 type    "wikibase-entityid",
#                                 value   {
#                                     entity-type   "item",
#                                     id            "Q5",
#                                     numeric-id    5
#                                 }
#                             },
#                             property    "P31",
#                             snaktype    "value"
#                         },
#                         rank       "normal",
#                         type       "statement"
#                     }
#                 ]
#             },
#             grammaticalFeatures   [
#                 [0] "Q163012",
#                 [1] "Q163014"
#             ],
#             id                    "ID",
#             representations       {
#                 cs   {
#                     language   "cs",
#                     value      "Representation cs"
#                 },
#                 en   {
#                     language   "en",
#                     value      "Representation en"
#                 }
#             }
#         }
#     ],
#     ns       0,
#     senses   [
#         [0] {
#             claims    {
#                 P31   [
#                     [0] {
#                         mainsnak   {
#                             datatype    "wikibase-item",
#                             datavalue   {
#                                 type    "wikibase-entityid",
#                                 value   {
#                                     entity-type   "item",
#                                     id            "Q5",
#                                     numeric-id    5
#                                 }
#                             },
#                             property    "P31",
#                             snaktype    "value"
#                         },
#                         rank       "normal",
#                         type       "statement"
#                     }
#                 ]
#             },
#             glosses   {
#                 cs   {
#                     language   "cs",
#                     value      "Glosse cs"
#                 },
#                 en   {
#                     language   "en",
#                     value      "Glosse en"
#                 }
#             },
#             id        "ID"
#         }
#     ],
#     type     "lexeme"
# }

EXAMPLE2

use strict;
use warnings;

use Data::Printer;
use Unicode::UTF8 qw(decode_utf8);
use Wikibase::Datatype::Struct::Lexeme qw(struct2obj);

# Lexeme structure.
my $struct_hr = {
        'claims' => {
                'P5185' => [{
                        'mainsnak' => {
                                'datavalue' => {
                                        'type' => 'wikibase-entityid',
                                        'value' => {
                                                'entity-type' => 'item',
                                                'id' => 'Q499327',
                                                'numeric-id' => 499327,
                                        },
                                },
                                'datatype' => 'wikibase-item',
                                'property' => 'P5185',
                                'snaktype' => 'value',
                        },
                        'rank' => 'normal',
                        'references' => [{
                                'snaks' => {
                                        'P214' => [{
                                                'datavalue' => {
                                                        'type' => 'string',
                                                        'value' => '113230702',
                                                },
                                                'datatype' => 'external-id',
                                                'property' => 'P214',
                                                'snaktype' => 'value',
                                        }],
                                        'P248' => [{
                                                'datavalue' => {
                                                        'type' => 'wikibase-entityid',
                                                        'value' => {
                                                                'entity-type' => 'item',
                                                                'id' => 'Q53919',
                                                                'numeric-id' => 53919,
                                                        },
                                                },
                                                'datatype' => 'wikibase-item',
                                                'property' => 'P248',
                                                'snaktype' => 'value',
                                        }],
                                        'P813' => [{
                                                'datavalue' => {
                                                        'type' => 'time',
                                                        'value' => {
                                                                'after' => 0,
                                                                'before' => 0,
                                                                'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
                                                                'precision' => 11,
                                                                'time' => '+2013-12-07T00:00:00Z',
                                                                'timezone' => 0,
                                                        },
                                                },
                                                'datatype' => 'time',
                                                'property' => 'P813',
                                                'snaktype' => 'value',
                                        }],
                                },
                                'snaks-order' => [
                                        'P248',
                                        'P214',
                                        'P813',
                                ],
                        }],
                        'type' => 'statement',
                }],
        },
        'forms' => [{
                'claims' => {
                        'P898' => [{
                                'mainsnak' => {
                                        'datavalue' => {
                                                'type' => 'string',
                                                'value' => decode_utf8('pɛs'),
                                        },
                                        'datatype' => 'string',
                                        'property' => 'P898',
                                        'snaktype' => 'value',
                                },
                                'rank' => 'normal',
                                'type' => 'statement',
                        }],
                },
                'grammaticalFeatures' => [
                        'Q110786',
                        'Q131105',
                ],
                'id' => 'L469-F1',
                'representations' => {
                        'cs' => {
                                'language' => 'cs',
                                'value' => 'pes',
                        },
                },
        }],
        'id' => 'L469',
        'language' => 'Q9056',
        'lastrevid' => 1428556087,
        'lemmas' => {
                'cs' => {
                        'language' => 'cs',
                        'value' => 'pes',
                },
        },
        'lexicalCategory' => 'Q1084',
        'modified' => '2022-06-24T12:42:10Z',
        'ns' => 146,
        'pageid' => 54393954,
        'senses' => [{
                'claims' => {
                        'P18' => [{
                                'mainsnak' => {
                                        'datavalue' => {
                                                'type' => 'string',
                                                'value' => 'Canadian Inuit Dog.jpg',
                                        },
                                        'datatype' => 'commonsMedia',
                                        'property' => 'P18',
                                        'snaktype' => 'value',
                                },
                                'rank' => 'normal',
                                'type' => 'statement',
                        }],
                        'P5137' => [{
                                'mainsnak' => {
                                        'datavalue' => {
                                                'type' => 'wikibase-entityid',
                                                'value' => {
                                                        'entity-type' => 'item',
                                                        'id' => 'Q144',
                                                        'numeric-id' => 144,
                                                },
                                        },
                                        'datatype' => 'wikibase-item',
                                        'property' => 'P5137',
                                        'snaktype' => 'value',
                                },
                                'rank' => 'normal',
                                'type' => 'statement',
                        }],
                },
                'glosses' => {
                        'cs' => {
                                'language' => 'cs',
                                'value' => decode_utf8('psovitá šelma chovaná jako domácí zvíře'),
                        },
                        'en' => {
                                'language' => 'en',
                                'value' => 'domesticated mammal related to the wolf',
                        },
                },
                'id' => 'L469-S1',
        }],
        'title' => 'Lexeme:L469',
        'type' => 'lexeme',
 };

# Get object.
my $obj = struct2obj($struct_hr);

# Dump object.
p $obj;

# Output:
# Wikibase::Datatype::Lexeme  {
#     parents: Mo::Object
#     public methods (5):
#         BUILD
#         Error::Pure:
#             err
#         Mo::utils:
#             check_array_object, check_number
#         Wikibase::Datatype::Utils:
#             check_entity
#     private methods (0)
#     internals: {
#         forms              [
#             [0] Wikibase::Datatype::Form
#         ],
#         id                 "L469",
#         language           "Q9056",
#         lastrevid          1428556087,
#         lemmas             [
#             [0] Wikibase::Datatype::Value::Monolingual
#         ],
#         lexical_category   "Q1084",
#         modified           "2022-06-24T12:42:10Z" (dualvar: 2022),
#         ns                 146,
#         page_id            54393954,
#         senses             [
#             [0] Wikibase::Datatype::Sense
#         ],
#         statements         [
#             [0] Wikibase::Datatype::Statement
#         ],
#         title              "Lexeme:L469"
#     }
# }

DEPENDENCIES

Error::Pure, Exporter, Readonly, Wikibase::Datatype::Form, Wikibase::Datatype::Lexeme, Wikibase::Datatype::Struct::Form, Wikibase::Datatype::Struct::Language, Wikibase::Datatype::Struct::Sense, Wikibase::Datatype::Struct::Statement, Wikibase::Datatype::Value::Item.

SEE ALSO

Wikibase::Datatype::Form

Wikibase form datatype.

Wikibase::Datatype::Sense

Wikibase sense datatype.

Wikibase::Datatype::Struct

Wikibase structure serialization.

REPOSITORY

https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2020-2024 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.13