#!/usr/local/bin/perl
# This perl script takes a file from the standard input and replaces
# vowels with vowels according to some random function
# usage: messUp < file
# 961116 erik.tjong@ling.uu.se
# 961125 adapted pairs

# initialize the random function with a random value (Perl book p188)
srand(time|$$);

# read all lines from the input file and perform an action on each line
while (<>) {

   # divide the line in characters and put them in the list @chars
   # $_ is the current item that is being processed; here it is a line
   # the split function is explained in the Perl book on page 185
   @chars=split(//,$_);
   
   # perform an action on each character
   foreach (@chars) {

      # if the character is 'a' or 'y' then...
      # $_ is the current item that is being processed; here it is a word
      if (($_ eq 'a') || ($_ eq 'y')) {

         # if some random number is larger than 0.5 then print 'a'
         if (rand>=0.5) { printf 'a'; }
         # if the random number was smaller than 0.5 then print 'e'
         else { printf 'y'; }

      # if the character is 'e' or 'u' then...
      } elsif (($_ eq 'e') || ($_ eq 'u')) {

         # if some random number is larger than 0.5 then print 'i'
         if (rand>=0.5) { printf 'e'; }
         # if the random number was smaller than 0.5 then print 'o'
         else { printf 'u'; }

      # if the character is 'i' or 'o' then...
      } elsif (($_ eq 'i') || ($_ eq 'o')) {

         # if some random number is larger than 0.5 then print 'u'
         if (rand>=0.5) { printf 'i'; }
         # if the random number was smaller than 0.5 then print 'ä'
         else { printf 'o'; }

      # if the character is 'ä' or 'ö' then...
      } elsif (($_ eq 'ä') || ($_ eq 'ö')) {

         # if some random number is larger than 0.5 then print 'å'
         if (rand>=0.5) { printf 'ä'; }
         # if the random number was smaller than 0.5 then print 'ö'
         else { printf 'ö'; }

      # if the character was not in 'aeiouäåö' then print the character
      } else { printf '%s',$_; }
   }
}