| 1 | #!usr/bin/perl | ||
| 2 | # | ||
| 3 | # This file is part of Convert-TBX-UTX | ||
| 4 | # | ||
| 5 | # This software is copyright (c) 2014 by Alan Melby. | ||
| 6 | # | ||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||
| 9 | # | ||
| 10 | package Convert::TBX::UTX; | ||
| 11 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 4 | ▶ | use feature 'state'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 4 | ▶ | use feature 'say'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 4 | ▶ | use DateTime; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 0 | ▶ | use TBX::Min 0.05; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use Path::Tiny; | ||
| 18 | use Exporter::Easy ( | ||
| 19 | OK => [ 'utx2min', 'min2utx' ] | ||
| 20 | ); | ||
| 21 | use open ':encoding(utf8)', ':std'; | ||
| 22 | |||
| 23 | our $VERSION = '0.032'; | ||
| 24 | |||
| 25 | # ABSTRACT: Convert UTX to TBX-Min | ||
| 26 | sub utx2min { | ||
| 27 | my ($fh, $TBX); | ||
| 28 | my ($input, $output) = @_; | ||
| 29 | $fh = _get_handle($input); | ||
| 30 | |||
| 31 | $TBX = _import_utx($fh); | ||
| 32 | |||
| 33 | if (defined $output) { _print_converted($TBX, $output) }; | ||
| 34 | return \$TBX; | ||
| 35 | } | ||
| 36 | |||
| 37 | # ABSTRACT: Convert TBX-Min to UTX | ||
| 38 | sub min2utx { | ||
| 39 | my ($fh, $data, $UTX); | ||
| 40 | my ($input, $output) = @_; | ||
| 41 | $UTX = _export_utx($input); | ||
| 42 | if (defined $output) { _print_converted($UTX, $output) }; | ||
| 43 | |||
| 44 | return \$UTX; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _get_handle { | ||
| 48 | my ($input) = @_; | ||
| 49 | |||
| 50 | my $fh; | ||
| 51 | |||
| 52 | if((ref $input) eq 'SCALAR'){ | ||
| 53 | |||
| 54 | open $fh, '<', $input; ## no critic(RequireBriefOpen) | ||
| 55 | |||
| 56 | }else{ | ||
| 57 | |||
| 58 | $fh = path($input)->filehandle('<'); | ||
| 59 | |||
| 60 | } | ||
| 61 | return $fh; | ||
| 62 | } | ||
| 63 | |||
| 64 | # used if UTX.pm is called as a script | ||
| 65 | sub _run { | ||
| 66 | my ($in, $out, $die_message); | ||
| 67 | |||
| 68 | $die_message = "\nExample (TBX-Min to UTX): UTX.pm --tbx2utx Input.tbx Output.utx\n" | ||
| 69 | ."Example (UTX to TBX-Min): UTX.pm --utx2tbx Input.utx Output.tbx\n\n"; | ||
| 70 | |||
| 71 | @ARGV == 3 or die "usage: UTX.pm <--utx2tbx or --tbx2utx (conversion direction)> <input_path> <output_path>\n".$die_message; | ||
| 72 | |||
| 73 | if ($ARGV[0] =~ /--(tbx2utx|utx2tbx)/i){ | ||
| 74 | $in = lc $1 ; | ||
| 75 | ($in =~ /tbx2utx/i) ? ($out = 'utx2tbx') : ($out = 'tbx'); | ||
| 76 | }else{ die "usage: UTX.pm <--utx2tbx or --tbx2utx (conversion direction)> <input_path> <output_path>\n".$die_message; } | ||
| 77 | |||
| 78 | my %import_type = ( | ||
| 79 | tbx2utx => \&min2utx, | ||
| 80 | utx2tbx => \&utx2min | ||
| 81 | ); | ||
| 82 | |||
| 83 | $import_type{$in}->($ARGV[1], $ARGV[2]); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | sub _print_converted { | ||
| 88 | my ($Converted, $output) = @_; | ||
| 89 | open my $fhout, '>', $output | ||
| 90 | or die "An error occured: $!"; | ||
| 91 | |||
| 92 | print $fhout $Converted; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _import_utx { | ||
| 96 | my $fhin = shift; | ||
| 97 | # open my $fhin, '<', \$data; | ||
| 98 | my ($linein, $id, $src, $tgt, $timestamp, $creator, $license, $directionality, $description, $subject, @record, @field_name); | ||
| 99 | |||
| 100 | # header lines | ||
| 101 | # input all relevant information until last line of header is found | ||
| 102 | # keep checking until 'src' or 'tgt' (last line of a UTX header) | ||
| 103 | do { | ||
| 104 | $linein++; | ||
| 105 | $_ = <$fhin>; | ||
| 106 | s/\s*$//; # chomp all trailing whitespace: space, CR, LF, whatever. | ||
| 107 | if ($linein == 1){ | ||
| 108 | die "not a UTX file\n" unless /^#UTX/; | ||
| 109 | ($src, $tgt) = ($1, $2) if m{([a-zA-Z-]*)/([a-zA-Z-]*)}; | ||
| 110 | } | ||
| 111 | if($_ !~ /^#[src|tgt]/i){ | ||
| 112 | $timestamp = $1 if /; ?([0-9T:+-]+)/; | ||
| 113 | $creator = $1 if /creator|copyright: ?([^;]+)/i; | ||
| 114 | $license = $1 if /license: ?([^;]+)/i; | ||
| 115 | $description = $1 if /description: ?([^;]+)/i; | ||
| 116 | $id = $1 if /dictionary id:* ?([^;]+)/i; | ||
| 117 | $directionality = $1 if /(bidirectional)/i; | ||
| 118 | $subject = $1 if /subject\w*: ?([^;]+)/i; | ||
| 119 | } | ||
| 120 | } until ($_ =~ /^#[src|tgt]/i or eof); #eof catch is used for testing | ||
| 121 | |||
| 122 | if ($_ =~ /^#[src|tgt]/i ){ #used for testing | ||
| 123 | s/^#//; | ||
| 124 | @field_name = split /\t/; | ||
| 125 | die "no src column\n" unless $field_name[0] eq 'src'; | ||
| 126 | die "no tgt column\n" unless $field_name[1] eq 'tgt'; | ||
| 127 | die "no pos column\n" unless $field_name[2] =~ /pos/i; | ||
| 128 | # a 'validating' UTX parser would also check for src:pos | ||
| 129 | # but we defer POS issues here | ||
| 130 | |||
| 131 | # body lines | ||
| 132 | while (<$fhin>) { | ||
| 133 | next if /^#/; | ||
| 134 | s/\s*$//; | ||
| 135 | next if /^$/; | ||
| 136 | # turn line to list, then list to hash | ||
| 137 | my @field = split /\t/; | ||
| 138 | my %record; | ||
| 139 | %record = map {$field_name[$_] => $field[$_]} (0..$#field); | ||
| 140 | # clear out blanks, except src and tgt | ||
| 141 | for my $field (grep {$_ ne 'src' and $_ ne 'tgt'} keys %record) { | ||
| 142 | delete $record{$field} unless $record{$field} =~ /\S/ | ||
| 143 | } | ||
| 144 | push @record, \%record; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | #test and adjust directionality if needed before sending it | ||
| 149 | if (defined $directionality) { | ||
| 150 | $directionality = 'monodirectional' if ($directionality ne 'bidirectional'); | ||
| 151 | } | ||
| 152 | else{ | ||
| 153 | $directionality = 'monodirectional' | ||
| 154 | } | ||
| 155 | # $id = '' if defined $id == 0; | ||
| 156 | |||
| 157 | _export_tbx([$fhin, $id, $src, $tgt, $timestamp, $creator, $license, $directionality, $description, $subject, @record]); | ||
| 158 | # return [$data, $id, $src, $tgt, $timestamp, $creator, $license, $directionality, $description, $subject, @record]; | ||
| 159 | |||
| 160 | } # end import_utx | ||
| 161 | |||
| 162 | sub _export_tbx { | ||
| 163 | my $glossary = shift; | ||
| 164 | my ($data, $id, $src, $tgt, $timestamp, $creator, $license, $directionality, $description, $subject, @record) = @$glossary; | ||
| 165 | |||
| 166 | my $generated_timestamp = DateTime->now()->iso8601(); #only use if desired timestamp is time of conversion rather than the timestamp included on the file being converted or if there is no timestamp to convert | ||
| 167 | |||
| 168 | my $ID_Check = TBX::Min->new(); | ||
| 169 | my $TBX = TBX::Min->new(); | ||
| 170 | $TBX->source_lang($src) if (defined $src); | ||
| 171 | $TBX->target_lang($tgt) if (defined $tgt); | ||
| 172 | $TBX->creator($creator) if (defined $creator); | ||
| 173 | (defined $timestamp) ? ($TBX->date_created($timestamp)) : ($TBX->date_created($generated_timestamp)); | ||
| 174 | $TBX->description($description) if (defined $description); | ||
| 175 | $TBX->directionality($directionality) if (defined $directionality); | ||
| 176 | $TBX->license($license) if (defined $license); | ||
| 177 | $TBX->id($id) if (defined $id); | ||
| 178 | |||
| 179 | # This goes through each | ||
| 180 | foreach my $hash_ref (@record) { | ||
| 181 | my ($lang_group_src, $lang_group_tgt, $term_group_src, $term_group_tgt, $entry, $status_bidirectional, @redo); | ||
| 182 | my %hash = %$hash_ref; | ||
| 183 | $entry = TBX::Min::Entry->new(); | ||
| 184 | |||
| 185 | if (keys(%hash) !~ /status$/ && $directionality eq 'bidirectional'){ | ||
| 186 | $status_bidirectional = 1; | ||
| 187 | } | ||
| 188 | |||
| 189 | while(my ($key, $value) = each %hash){ | ||
| 190 | if ($key =~ /src$/){ | ||
| 191 | $lang_group_src = TBX::Min::LangGroup->new({code => $src}); | ||
| 192 | $term_group_src = TBX::Min::TermGroup->new({term => $value}); | ||
| 193 | } | ||
| 194 | elsif ($key =~ /tgt$/){ | ||
| 195 | $lang_group_tgt = TBX::Min::LangGroup->new({code => $tgt}); | ||
| 196 | $term_group_tgt = TBX::Min::TermGroup->new({term => $value}); | ||
| 197 | } | ||
| 198 | |||
| 199 | } | ||
| 200 | while(my ($key, $value) = each %hash){ | ||
| 201 | if ($key =~ /src/ && $key !~ /src$/){ | ||
| 202 | ($term_group_src) = _set_terms($key, $value, $term_group_src, $status_bidirectional); | ||
| 203 | } | ||
| 204 | elsif ($key =~ /tgt/ && $key !~ /tgt$/){ | ||
| 205 | ($term_group_tgt) = _set_terms($key, $value, $term_group_tgt, $status_bidirectional); | ||
| 206 | } | ||
| 207 | elsif ($key =~ /\bid\b/i){ | ||
| 208 | $entry->id($value) if (defined $value); | ||
| 209 | } | ||
| 210 | elsif($key =~ /term status$/i){ | ||
| 211 | ($term_group_tgt, $term_group_src) = _set_terms($key, $value, $term_group_tgt, undef, $term_group_src); | ||
| 212 | } | ||
| 213 | elsif($key !~ /src|tgt/i){ | ||
| 214 | ($term_group_tgt) = _set_terms($key, $value, $term_group_tgt); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | if (defined $term_group_src){ | ||
| 219 | $lang_group_src->add_term_group($term_group_src); | ||
| 220 | $entry->add_lang_group($lang_group_src); | ||
| 221 | } | ||
| 222 | if (defined $term_group_tgt){ | ||
| 223 | $lang_group_tgt->add_term_group($term_group_tgt); | ||
| 224 | $entry->add_lang_group($lang_group_tgt); | ||
| 225 | } | ||
| 226 | $entry->subject_field($subject); | ||
| 227 | |||
| 228 | $ID_Check->add_entry($entry); | ||
| 229 | } | ||
| 230 | |||
| 231 | my (%count_ids_one, %count_ids_two, @entry_ids, $generated_ids); | ||
| 232 | my $entry_list = $ID_Check->entries; | ||
| 233 | foreach my $entry_value (@$entry_list) { | ||
| 234 | my $c_id = $entry_value->id; | ||
| 235 | if (defined $c_id){ | ||
| 236 | $count_ids_one{$c_id}++; | ||
| 237 | for ($c_id) {s/C([0-9]+)/$1/i}; | ||
| 238 | push (@entry_ids, $c_id); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | foreach my $entry_value (@$entry_list) { | ||
| 243 | my $c_id = $entry_value->id; | ||
| 244 | $count_ids_two{$c_id}++ if defined $c_id; | ||
| 245 | |||
| 246 | if (defined $c_id == 0 or $c_id eq '-' or (defined $c_id && $count_ids_one{$c_id} > 1 && $count_ids_two{$c_id} > 1)) { | ||
| 247 | do {$generated_ids++} until ("@entry_ids" !~ sprintf("%03d", $generated_ids)); | ||
| 248 | push @entry_ids, $generated_ids; | ||
| 249 | $entry_value->id("C".sprintf("%03d", $generated_ids)) | ||
| 250 | } | ||
| 251 | $TBX->add_entry($entry_value); | ||
| 252 | } | ||
| 253 | |||
| 254 | my $TBX_ref = $TBX->as_xml; | ||
| 255 | my $TBXstring .= "<?xml version='1.0' encoding=\"UTF-8\"?>\n".$$TBX_ref; #as_xml returns a string ref | ||
| 256 | return $TBXstring; #returns a string | ||
| 257 | } #end export_tbx | ||
| 258 | |||
| 259 | sub _export_utx { | ||
| 260 | my $fh = shift; | ||
| 261 | my $TBX = TBX::Min->new_from_xml($fh); | ||
| 262 | my ($source_lang, $target_lang, $timestamp, $creator, $license, $directionality, $DictID, | ||
| 263 | $description, $entries); #because TBX-Min supports multiple subject fields and UTX does not, subject_field cannot be included here | ||
| 264 | #note that in UTX 1.11, $source_lang, $target_lang,$creator, and $license are required | ||
| 265 | |||
| 266 | #~ my $timestamp = DateTime->now()->iso8601(); #this was used to generate timestamp at time of conversion rather than read in the old one | ||
| 267 | |||
| 268 | #Get values from input | ||
| 269 | $source_lang = $TBX->source_lang if (defined $TBX->source_lang); | ||
| 270 | $target_lang = $TBX->target_lang if (defined $TBX->target_lang); | ||
| 271 | (defined $TBX->date_created) ? ($timestamp = $TBX->date_created) : ($timestamp = DateTime->now()->iso8601()); | ||
| 272 | $creator = "copyright: ".$TBX->creator if (defined $TBX->creator); | ||
| 273 | $license = "license: ".$TBX->license if (defined $TBX->license); | ||
| 274 | $directionality = $TBX->directionality if (defined $TBX->directionality); | ||
| 275 | $DictID = "Dictionary ID: ".$TBX->id if (defined $TBX->id); | ||
| 276 | $description = "description: ".$TBX->description if (defined $TBX->description); | ||
| 277 | $entries = $TBX->entries if (defined $TBX->entries); | ||
| 278 | |||
| 279 | my (@output, @status_list); | ||
| 280 | my ($tgt_pos_exists, $status_exists, $customer_exists, $src_note_exists, $tgt_note_exists, $entry_id_exists) = 0; | ||
| 281 | |||
| 282 | foreach my $entry (@$entries){ | ||
| 283 | my $notehistory; | ||
| 284 | my ($entry_id, $lang_groups, $src_term, $tgt_term, $src_pos, $tgt_pos, $src_note, $tgt_note, $customer); | ||
| 285 | my ($value_count, $approved_count); | ||
| 286 | my (@source_term_info, @target_term_info); | ||
| 287 | if (defined $entry->id){ | ||
| 288 | $entry_id = "\t".$entry->id; | ||
| 289 | $entry_id_exists = 1; | ||
| 290 | } | ||
| 291 | $lang_groups = $entry->lang_groups; | ||
| 292 | |||
| 293 | foreach my $lang_group (@$lang_groups){ | ||
| 294 | |||
| 295 | my $term_groups = $lang_group->term_groups; | ||
| 296 | my $code = $lang_group->code; | ||
| 297 | |||
| 298 | foreach my $term_group (@$term_groups){ | ||
| 299 | |||
| 300 | my ($status, %term_info); | ||
| 301 | |||
| 302 | if ($code eq $source_lang){ | ||
| 303 | $src_term = $term_group->term."\t" if (defined $term_group->term); | ||
| 304 | $term_info{term} = $src_term; | ||
| 305 | |||
| 306 | my $value = $term_group->part_of_speech; | ||
| 307 | (defined $value && $value =~ /noun|properNoun|verb|adjective|adverb/i) ? ($src_pos = $value) : ($src_pos = ""); | ||
| 308 | $src_pos = 'noun' if $src_pos eq 'properNoun'; | ||
| 309 | $term_info{pos} = $src_pos; | ||
| 310 | |||
| 311 | if (defined $term_group->note){ | ||
| 312 | ($src_note = "\t".$term_group->note); | ||
| 313 | $src_note = "\t" if (defined $notehistory && $term_group->note eq $notehistory); | ||
| 314 | $notehistory = $term_group->note if (defined $notehistory == 0); | ||
| 315 | $term_info{note} = $src_note; | ||
| 316 | $src_note_exists = 1; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | elsif ($code eq $target_lang){ | ||
| 320 | $tgt_term = $term_group->term; | ||
| 321 | $term_info{term} = $tgt_term; | ||
| 322 | |||
| 323 | my $value = $term_group->part_of_speech; | ||
| 324 | if (defined $value && $value =~ /noun|properNoun|verb|adjective|adverb|sentece/i){ #technically sentence should never exist in current TBX-Min | ||
| 325 | $value = 'noun' if $value =~ /properNoun/i; | ||
| 326 | $tgt_pos = "\t".$value; | ||
| 327 | $term_info{pos} = $tgt_pos; | ||
| 328 | $tgt_pos_exists = 1; | ||
| 329 | } | ||
| 330 | |||
| 331 | if (defined $term_group->note){ | ||
| 332 | ($tgt_note = "\t".$term_group->note); | ||
| 333 | $tgt_note = "\t" if (defined $notehistory && $term_group->note eq $notehistory); | ||
| 334 | $notehistory = $term_group->note if (defined $notehistory == 0); | ||
| 335 | $term_info{note} = $tgt_note; | ||
| 336 | $tgt_note_exists = 1; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | if (defined $term_group->customer){ | ||
| 341 | ($customer = "\t".$term_group->customer); | ||
| 342 | $term_info{customer} = $customer; | ||
| 343 | $customer_exists = 1; | ||
| 344 | } | ||
| 345 | |||
| 346 | if (defined $term_group->status){ | ||
| 347 | my $value = $term_group->status; | ||
| 348 | if ($value =~ /admitted|preferred|notRecommended|obsolete/i){ | ||
| 349 | $value_count++; | ||
| 350 | $status = $value; | ||
| 351 | $status = "provisional" if $status =~ /admitted/; | ||
| 352 | $status = "non-standard" if $status =~ /notRecommended/; | ||
| 353 | $status = "forbidden" if $status =~ /obsolete/; | ||
| 354 | if ($status =~ /preferred/){ | ||
| 355 | $approved_count++; | ||
| 356 | $status = "approved"; | ||
| 357 | } | ||
| 358 | }else{ | ||
| 359 | $value_count++; | ||
| 360 | $status = undef; | ||
| 361 | }; | ||
| 362 | |||
| 363 | #~ push @status_list, $status; | ||
| 364 | $status = "\t".$status if defined $status; | ||
| 365 | $term_info{status} = $status if defined $status; | ||
| 366 | $status_exists = 1; | ||
| 367 | |||
| 368 | } | ||
| 369 | #in UTX file can only have 'bidirectional' flag if all terms are approved | ||
| 370 | $directionality = undef unless (defined $approved_count && defined $value_count && | ||
| 371 | $approved_count == $value_count); | ||
| 372 | |||
| 373 | push @source_term_info, \%term_info if ($code eq $source_lang); | ||
| 374 | push @target_term_info, \%term_info if ($code eq $target_lang); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | if (@source_term_info) { | ||
| 379 | foreach my $src_hash_ref (@source_term_info) { | ||
| 380 | my %src_term_info = %$src_hash_ref; | ||
| 381 | |||
| 382 | if (@target_term_info) { | ||
| 383 | foreach my $tgt_hash_ref (@target_term_info){ | ||
| 384 | my ($status, $customer); | ||
| 385 | my %tgt_term_info = %$tgt_hash_ref; | ||
| 386 | |||
| 387 | if (defined $src_term_info{status} == 1 && defined $tgt_term_info{status} == 1) { $status = $tgt_term_info{status} } | ||
| 388 | elsif (defined $src_term_info{status} == 0 && defined $tgt_term_info{status} == 1) { $status = $tgt_term_info{status} } | ||
| 389 | elsif (defined $src_term_info{status} == 1 && defined $tgt_term_info{status} == 0) { $status = $src_term_info{status} } | ||
| 390 | |||
| 391 | if (defined $src_term_info{customer} == 1 && defined $tgt_term_info{customer} == 1) { $customer = $tgt_term_info{customer} } | ||
| 392 | elsif (defined $src_term_info{customer} == 0 && defined $tgt_term_info{customer} == 1) { $customer = $tgt_term_info{customer} } | ||
| 393 | elsif (defined $src_term_info{customer} == 1 && defined $tgt_term_info{customer} == 0) { $customer = $src_term_info{customer} } | ||
| 394 | |||
| 395 | my @output_line = ($src_term_info{term}, $tgt_term_info{term}, $src_term_info{pos}, $tgt_term_info{pos}, $status, $customer, $src_term_info{note}, $tgt_term_info{note}, $entry_id); | ||
| 396 | push @output, \@output_line; | ||
| 397 | } | ||
| 398 | }else { | ||
| 399 | my @output_line = ($src_term_info{term}, "", $src_term_info{pos}, "", $src_term_info{status}, $src_term_info{customer}, $src_term_info{note}, "", $entry_id); | ||
| 400 | push @output, \@output_line; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | } else { | ||
| 404 | print "The following target terms could not be converted into valid UTX due to lack of corresponding source terms:\n\n"; | ||
| 405 | foreach my $tgt_hash_ref (@target_term_info){ | ||
| 406 | # my ($status, $customer); | ||
| 407 | my %tgt_term_info = %$tgt_hash_ref; | ||
| 408 | |||
| 409 | print $tgt_term_info{term}."\n"; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | my $UTX = _format_utx([$tgt_pos_exists, $status_exists, $customer_exists, $src_note_exists, $tgt_note_exists, $entry_id_exists, | ||
| 415 | $source_lang, $target_lang, $timestamp, $creator, $license, $directionality, $DictID, $description, @output]); | ||
| 416 | return $UTX; | ||
| 417 | } # end _export_utx | ||
| 418 | |||
| 419 | sub _set_terms { #used when exporting to TBX | ||
| 420 | my ($key, $value, $term_group, $status_bidirectional, $source_term_group) = @_; | ||
| 421 | if ($key =~ /pos$/){ | ||
| 422 | |||
| 423 | $value = "other" if $value !~ /verb|adjective|adverb|noun/i; | ||
| 424 | |||
| 425 | $term_group->part_of_speech($value); | ||
| 426 | } | ||
| 427 | elsif ($key =~ /status$/){ | ||
| 428 | $value = "admitted" if $value =~ /provisional/i; | ||
| 429 | $value = "preferred" if $value =~ /approved/i; | ||
| 430 | $value = "notRecommended" if $value =~ /non-standard/i; | ||
| 431 | $value = "obsolete" if $value =~ /forbidden/i; | ||
| 432 | $term_group->status($value) if ($value =~ /admitted|preferred|notRecommended|obsolete/i); | ||
| 433 | $source_term_group->status($value) if ($value =~ /preferred/i); | ||
| 434 | } | ||
| 435 | elsif ($key =~ /customer/i){ | ||
| 436 | $term_group->customer($value) unless $value eq ''; | ||
| 437 | } | ||
| 438 | elsif ($key =~ /comment/i) { | ||
| 439 | $term_group->note($value) unless $value eq ''; | ||
| 440 | } | ||
| 441 | $term_group->status('preferred') if defined $status_bidirectional; #UTX allows empty term status if bidirectionality flag is true | ||
| 442 | return ($term_group, $source_term_group); #return to &_export_tbx; | ||
| 443 | } # end _set_terms | ||
| 444 | |||
| 445 | sub _format_utx { #accepts $exists, and @output | ||
| 446 | my $args = shift; | ||
| 447 | my ($tgt_pos_exists, $status_exists, $customer_exists, $src_note_exists, $tgt_note_exists, $entry_id_exists, | ||
| 448 | $source_lang, $target_lang, $timestamp, $creator, $license, $directionality, $DictID, $description, @output) = @$args; | ||
| 449 | my $UTX; | ||
| 450 | |||
| 451 | #print header | ||
| 452 | $UTX .= "#UTX 1.11;"; | ||
| 453 | $UTX .= " $source_lang" if defined $source_lang; | ||
| 454 | $UTX .= "/$target_lang;" if defined $target_lang; | ||
| 455 | $UTX .= " $timestamp;" if defined $timestamp; | ||
| 456 | $UTX .= " $creator;" if defined $creator; | ||
| 457 | $UTX .= " $license;" if defined $license; | ||
| 458 | $UTX .= " bidirectional;" if (defined $directionality && $directionality =~ /bidirectional/); | ||
| 459 | $UTX .= " $DictID;" if defined $DictID; | ||
| 460 | $UTX .= "\n#$description;" if (defined $description); #print middle of header if necessary | ||
| 461 | $UTX .= "\n#src tgt src:pos"; #print necessary values of final line of Header | ||
| 462 | |||
| 463 | $UTX .= "\ttgt:pos" if ($tgt_pos_exists); | ||
| 464 | $UTX .= "\tterm status" if ($status_exists && (defined $directionality == 0 or $directionality ne 'bidirectional')); | ||
| 465 | $UTX .= "\tsrc:comment" if ($src_note_exists); | ||
| 466 | $UTX .= "\ttgt:comment" if ($tgt_note_exists); | ||
| 467 | $UTX .= "\tcustomer" if ($customer_exists); | ||
| 468 | $UTX .= "\tconcept ID" if ($entry_id_exists); | ||
| 469 | |||
| 470 | $status_exists = 0 if (defined $directionality && $directionality =~ /bidirectional/); | ||
| 471 | |||
| 472 | foreach my $output_line_ref (@output) { | ||
| 473 | |||
| 474 | my ($src_term, $tgt_term, $src_pos, $tgt_pos, $status, $customer, $src_note, $tgt_note, $entry_id) = @$output_line_ref; | ||
| 475 | |||
| 476 | $tgt_term = $tgt_term."\t"; | ||
| 477 | if (defined $src_term && defined $tgt_term){ | ||
| 478 | $UTX .= "\n$src_term$tgt_term$src_pos"; | ||
| 479 | |||
| 480 | if ($tgt_pos_exists){ (defined $tgt_pos) ? ($UTX .= "$tgt_pos") : ($UTX .= "\t") } | ||
| 481 | if ($status_exists){ (defined $status) ? ($UTX .= "$status") : ($UTX .= "\t") } | ||
| 482 | if ($src_note_exists){ (defined $src_note) ? ($UTX .= "$src_note") : ($UTX .= "\t") } | ||
| 483 | if ($tgt_note_exists){ (defined $tgt_note) ? ($UTX .= "$tgt_note") : ($UTX .= "\t") } | ||
| 484 | if ($customer_exists){ (defined $customer) ? ($UTX .= "$customer") : ($UTX .= "\t") } | ||
| 485 | if ($entry_id_exists){ (defined $entry_id) ? ($UTX .= "$entry_id") : ($UTX .= "\t") } | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | return $UTX; | ||
| 490 | } #end _print_utx | ||
| 491 | |||
| 492 | _run() unless caller; | ||
| 493 | |||
| 494 | __END__ | ||