File:  [LON-CAPA] / loncom / localize / localize / checksimilar_2files.pl
Revision 1.1: download - view: text, annotated - select for diffs
Tue Mar 9 15:16:26 2010 UTC (14 years, 2 months ago) by wenzelju
Branches: MAIN
CVS tags: PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD
Scripts to check for similar phrases in translationfiles.

#!/usr/bin/perl
# The LearningOnline Network with CAPA
# $Id: checksimilar_2files.pl,v 1.1 2010/03/09 15:16:26 wenzelju Exp $

use strict;
use warnings;
use utf8;
use open ':utf8';

####
#### Checks, if there are similar keys in the two inputfiles.
#### For example, check the current lang.pm (first input) and newphrases.
#### So if there are similar keys you don't have to translate
#### them again but use the old value and just modify it.
#### IMPORTANT: Both inputfiles have to contain a hash %Lexicon (like lang.pm) !!!


####--------Subroutines--------#### 

sub read {
    # Read file into memory
    my $file = shift;
    open(IN,$file) or die;
    my %filecontent = ();
    my $contents = join('',<IN>);
    close(IN);
    # Build hash with hash from file
    my %Lexicon = ();
    eval($contents.'; %filecontent=%Lexicon;');
    if ($@ ne "") {
        print "\nAn error occurred during the attempt to retrieve the translation hash.\n"
             ."Error: ".$@."\n";
        die;
    }
    return %filecontent;
}

sub similarities{
    my $text = shift;
    $text =~ s/[.,\_\-?!:]//g;
    return $text;
}



sub CourseCommunity {
    
    my $text1 = shift;
    my $text2 = shift;
    
    $text1 =~ s/courses//gi;
    $text1 =~ s/communities//gi;    
    $text1 =~ s/course//gi;
    $text1 =~ s/community//gi;
    $text2 =~ s/courses//gi;
    $text2 =~ s/communities//gi;
    $text2 =~ s/course//gi;
    $text2 =~ s/community//gi;

    if(lc($text1) eq lc($text2)) {
        return 1;
    }
    
    return 0;
}



####--------Main Program--------####

my $file1 = $ARGV[0];  # Old language.pm
my $file2 = $ARGV[1];  # New Phrases
my %langOLD = &read($file1); #Hash with old phrases
my %langNEW = &read($file2); #Hash with new phrases
my $dlm; 
my $count = 1; #Counter

open(OUT,'>similarities.txt') or die;

# For each new phrase, check if there is already a similar one
while( my ($kNEW, $vNEW) = each %langNEW ) {
    my $temp1 = $kNEW;
    $temp1 = &similarities($temp1);
   
    while( my ($kOLD, $vOLD) = each %langOLD ) {
        my $temp2 = $kOLD;
        $temp2 = &similarities($temp2);

        #Check for similar punctuation (case insensitive) or
        #similarity related to Course/Community 
        if(lc($temp1) eq lc($temp2) || &CourseCommunity($temp1,$temp2)){
            #Find delimiter for key and value
            if (($kNEW=~/\'/) & ($kNEW=~/\"/)) {
                print " (Warning: Both, ' and \", occur!)";
            }
            if ($kNEW=~/\'/) {
	        $dlm = '"';
	    } else {
	        $dlm = "'";
	    }
            print OUT (<<ENDNEW);
#Old key: $kOLD
   $dlm$kNEW$dlm
=> $dlm$vOLD$dlm,

ENDNEW
            $count++;

        }
    }
}
print("Finished. ".$count." similar expressions found!\n");



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