| 1 | package PMLTQ::Command::init; | ||
| 2 | our $AUTHORITY = 'cpan:MATY'; | ||
| 3 | $PMLTQ::Command::init::VERSION = '2.0.3'; | ||
| 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 | 0 | ▶ | use PMLTQ::PML2BASE; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use PMLTQ::Common; | ||
| 11 | use Treex::PML::Factory; | ||
| 12 | use YAML::Tiny; | ||
| 13 | use List::Util qw/none first/; | ||
| 14 | |||
| 15 | has usage => sub { shift->extract_usage }; | ||
| 16 | |||
| 17 | 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/coref.rf' => 'nonterminal', | ||
| 25 | 'nonterminal/coindex.rf' => 'nonterminal', | ||
| 26 | 'nonterminal/gapping.rf' => 'nonterminal', | ||
| 27 | 'problem/author' => 'user', | ||
| 28 | 'secedge/idref' => 'nonterminal', | ||
| 29 | 'st-node/tnode.rfs' => 't-node', | ||
| 30 | 't-a/aux.rf' => 'a-node', | ||
| 31 | 't-a/lex.rf' => 'a-node', | ||
| 32 | 't-align-links/counterpart.rf' => 't-node', | ||
| 33 | 't-bridging-link/target_node.rf' => 't-node', | ||
| 34 | 't-coref_text-link/target_node.rf' => 't-node', | ||
| 35 | 't-discourse-link/a-connectors.rf' => 'a-node', | ||
| 36 | 't-discourse-link/all_a-connectors.rf' => 'a-node', | ||
| 37 | 't-discourse-link/t-connectors.rf' => 't-node', | ||
| 38 | 't-discourse-link/target_node.rf' => 't-node', | ||
| 39 | 't-node/compl.rf' => 't-node', | ||
| 40 | 't-node/coref_gram.rf' => 't-node', | ||
| 41 | 't-node/coref_text.rf' => 't-node', | ||
| 42 | 't-node/original_parent.rf' => 't-node', | ||
| 43 | 't-node/src_tnode.rf' => 't-node', | ||
| 44 | 't-node/val_frame.rf' => 'v-frame', | ||
| 45 | 't-root/atree.rf' => 'a-root', | ||
| 46 | 't-root/src_tnode.rf' => 't-node', | ||
| 47 | 'terminal/coindex.rf' => 'nonterminal', | ||
| 48 | 'valency_lexicon/owner' => 'user', | ||
| 49 | ); | ||
| 50 | |||
| 51 | sub run { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | my $config = $self->config; | ||
| 55 | |||
| 56 | unless (@_) { | ||
| 57 | say 'Specify at least one schema file'; | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | my ( @layers, @schemas ); | ||
| 62 | |||
| 63 | for my $schema_file (@_) { | ||
| 64 | unless ( -f $schema_file ) { | ||
| 65 | my $in_resources = File::Spec->catfile( $config->{resources}, $schema_file ); | ||
| 66 | unless ( -f $in_resources ) { | ||
| 67 | say "Schema file '$schema_file' not found"; | ||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | $schema_file = $in_resources; | ||
| 71 | } | ||
| 72 | |||
| 73 | push @schemas, my $schema = Treex::PML::Factory->createPMLSchema( { filename => $schema_file } ); | ||
| 74 | PMLTQ::PML2BASE::init(); | ||
| 75 | my $refs = PMLTQ::PML2BASE::complete_schema_pmlref_list($schema); | ||
| 76 | PMLTQ::PML2BASE::destroy(); | ||
| 77 | |||
| 78 | push @layers, { | ||
| 79 | name => $schema->get_root_name, | ||
| 80 | data => '<change this to point to files relative to the data_dir>', | ||
| 81 | COMMENT_path => '<change this to point to files relative to the data directory on server>', | ||
| 82 | references => {%$refs} | ||
| 83 | }; | ||
| 84 | } | ||
| 85 | |||
| 86 | my $resources_dir = | ||
| 87 | @schemas > 1 | ||
| 88 | ? $self->common_prefix( map { dirname( File::Spec->canonpath( $_->get_url ) ) } @schemas ) | ||
| 89 | : dirname( $schemas[0]->get_url ); | ||
| 90 | |||
| 91 | $_->set_url( File::Spec->abs2rel( $_->get_url, $resources_dir ) ) for (@schemas); | ||
| 92 | |||
| 93 | # resolve refs | ||
| 94 | $self->try_resolve_references( \@layers, \@schemas ); | ||
| 95 | |||
| 96 | my $treebank_title = $self->prompt_str( 'Full treebank title', { default => 'Prague Dependency Treebank 3.0' } ); | ||
| 97 | |||
| 98 | my $treebank_id = join '', grep {/[a-z0-9]/} map { substr $_, 0, 1 } split /[^a-z0-9]+/, lc $treebank_title; | ||
| 99 | $treebank_id = $self->prompt_str( | ||
| 100 | 'Treebank ID (can only contain lowercase letters, numbers and underscores)', { | ||
| 101 | default => $treebank_id, | ||
| 102 | check => sub { defined $_[0] and length $_[0] and $_[0] =~ m/^[a-z0-9_]+$/ } | ||
| 103 | } | ||
| 104 | ); | ||
| 105 | |||
| 106 | my $yaml = YAML::Tiny->new( | ||
| 107 | { | ||
| 108 | treebank_id => $treebank_id, | ||
| 109 | COMMENT_data_dir => '', | ||
| 110 | resources => $resources_dir, | ||
| 111 | COMMENT_output_dir => '', | ||
| 112 | db => | ||
| 113 | { | ||
| 114 | name => $treebank_id, | ||
| 115 | COMMENT_host => 'localhost', | ||
| 116 | COMMENT_port => 5432, | ||
| 117 | COMMENT_user => '', | ||
| 118 | COMMENT_password => '' | ||
| 119 | }, | ||
| 120 | COMMENT_sys_db => 'postgres', | ||
| 121 | layers => \@layers, | ||
| 122 | title => $treebank_title, | ||
| 123 | COMMENT_homepage => '', | ||
| 124 | COMMENT_description => '', | ||
| 125 | COMMENT_isFree => 'false', | ||
| 126 | COMMENT_isAllLogged => 'false', | ||
| 127 | COMMENT_isPublic => 'false', | ||
| 128 | COMMENT_isFeatured => 'false', | ||
| 129 | COMMENT_web_api => | ||
| 130 | { | ||
| 131 | COMMENT_dbserver => '', | ||
| 132 | COMMENT_url => '', | ||
| 133 | COMMENT_user => '', | ||
| 134 | COMMENT_password => '' | ||
| 135 | }, | ||
| 136 | COMMENT_manuals => | ||
| 137 | [ | ||
| 138 | { | ||
| 139 | COMMENT_title => '', | ||
| 140 | COMMENT_url => '' | ||
| 141 | } | ||
| 142 | ], | ||
| 143 | COMMENT_tags => | ||
| 144 | [ | ||
| 145 | 'COMMENT_mytag' | ||
| 146 | ], | ||
| 147 | COMMENT_language => 'lang code', | ||
| 148 | COMMENT_test_query => | ||
| 149 | { | ||
| 150 | COMMENT_result_dir => 'webverify_query_results', | ||
| 151 | COMMENT_queries => | ||
| 152 | [ | ||
| 153 | { | ||
| 154 | COMMENT_filename => '01.svg', | ||
| 155 | COMMENT_query => 'a-node [];' | ||
| 156 | } | ||
| 157 | ] | ||
| 158 | } | ||
| 159 | } | ||
| 160 | ); | ||
| 161 | |||
| 162 | my $result = $yaml->write_string; | ||
| 163 | |||
| 164 | $result =~ s/\n( *[-]? *)COMMENT_/\n#$1/g; | ||
| 165 | $result =~ s/\n#(.*?)\n( *-)/\n#$1\n#$2/g; | ||
| 166 | |||
| 167 | say $result; | ||
| 168 | |||
| 169 | my $save = $self->prompt_yn( 'Save?', { default => 0 } ); | ||
| 170 | |||
| 171 | if ($save) { | ||
| 172 | my $filename = $self->prompt_str( | ||
| 173 | 'Save as', { | ||
| 174 | default => 'pmltq.yml' | ||
| 175 | } | ||
| 176 | ); | ||
| 177 | #$yaml->write($filename); | ||
| 178 | open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; | ||
| 179 | print $fh $result; | ||
| 180 | close $fh; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | sub common_prefix { | ||
| 185 | my $self = shift; | ||
| 186 | my @paths = @_; | ||
| 187 | my %prefixes; | ||
| 188 | |||
| 189 | for (@_) { | ||
| 190 | my @parts = File::Spec->splitdir($_); | ||
| 191 | my $prefix = shift @parts; | ||
| 192 | ++$prefixes{$prefix}; | ||
| 193 | ++$prefixes{ $prefix = File::Spec->catdir( $prefix, $_ ) } for (@parts); | ||
| 194 | } | ||
| 195 | return first { $prefixes{$_} == @paths } reverse sort keys %prefixes; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub try_resolve_references { | ||
| 199 | my ( $self, $layers, $schemas ) = @_; | ||
| 200 | |||
| 201 | my %type_map; | ||
| 202 | |||
| 203 | for my $schema (@$schemas) { | ||
| 204 | $schema->for_each_decl( | ||
| 205 | sub { | ||
| 206 | my $decl = shift; | ||
| 207 | my $is_node = ( $decl->get_role || '' ) eq '#NODE'; | ||
| 208 | return unless $is_node; | ||
| 209 | |||
| 210 | my $type = PMLTQ::Common::DeclPathToQueryType( $decl->get_decl_path ); | ||
| 211 | $type_map{$type} = $schema; | ||
| 212 | } | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | |||
| 216 | for my $layer (@$layers) { | ||
| 217 | my $refs = $layer->{references}; | ||
| 218 | for my $path ( grep { $known_relations{$_} } keys %$refs ) { | ||
| 219 | my $type = $known_relations{$path}; | ||
| 220 | my $schema = $type_map{$type}; | ||
| 221 | next unless $schema; # check if type exists at all | ||
| 222 | |||
| 223 | my $is_other_schema = $schema->get_root_name ne $layer->{name}; | ||
| 224 | $refs->{$path} = ( $is_other_schema ? $schema->get_root_name . ':' : '' ) . $type; | ||
| 225 | |||
| 226 | if ($is_other_schema) { # add to related schemas | ||
| 227 | $layer->{'related-schema'} = [] unless $layer->{'related-schema'}; | ||
| 228 | my $schema_url = $schema->get_url->as_string; | ||
| 229 | push @{ $layer->{'related-schema'} }, $schema_url | ||
| 230 | if none { $schema_url eq $_ } @{ $layer->{'related-schema'} }; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | =head1 SYNOPSIS | ||
| 237 | |||
| 238 | pmltq init --resources='/path/to/schemas' schema1 schema2 | ||
| 239 | |||
| 240 | =head1 DESCRIPTION | ||
| 241 | |||
| 242 | Initialize configuration file based on given schemas. | ||
| 243 | |||
| 244 | =head1 OPTIONS | ||
| 245 | |||
| 246 | =head1 PARAMS | ||
| 247 | |||
| 248 | =over 5 | ||
| 249 | |||
| 250 | =item B<resources> | ||
| 251 | |||
| 252 | Optional path to schema files. Standard configuration value. | ||
| 253 | |||
| 254 | =item B<schemas> | ||
| 255 | |||
| 256 | List of schemas to generate the configuration from | ||
| 257 | |||
| 258 | =back | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | 1; | ||