#!/usr/bin/perl -w # # convert an exported .txt file to our tesaurus xml schema # use strict; #holds a tesaurus entry my %record; #elements order in entry my @element_order = ( 'notes' => ['M:', 'H:', 'T:', 'Vált:', 'B:', 'Forrás:'], 'connections' => ['L', 'LV', 'LAND', 'H', 'HV', 'HAND', 'F', 'A', 'T', 'P', 'E', 'R', 'X', '='] ); #for my $item (@element_order) { # foreach my $key (keys %$item) { # print "$key\n"; # for my $element (@{$item->{$key}}) { # print "$element "; # } # print "\n\n"; # } #} # #%record = (); # #exit; if ((scalar @ARGV) != 2) { print "Usage:\ntxt2xml \n"; exit; } open (my $TXTFILE, '<', $ARGV[0]) or die "A szovegfajlt (".$ARGV[0].") nem tudtam megnyitni.\n"; open (my $XMLFILE, '>', $ARGV[1]) or die "Az xmlfajlt (".$ARGV[1].") nem tudtam irasra megnyitni.\n"; write_xml_header($XMLFILE); while (next_txt_record($TXTFILE, \%record)) { write_xml_record($XMLFILE, \%record, \@element_order); } write_xml_footer($XMLFILE); #================================================================ # subsequences # =============================================================== sub write_xml_header { my ($xml_ref) = @_; print $xml_ref "\n". "\n\n"; } sub write_xml_footer { my ($xml_ref) = @_; print $xml_ref "\n\n"; } sub write_xml_record { my ($xml_ref, $rec_ref, $order_ref) = @_; print $xml_ref "\n\t\n"; print $xml_ref "\t\t".$rec_ref->{'name'}."\n"; print $xml_ref "\t\t".$rec_ref->{'id'}."\n"; print $xml_ref "\t\t\n"; for my $key ($order_ref->{'notes'}) { if (exists($rec_ref->{$key.':'})) { for my $item ($rec_ref->{$key.':'}) { print "\t\t\t<$key>$item\n"; } } } print $xml_ref "\t\t\n"; print $xml_ref "\t\t\n"; for my $key ($order_ref->{'connections'}) { if (exists($rec_ref->{$key})) { for my $item ($rec_ref->{$key}) { print "\t\t\t<$key>$item\n"; } } } print $xml_ref "\t\t\n"; print $xml_ref "\t\n"; } sub next_txt_record { my ($txt_ref, $record_ref) = @_; my $first = 1; my $line; my $last_mark = ''; %$record_ref = (); while ($line = <$txt_ref>) { #handle empty lines if ($line eq "" ){ if ($first) { return 0; } else { last; } } $first = 0; #do the job $last_mark = process_line($line, $record_ref, $last_mark); } if ($first) { return 0; } else { return 1; } } #i suppose that the file is a well-structured text file sub process_line { my ($line, $record_ref, $preceding) = @_; my $this_mark = $preceding; if ($line =~ m/^([^\s].*)$/) { #leaderword line $record_ref->{'name'} = $1; $record_ref->{'id'} = get_my_id($record_ref->{'name'}); } elsif ($line =~ m/^[\s]([^\s]+)[\s]+(.*)$/) { #connection if ($1 eq 'L&') { $_ = 'LAND'; } elsif ($1 eq 'H&') { $_ = 'HAND'; } else { $_ = $1; } SWITCH: { if (/M:/) {$record_ref->{$_} = [$2]; last SWITCH;} if (/H:/) {$record_ref->{$_} = [$2]; last SWITCH;} if (/T:/) {$record_ref->{$_} = [$2]; last SWITCH;} if (/Vált:/) {$record_ref->{$_} = [$2]; last SWITCH;} if (/B:/) {$record_ref->{$_} = [$2]; last SWITCH;} if (/Forrás:/) {$record_ref->{$_} = [$2]; last SWITCH;} if (/L/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/LV/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/LAND/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/H/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/HV/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/HAND/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/F/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/A/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/T/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/P/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/E/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/R/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/X/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} if (/=/) {$record_ref->{$_} = [get_my_id($2)]; last SWITCH;} #homoníma } $this_mark = $1; } elsif ($line =~ m/^[\s]{2}(.*)$/) { my $lgth = scalar $record_ref->{$this_mark}; $record_ref->{$this_mark}->[$lgth] = $1; #$record_ref->{$this_mark} = $1; } return $this_mark; } #value of the maximal id ever delivered our $maxid = 0; #hash of name-id pairs our %ids; sub get_my_id { my ($name) = @_; if (!(exists $ids{$name})) { $ids{$name} = $maxid++; } return $ids{$name}; }