App-skos2jskos

 view release on metacpan or  search on metacpan

script/skos2jskos  view on Meta::CPAN

    'directory|d=s', 'name|n=s', 'language|l=s', 'quiet|q',
    'verbose|v',     'debug',    'version',
) or exit 1;
say "skos2jskos $VERSION" and exit if $opt{version};
if ( $opt{help} or ( !$opt{sparql} && !@ARGV ) ) {
    pod2usage( -exitval => 1, -indent => 2 );
}

$opt{directory} //= '.';
$opt{directory} =~ s{/$}{};
die "output directory not found: " . $opt{directory} . "\n"
  unless -d $opt{directory};

$opt{language} //= 'en';
$opt{verbose} = 2 if $opt{debug};

## Logging methods
use Term::ANSIColor;
my $colored = -t STDOUT;    ## no critic

sub error($) {              ## no critic
    say STDERR ( $colored ? colored( $_[0], 'red' ) : $_[0] );
}

sub fatal($) {              ## no critic
    error $_[0];
    exit 1;
}

sub warning($) {            ## no critic
    say STDERR ( $colored ? colored( $_[0], 'yellow' ) : $_[0] );
}

sub info($) {               ## no critic
    return if $opt{quiet};
    say( $colored ? colored( $_[0], 'green' ) : $_[0] );
}

sub debug($) {              ## no critic
    return unless $opt{verbose};
    say( $colored ? colored( $_[0], 'white' ) : $_[0] );
}

sub trace($) {              ## no critic
    return unless $opt{verbose} > 1;
    say $_[0];
}

## check where to get RDF data from
use RDF::Trine;
use RDF::Query;
use RDF::Query::Client;

my $source = $opt{sparql} || RDF::Trine::Model->new;

if ( $opt{sparql} ) {
    info "Getting RDF from SPARQL endpoint " . $opt{sparql};
}
elsif ( $ARGV[0] =~ qr{^https?://} ) {
    info "Reading RDF from $ARGV[0]";
    RDF::Trine->default_useragent->ssl_opts( verify_hostname => 0 );
    RDF::Trine::Parser->parse_url_into_model( $ARGV[0], $source );
    debug $source->size . " triples";
}
else {
    info "Reading RDF files";
    my $size = 0;
    foreach my $file (@ARGV) {
        my $parser = RDF::Trine::Parser->guess_parser_by_filename($file);
        $parser->parse_file_into_model( "file://$file", $file, $source );
        debug $source->size - $size . " triples from $file";
        $size = $source->size;
    }
}

### SPARQL query method
sub sparql {
    my ($query) = @_;

    $query = <<'SPARQL'. $query . "\n}";
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX vann: <http://purl.org/vocab/vann/> 
SELECT * WHERE {
SPARQL

    trace $query;

    my $q =
      $opt{sparql}
      ? RDF::Query::Client->new($query)
      : RDF::Query->new($query);

    $q->execute($source) // RDF::Trine::Iterator->new( [], 'bindings', [] );
}

sub language {
    $_[0]->literal_value_language // do {
        warning "missing language tag of " . $_[0];
        $opt{language};
    };
}

## Conversion methods
sub prefLabel {
    my ( $entity, $label ) = @_;
    return unless isLiteral( $label, 'prefLabel' );
    my $language = language($label);
    my $value    = $label->literal_value;
    if ( ( $entity->{prefLabel}->{$language} // $value ) ne $value ) {
        warning "multiple prefLabel\@$language for " . $entity->{uri};
    }
    $entity->{prefLabel}->{$language} = $value;
}

sub equal {
    my ( $a, $b ) = @_;
    return 1 if $a eq $b or $a == $b;
    if ( 'HASH' eq ref $a && 'HASH' eq ref $b ) {
        return 1 if ( $a->{uri} && $a->{uri} eq $b->{uri} );
        return $JSON->encode($a) eq $JSON->encode($b);



( run in 0.511 second using v1.01-cache-2.11-cpan-5735350b133 )