A set of scripts to translate iChat acronyms

Aug 13, '04 09:26:00AM

Contributed by: galaher

I'm not a big acronym user, finding it generally faster to type what I'm thinking than abbreviate it. However, I often find myself on the receiving end of acronyms that I have no idea what they mean. For fun, I went onto the web and scraped a few web pages listing hundreds of common chat acronyms and made a tool that I can use to very easily translate these on the fly while using iChat.

I made two scripts that work together, allowing me to highlight text in iChat and get a quick translation of any acronyms in the highlighted text. Due to the fact that the perl script was a few thousand lines with all the definitions, I've cut out all but four acronyms so that folks can add their own or contact me for the full set of definitions if they want. As the definitions were simply scraped off the web, I make no claims on the quality of the content of the translations which also contain some swear words. You might wish to create your own list of definitions and add them to the script.

I'm a self taught scripter, so I'm sure there might be shorter and or better ways to do this. It's intended to be able to translate whole sentences containing acronyms, and the way it handles deconstructing white space and punctuation and re-assembling them might be handled better, but it seems to work fine for me as is.

Some notes:

  1. The AppleScript uses UI scripting, so make sure 'Enable access for assistive devices' is checked in System Preferences -> Universal Access.

  2. Save the AppleScript and name it something like acronym_trans.scpt. Using a script launcher program such as DragThing, of course, makes this even more convenient to use.

  3. Save the perl script as im_translator_ichat.pl. Make sure that the path to this script as contained in the AppleScript matches wherever you choose to put it. Mine is ~/bin/im_translator_ichat.pl.

  4. Give the perl script execute permissions: chmod 755 im_translator_ichat.pl

  5. Expand the hashes in the define_translation_hashes subroutine to meet your needs. For each acronym you add to the %im_acronym hash, the translation must be added to the %im_translation hash and have the same key name. See the comments in the define_translation_hashes subroutine.
Now next time you get an acronym in a chat, just highlight it and run the AppleScript, and you should get an applscript dialog with he translation.

Applescript:
tell application "iChat"
  activate
end tell
tell application "System Events"
  tell process "iChat"
    keystroke "c" using {command down}
    delay 2
    set myData to (the clipboard) as text
  end tell
end tell
do shell script "perl ~/bin/im_translator_ichat.pl " & "\"" & myData & "\""
Perl:
#!/usr/bin/perl -w
use strict;

my $out = "";
my ($im_acronym, $im_translation) = &define_translation_hashes();
my %im_acronym = %{$im_acronym};
my %im_translation = %{$im_translation};

my $string = $ARGV[0];
my @string = ();


################################################

@string = &split_on_white($string);

&translate(\@string);

for(@string){
  if($_ =~ m/^[.?!,]$/m){
    $out .= "$_";
  }
  else{
        $out .= " $_";  

  }

}

my $SCRIPT =<<EOS;
tell application "iChat"
    activate
  display dialog "$out"
end tell
EOS

open (FH, "|/usr/bin/osascript");
print FH $SCRIPT;
close FH;

exit;

################################################

sub split_on_white(){
  my $string = shift @_;
  
  #all white space to single spaces
  $string =~ s![ \t]+! !mig;
  $string =~ s/([.,!?])/ $1/mig;  
  my @string = split (/ /, $string);
  
}

sub translate(){
  my $string = shift @_;
  for(@{$string}){
      my $cur_string = $_;
    
    #Check each for match to %im_acronym
    foreach my $key(sort keys %im_acronym){
      if($im_acronym{$key} =~ m#^\Q${cur_string}\E$#im){
        $_ = $im_translation{$key};
  
      }
    }      }

}

################################################

sub define_translation_hashes(){

  my %im_acronym = (
  
  im_acronym1 => "AFAIK",
  im_acronym2 => "IMHO",
  im_acronym3 => "BTW",
  im_acronym4 => "HTH",
  #ADD FURTHER ACRONYMS HERE
  
  );
  
  my %im_translation = (
  
  im_acronym1 => "as far as I know",
  im_acronym2 => "in my humble opinion",
  im_acronym3 => "by the way",
  im_acronym4 => "hope that helps",
  #ADD CORRESPONDING TRANSLATION HERE
  
  );
  
  
  my $im_translation = \%im_translation;
  my $im_acronym = \%im_acronym;
  
  return ($im_acronym, $im_translation);
}

################################################

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040812190839795