| 1 | package PMLTQ::Command::init; | ||||||||||||||||||||||||||||
| 2 | 1 | our $AUTHORITY = 'cpan:MATY'; | |||||||||||||||||||||||||||
| 3 | 1 | $PMLTQ::Command::init::VERSION = '1.2.2'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Initialize empty database | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use PMLTQ::Base 'PMLTQ::Command'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Basename 'dirname'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use PMLTQ::PML2BASE; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use PMLTQ::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Treex::PML::Factory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use YAML::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use List::Util qw/none first/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | has usage => sub { shift->extract_usage }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | my %known_relations = ( | |||||||||||||||||||||||||||
| 18 | 'a-align-links/counterpart.rf' => 'a-node', | ||||||||||||||||||||||||||||
| 19 | 'a-node/p_terminal.rf' => 'p-terminal', | ||||||||||||||||||||||||||||
| 20 | 'a-root/ptree.rf' => 'p-nonterminal', | ||||||||||||||||||||||||||||
| 21 | 'apreds/LM/target.rf' => 'node', | ||||||||||||||||||||||||||||
| 22 | 'local_event/author' => 'user', | ||||||||||||||||||||||||||||
| 23 | 'n-node/a.rf' => 'a-node', | ||||||||||||||||||||||||||||
| 24 | 'nonterminal/coindex.rf' => 'nonterminal', | ||||||||||||||||||||||||||||
| 25 | 'problem/author' => 'user', | ||||||||||||||||||||||||||||
| 26 | 'secedge/idref' => 'nonterminal', | ||||||||||||||||||||||||||||
| 27 | 'st-node/tnode.rfs' => 't-node', | ||||||||||||||||||||||||||||
| 28 | 't-a/aux.rf' => 'a-node', | ||||||||||||||||||||||||||||
| 29 | 't-a/lex.rf' => 'a-node', | ||||||||||||||||||||||||||||
| 30 | 't-align-links/counterpart.rf' => 't-node', | ||||||||||||||||||||||||||||
| 31 | 't-bridging-link/target_node.rf' => 't-node', | ||||||||||||||||||||||||||||
| 32 | 't-coref_text-link/target_node.rf' => 't-node', | ||||||||||||||||||||||||||||
| 33 | 't-discourse-link/a-connectors.rf' => 'a-node', | ||||||||||||||||||||||||||||
| 34 | 't-discourse-link/all_a-connectors.rf' => 'a-node', | ||||||||||||||||||||||||||||
| 35 | 't-discourse-link/t-connectors.rf' => 't-node', | ||||||||||||||||||||||||||||
| 36 | 't-discourse-link/target_node.rf' => 't-node', | ||||||||||||||||||||||||||||
| 37 | 't-node/compl.rf' => 't-node', | ||||||||||||||||||||||||||||
| 38 | 't-node/coref_gram.rf' => 't-node', | ||||||||||||||||||||||||||||
| 39 | 't-node/coref_text.rf' => 't-node', | ||||||||||||||||||||||||||||
| 40 | 't-node/original_parent.rf' => 't-node', | ||||||||||||||||||||||||||||
| 41 | 't-node/src_tnode.rf' => 't-node', | ||||||||||||||||||||||||||||
| 42 | 't-node/val_frame.rf' => 'v-frame', | ||||||||||||||||||||||||||||
| 43 | 't-root/atree.rf' => 'a-root', | ||||||||||||||||||||||||||||
| 44 | 't-root/src_tnode.rf' => 't-node', | ||||||||||||||||||||||||||||
| 45 | 'terminal/coindex.rf' => 'nonterminal', | ||||||||||||||||||||||||||||
| 46 | 'valency_lexicon/owner' => 'user', | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub run { | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | unless (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | say 'Specify at least one schema file'; | |||||||||||||||||||||||||||
| 56 | 0 | return 0; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my ( @layers, @schemas ); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | for my $schema_file (@_) { | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | unless ( -f $schema_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | my $in_resources = File::Spec->catfile( $config->{resources}, $schema_file ); | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | unless ( -f $in_resources ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | say "Schema file '$schema_file' not found"; | |||||||||||||||||||||||||||
| 66 | 0 | return 0; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | $schema_file = $in_resources; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | push @schemas, my $schema = Treex::PML::Factory->createPMLSchema( { filename => $schema_file } ); | |||||||||||||||||||||||||||
| 72 | 0 | PMLTQ::PML2BASE::init(); | |||||||||||||||||||||||||||
| 73 | 0 | my $refs = PMLTQ::PML2BASE::complete_schema_pmlref_list($schema); | |||||||||||||||||||||||||||
| 74 | 0 | PMLTQ::PML2BASE::destroy(); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | push @layers, { | |||||||||||||||||||||||||||
| 77 | name => $schema->get_root_name, | ||||||||||||||||||||||||||||
| 78 | data => '<change this to point to files relative to the data_dir>', | ||||||||||||||||||||||||||||
| 79 | references => {%$refs} | ||||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | my $resources_dir = | ||||||||||||||||||||||||||||
| 84 | @schemas > 1 | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | ? $self->common_prefix( map { dirname( File::Spec->canonpath( $_->get_url ) ) } @schemas ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | : dirname( $schemas[0]->get_url ); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | $_->set_url( File::Spec->abs2rel( $_->get_url, $resources_dir ) ) for (@schemas); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # resolve refs | ||||||||||||||||||||||||||||
| 91 | 0 | $self->try_resolve_references( \@layers, \@schemas ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my $treebank_title = $self->prompt_str( 'Full treebank title', { default => 'Prague Dependency Treebank 3.0' } ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $treebank_id = join '', grep {/[a-z0-9]/} map { substr $_, 0, 1 } split /[^a-z0-9]+/, lc $treebank_title; | |||||||||||||||||||||||||||
| 96 | $treebank_id = $self->prompt_str( | ||||||||||||||||||||||||||||
| 97 | 'Treebank ID (can only contain lowercase letters, numbers and underscores)', { | ||||||||||||||||||||||||||||
| 98 | default => $treebank_id, | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | check => sub { defined $_[0] and length $_[0] and $_[0] =~ m/^[a-z0-9_]+$/ } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
Condition0%0/3
defined $_[0] and length $_[0]
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0]
length $_[0]
Truth table0%0/3
A and B
A= defined $_[0]B= length $_[0]
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $yaml = YAML::Tiny->new( | |||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | treebank_id => $treebank_id, | ||||||||||||||||||||||||||||
| 106 | title => $treebank_title, | ||||||||||||||||||||||||||||
| 107 | resources => $resources_dir, | ||||||||||||||||||||||||||||
| 108 | layers => \@layers | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | say $yaml->write_string; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $save = $self->prompt_yn( 'Save?', { default => 0 } ); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($save) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | my $filename = $self->prompt_str( | |||||||||||||||||||||||||||
| 118 | 'Save as', { | ||||||||||||||||||||||||||||
| 119 | default => 'pmltq.yml' | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | 0 | $yaml->write($filename); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub common_prefix { | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
common_prefix: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 0 | my @paths = @_; | |||||||||||||||||||||||||||
| 129 | 0 | my %prefixes; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | for (@_) { | |||||||||||||||||||||||||||
| 132 | 0 | my @parts = File::Spec->splitdir($_); | |||||||||||||||||||||||||||
| 133 | 0 | my $prefix = shift @parts; | |||||||||||||||||||||||||||
| 134 | 0 | ++$prefixes{$prefix}; | |||||||||||||||||||||||||||
| 135 | 0 | ++$prefixes{ $prefix = File::Spec->catdir( $prefix, $_ ) } for (@parts); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | return first { $prefixes{$_} == @paths } reverse sort keys %prefixes; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub try_resolve_references { | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my ( $self, $layers, $schemas ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
try_resolve_references: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my %type_map; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | for my $schema (@$schemas) { | |||||||||||||||||||||||||||
| 146 | $schema->for_each_decl( | ||||||||||||||||||||||||||||
| 147 | sub { | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my $decl = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $is_node = ( $decl->get_role || '' ) eq '#NODE'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$decl->get_role || ''
MC/DC0%0/1
$decl->get_role || ''
$decl->get_role
Truth table0%0/2
A
A= $decl->get_role
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | return unless $is_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | my $type = PMLTQ::Common::DeclPathToQueryType( $decl->get_decl_path ); | |||||||||||||||||||||||||||
| 153 | 0 | $type_map{$type} = $schema; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 0 | ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | for my $layer (@$layers) { | |||||||||||||||||||||||||||
| 159 | 0 | my $refs = $layer->{references}; | |||||||||||||||||||||||||||
| 160 | 0 | for my $path ( grep { $known_relations{$_} } keys %$refs ) { | |||||||||||||||||||||||||||
| 161 | 0 | my $type = $known_relations{$path}; | |||||||||||||||||||||||||||
| 162 | 0 | my $schema = $type_map{$type}; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | next unless $schema; # check if type exists at all | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $is_other_schema = $schema->get_root_name ne $layer->{name}; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | $refs->{$path} = ( $is_other_schema ? $schema->get_root_name . ':' : '' ) . $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ($is_other_schema) { # add to related schemas | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | $layer->{'related-schema'} = [] unless $layer->{'related-schema'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | my $schema_url = $schema->get_url->as_string; | |||||||||||||||||||||||||||
| 171 | 0 | push @{ $layer->{'related-schema'} }, $schema_url | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | if none { $schema_url eq $_ } @{ $layer->{'related-schema'} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||