Annotation of loncom/localize/localize/checksimilar_2files.pl, revision 1.4

1.1       wenzelju    1: #!/usr/bin/perl
                      2: # The LearningOnline Network with CAPA
1.4     ! bisitz      3: # $Id: checksimilar_2files.pl,v 1.3 2013/01/07 15:13:26 bisitz Exp $
1.1       wenzelju    4: 
                      5: use strict;
                      6: use warnings;
                      7: use utf8;
                      8: use open ':utf8';
                      9: 
                     10: ####
                     11: #### Checks, if there are similar keys in the two inputfiles.
                     12: #### For example, check the current lang.pm (first input) and newphrases.
                     13: #### So if there are similar keys you don't have to translate
                     14: #### them again but use the old value and just modify it.
                     15: #### IMPORTANT: Both inputfiles have to contain a hash %Lexicon (like lang.pm) !!!
                     16: 
                     17: 
                     18: ####--------Subroutines--------#### 
                     19: 
                     20: sub read {
                     21:     # Read file into memory
                     22:     my $file = shift;
                     23:     open(IN,$file) or die;
                     24:     my %filecontent = ();
                     25:     my $contents = join('',<IN>);
                     26:     close(IN);
                     27:     # Build hash with hash from file
                     28:     my %Lexicon = ();
                     29:     eval($contents.'; %filecontent=%Lexicon;');
                     30:     if ($@ ne "") {
                     31:         print "\nAn error occurred during the attempt to retrieve the translation hash.\n"
                     32:              ."Error: ".$@."\n";
                     33:         die;
                     34:     }
                     35:     return %filecontent;
                     36: }
                     37: 
1.4     ! bisitz     38: sub similar_chars {
1.1       wenzelju   39:     my $text = shift;
1.4     ! bisitz     40:     $text =~ s/\[_\d\]//g; # translation parameters
        !            41:     $text =~ s/[.,\_\-?!: \/]//g; # punctuation
1.1       wenzelju   42:     return $text;
                     43: }
                     44: 
                     45: 
                     46: 
1.4     ! bisitz     47: sub similar_phrases {
1.1       wenzelju   48:     
                     49:     my $text1 = shift;
                     50:     my $text2 = shift;
                     51:     
1.2       bisitz     52:     $text1 =~ s/courses/X001X/gi;
                     53:     $text1 =~ s/communities/X001X/gi;    
                     54:     $text1 =~ s/course/X002X/gi;
                     55:     $text1 =~ s/community/X002X/gi;
1.4     ! bisitz     56:     $text1 =~ s/member/X003X/gi;
        !            57:     $text1 =~ s/student/X003X/gi;
        !            58:     $text1 =~ s/students/X003X/gi;
        !            59: 
1.2       bisitz     60:     $text2 =~ s/courses/X001X/gi;
                     61:     $text2 =~ s/communities/X001X/gi;
                     62:     $text2 =~ s/course/X002X/gi;
                     63:     $text2 =~ s/community/X002X/gi;
1.4     ! bisitz     64:     $text2 =~ s/member/X003X/gi;
        !            65:     $text2 =~ s/student/X003X/gi;
        !            66:     $text2 =~ s/students/X003X/gi;
1.1       wenzelju   67: 
1.4     ! bisitz     68:     if (lc($text1) eq lc($text2)) {
1.1       wenzelju   69:         return 1;
                     70:     }
                     71:     
                     72:     return 0;
                     73: }
                     74: 
                     75: 
                     76: 
                     77: ####--------Main Program--------####
                     78: 
                     79: my $file1 = $ARGV[0];  # Old language.pm
                     80: my $file2 = $ARGV[1];  # New Phrases
1.3       bisitz     81: 
                     82: print("Checking for similar expressions in phrases in $file1 and $file2...\n");
                     83: 
1.1       wenzelju   84: my %langOLD = &read($file1); #Hash with old phrases
                     85: my %langNEW = &read($file2); #Hash with new phrases
                     86: my $dlm; 
1.3       bisitz     87: my $count = 0;
1.1       wenzelju   88: 
                     89: # For each new phrase, check if there is already a similar one
                     90: while( my ($kNEW, $vNEW) = each %langNEW ) {
                     91:     my $temp1 = $kNEW;
1.4     ! bisitz     92:     $temp1 = &similar_chars($temp1);
1.1       wenzelju   93:    
                     94:     while( my ($kOLD, $vOLD) = each %langOLD ) {
                     95:         my $temp2 = $kOLD;
1.4     ! bisitz     96:         $temp2 = &similar_chars($temp2);
1.1       wenzelju   97: 
                     98:         #Check for similar punctuation (case insensitive) or
1.4     ! bisitz     99:         #similarity related to similar phrases 
        !           100:         if (lc($temp1) eq lc($temp2) || &similar_phrases($temp1,$temp2)) {
1.1       wenzelju  101:             #Find delimiter for key and value
                    102:             if (($kNEW=~/\'/) & ($kNEW=~/\"/)) {
                    103:                 print " (Warning: Both, ' and \", occur!)";
                    104:             }
                    105:             if ($kNEW=~/\'/) {
                    106: 	        $dlm = '"';
                    107: 	    } else {
                    108: 	        $dlm = "'";
                    109: 	    }
1.3       bisitz    110:             print (<<ENDNEW);
                    111: #   $kOLD #(Old key)
1.1       wenzelju  112:    $dlm$kNEW$dlm
                    113: => $dlm$vOLD$dlm,
                    114: 
                    115: ENDNEW
                    116:             $count++;
                    117: 
                    118:         }
                    119:     }
                    120: }
                    121: print("Finished. ".$count." similar expressions found!\n");
                    122: 
                    123: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>