DP_AlphaWords

The DP_AlphaWords library provides a simple method for converting arbitrary text to a list of words which help to disambiguate spelling. For example the word “CAT” may be translated to “Charlie Alfa Tango”.

This translation is useful when communicating information where spelling is crucial (computer passwords, names, license plate numbers, etc) over the phone or radio. It may also be useful when presenting potentially ambigious information such as randomly generated user names or passwords.

  • The function can output a simple list or can output a verbose script suitable for reading. The verbose output support several formatting options.
  • The function supports most typable english characters and will translate them to their names. For example the character “}” will be translated to “Right Curly Brace”.
  • The function supports several alphabet standards.
    • NATO: The NATO phonetic alphabet (used by most international and military agencies). Source.
    • LAPD: The Los Angeles Police Department alphabet (used by most law enforcement and emergency service agencies in the US). Source.
    • Army1916: From the 1916 Signal Book. Source.
    • Army1939: From the 1939 Basic Field Manual FM-24-5 Signal Communications”,”1939.
    • Army1944: Radio News Vol. 31 No. 2, 1944.
    • RAF1924: 1924-1942. Source.
    • RAF1942: 1942-1943. Source.
    • RAF1943: 1943-1956. Source.
    • Gorey: From “The Gashleycrumb Tinies” by Edward Gorey, 1963. Used with respectful adoration, but without permission.
    • Grover: From “Grover and the Twenty-Six Scoops” by Patricia Thackray featured in “The Sesame Street Bedtime Storybook”. Used with fond memory and love, but without permission.

This component requires a JavaScript (ECMAScript) 1.3 (or better) environment and has been tested successfully on Internet Explorer 6+, Firefox 1+ and Opera 9+.

Download

The component is available from GitHub:

All downloads and code are covered by our Source Code Policy.

Usage

The library consists of a single JavaScript file with a .JS extension.

Importing the Library

The library must be imported before it is used. To import the library use the <script> tag. For example:

<script type="text/javascript" src="DP_AlphaWords.js"></script>

Your path may vary depending on the location of the script on your server.

Using the Library

Once the library has been imported you may access any of the functions within it directly. For example:

var MyList = dpAlphaWords("CAT", "LAPD", "Simple");

See the Functions section for more information.

General Recommendations

Remember that JavaScript can easily convert lists to arrays using the split() method. Using it you can easily convert the output from the function into an array.

If you plan on using only one alphabet you can shrink the code somewhat by removing the unneeded alphabets from the library.

Functions

There is only one function currently available in the library, the dpAlphaWords function:

dpAlphaWords()

This functions accepts a string as input and returns the alphaword translation in the format specified.

Function Signature

dpAlphaWords(Input, [AlphaSet,] [OutputStyle,] [OutputPrefix,] [OutputPostfix,] [OutputIncludeCSS])

Arguments

This method has six arguments:

  • Input: String, Required. The string to be converted.
  • AlphaSet: String, Optional (defaults to NATO). The alphabet set to use in the conversion. Acceptable values are “NATO”, “LAPD”, “Army1916”, “Army1939”, “Army1944”, “RAF1924”, “RAF1942”, “RAF1943”, “Gorey” or “Grover”.
  • OutputStyle: String, Optional (defaults to Simple). Determines the style of output. Acceptable values are:
    • Simple: Each character is translated directly to the corrosponding word.
    • Verbose: Each character is presented again and translated to a phrase including character type and case information in addition to the corrosponding word.
    • Array: The string is converted to an array where each element is an object with information about the character. These objects have the following properties:
      • Char: The character itself.
      • Code: The ASCII code of the character.
      • Trans: The “translation” of the character (depends upon the Alphabet set used).
      • Type: The type of character. Value will be one of the following: “Lowercase”, “Uppercase”, “Number”, “Unknown” or “Symbol”.
  • OutputPrefix: String, Optional (defaults to an empty string). This string will be prefixed (inserted before) to the translation of each character. This argument is ignored if the OutputStyle is “Array”.
  • OutputPostfix: String, Optional (defaults to a space). This string will be postfixed (inserted after) to the translation of each character. This argument is ignored if the OutputStyle is “Array”.
  • OutputIncludeCSS: Boolean, Optional (defaults to false). If true the output will be tagged with CSS identifiers (using span tags) which the developer can set. These declarations are “dpAlphaWords_Char” (applied to the character in Verbose-style output) and “dpAlphaWords_Trans” (applied to the translation). Note that for long inputs including CSS can dramtically increase the size of the output.

Return

String. The function returns the translation of the input string according to the arguments passed.

Examples

Simple Conversions

Simple conversions just return the word or phrase associated with each character. The simplest method for converting a string is to accept all defaults. For example:

dpAlphaWords("Alphabet");

Returns:

Alfa Lima Papa Hotel Alfa Bravo Echo Tango

By using the formatting options you can ouput lists and other HTML. Here we’ll do the same conversion using the LAPD alphabet and create HTML list items for display:

	AWOut = dpAlphaWords("Alphabet", "LAPD", "Simple", "&lt;li&gt;", "&lt;/li&gt;");
	document.write("&lt;ul&gt;");
	document.write(AWOut);
	document.write("&lt;/ul&gt;");
	

This returns:

  • Adam
  • Lincoln
  • Paul
  • Henry
  • Adam
  • Boy
  • Edward
  • Tom

By using the formatting options you can ouput lists and easily convert those lists to arrays using the split() method. Here’s the same conversion changed to output an array of values:

MyArray = dpAlphaWords("Alphabet", "LAPD", "Simple", "", ",").split();

Verbose Conversions

Verbose conversions are more appropriate for generating readable scripts where case matters. You’ll mostly likely want to format verbose output in some way. For example here is verbose output formatted with line breaks between each translation:

dpAlphaWords("Alphabet", "LAPD", "Verbose", "", "&lt;br&gt;");

This would return:

Uppercase “A” as in Adam
Lowercase “l” as in Lincoln
Lowercase “p” as in Paul
Lowercase “h” as in Henry
Lowercase “a” as in Adam
Lowercase “b” as in Boy
Lowercase “e” as in Edward
Lowercase “t” as in Tom

Verbose conversions are easier to understand than simple conversions when symbols and numerals are involved. Consider the following conversion of a randomly generated password:

dpAlphaWords("K$rT12(]f", "NATO", "Verbose", "", "&lt;br&gt;");

This would return:

Uppercase “K” as in Kilo
The Dollar Sign symbol (“$”)
Lowercase “r” as in Romeo
Uppercase “T” as in Tango
The number “1”
The number “2”
The Left Parenthesis symbol (“(“)
The Right Square Bracket symbol (“]”)
Lowercase “f” as in Foxtrot

Array Conversions

Using the Array type provides you with complete control over display and usage. The output from this style is an array where each element is an object representing information about a single character of the input string. Using the Array style is more difficult but useful when the simple or verbose styles do not meet your needs.

The following example code presents a table with all of the values returned in the Array style:

	AWInfo = dpAlphaWords("Alphabet", "LAPD", "Array");
	document.write("$lt;table border='1' cellpadding='4'$gt;");
		document.write("$lt;tr$gt;");
		document.write("$lt;td$gt;Char$lt;/td$gt;");
		document.write("$lt;td$gt;Code$lt;/td$gt;");
		document.write("$lt;td$gt;Trans$lt;/td$gt;");
		document.write("$lt;td$gt;Type$lt;/td$gt;");
		document.write("$lt;/tr$gt;");
	for ( Cnt = 0; Cnt < AWInfo.length; Cnt++ ) {
		document.write("$lt;tr$gt;");
		document.write("$lt;td$gt;" + AWInfo[Cnt].Char + "$lt;/td$gt;");
		document.write("$lt;td$gt;" + AWInfo[Cnt].Code + "$lt;/td$gt;");
		document.write("$lt;td$gt;" + AWInfo[Cnt].Trans + "$lt;/td$gt;");
		document.write("$lt;td$gt;" + AWInfo[Cnt].Type + "$lt;/td$gt;");
		document.write("$lt;/tr$gt;");
	};
	document.write("$lt;/table$gt;");
	

Revision History

June 8, 2013

  • Initial GITHUB release.

September 10, 2006

  • Added the “Array” output method.

August 26, 2006

  • Initial Release.
Advertisements
%d bloggers like this: