File: Extract.pm

package info (click to toggle)
libcatmandu-viaf-perl 0.05-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 180 kB
  • sloc: perl: 447; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 3,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package Catmandu::VIAF::API::Extract;

use strict;
use warnings;

use Catmandu::Sane;
use Moo;

has api_response  => (is => 'ro', required => 1);
has lang          => (is => 'ro', default => 'nl-NL');
has fallback_lang => (is => 'ro', default => 'en-US');

sub single {
    my $self = shift;
    my $descriptions = $self->api_response->{'rdf:Description'};
    my $person;
    foreach my $description (@{$descriptions}) {
        if ($self->is_person($description->{'rdf:type'}) == 1) {
            $person = $description;
            last;
        }
    }

    if (ref($person) ne "HASH") {
        return {};
    }

    return $person;
}

sub pref_label {
    my ($self, $description) = @_;
    my $prefLabel;
    my $prefLabel_fallback;
    my $prefLabel_nolang;
    if (ref($description->{'skos:prefLabel'}) ne 'ARRAY') {
        $description->{'skos:prefLabel'} = [$description->{'skos:prefLabel'}];
    }
    foreach my $label (@{$description->{'skos:prefLabel'}}) {
        if (exists($label->{'xml:lang'})) {
            if ($label->{'xml:lang'} eq $self->lang) {
                $prefLabel = $label->{'content'};
                last;
            }
            if ($label->{'xml:lang'} eq $self->fallback_lang) {
                $prefLabel_fallback = $label->{'content'};
            }
        } else {
            $prefLabel_nolang = $label->{'content'};
        }
    }

    if (defined($prefLabel)) {
        return $prefLabel;
    } elsif (defined($prefLabel_fallback)) {
        return $prefLabel_fallback;
    } else {
        # No guarantee that this isn't undefined
        return $prefLabel_nolang;
    }
}

sub schema_name {
    my ($self, $description) = @_;
    my $name;
    my $name_fallback;
    my $name_nolang;
    if (ref($description->{'schema:name'}) ne 'ARRAY') {
        $description->{'schema:name'} = [$description->{'schema:name'}];
    }

    foreach my $s_name (@{$description->{'schema:name'}}) {
        if (ref($s_name) eq 'ARRAY') {
            if (exists($s_name->{'xml:lang'})) {
                if ($s_name->{'xml:lang'} eq $self->lang) {
                    $name = $s_name->{'content'};
                    last;
                }
                if ($s_name->{'xml:lang'} eq $self->fallback_lang) {
                    $name_fallback = $s_name->{'content'};
                }
            } else {
                $name_nolang = $s_name->{'content'};
            }
        }
    }
    if (defined($name)) {
        return $name;
    } elsif (defined($name_fallback)) {
        return $name_fallback;
    } else {
        return $name_nolang;
    }
}

sub name {
    my ($self, $description) = @_;
    my $name = $self->pref_label($description);

    if (!defined($name)) {
        $name = $self->schema_name($description);
    }
    return $name;
}

sub is_person {
    my ($self, $rdf_types) = @_;
    if (ref($rdf_types) ne 'ARRAY') {
        $rdf_types = [$rdf_types];
    }
    foreach my $rdf_type (@{$rdf_types}) {
        if ($rdf_type->{'rdf:resource'} eq 'http://schema.org/Person') {
            return 1;
            last;
        }
    }
    return 0;
}

1;
__END__