How to resolve the algorithm Soundex step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Soundex step by step in the PL/I programming language

Table of Contents

Problem Statement

Soundex is an algorithm for creating indices for words based on their pronunciation.

The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). There is a major issue in many of the implementations concerning the separation of two consonants that have the same soundex code! According to the official Rules [[1]]. So check for instance if Ashcraft is coded to A-261.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Soundex step by step in the PL/I programming language

Source code in the pl/i programming language

Soundex: procedure (pword) returns (character(4));
   declare pword character (*) varying, value character (length(pword)) varying;
   declare word character (length(pword));
   declare (prevCode, currCode) character (1);
   declare alphabet CHARACTER (26) STATIC INITIAL ('AEIOUHWYBFPVCGJKQSXZDTLMNR');
   declare replace  character (26) static initial ('00000000111122222222334556');
   declare i fixed binary;

   word = pword;

   /* Buffer to build up with character codes */
   value = ''; 

   /* Make sure the word is at least two characters in length. */
   if length(word) <= 1 then return (word);

   word = uppercase(word); /* Convert to uppercase. */

   /* The current and previous character codes */
   prevCode = '0'; 

   value = substr(word, 1, 1); /* The first character is unchanged. */

   word = Translate (word, replace, alphabet);

   /* Loop through the remaining characters ... */
   do i = 2 to length(word);
      currCode = substr(word, i, 1);
      /* Check to see if the current code is the same as the last one */
      if currCode ^= prevCode & currCode ^= '0' then
         /* If the current code is a vowel, ignore it. */
         value = value || currCode;
      /* Set the new previous character code */
      prevCode = currCode; 
   end; /* of do i = ... */

   return ( left(value, 4, '0') ); /* Pad, if necessary. */
         
end Soundex;

  

You may also check:How to resolve the algorithm Factors of an integer step by step in the Nim programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the Julia programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Lingo programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Sidef programming language