Results 1 to 9 of 9

Thread: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

  1. #1

    Thread Starter
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    Haven't been in here in years, haven't done any programming in just as many. I am glad to see a lot of you old timers are still around. Most of you were quiet helpful when I was still active. I would like to create a simple app for my own use that would a sentence or even paragraph over to the military phonetic alphabet.

    Example:

    CoachBarker

    CHARLIEOMEGAALPHACHARLIEHOTELBRAVOALPHAROMEOKILOECHOROMEO

    Difficulty level? Remember it has been awhile.
    Attached Images Attached Images  
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    I'd be inclined to create a Dictionary(of string, string) to hold the conversion table that you showed. The letter would be the key, the word would be the value. There are other solutions, but a lookup in a dictionary is particularly fast.

    A string can be seen as an array of characters, so a Dictionary (of char, string) might be a bit easier. You'd then just be iterating through the array of characters and looking things up in the dictionary. A few issues you'd want to consider ahead of time is what to do with punctuation and spaces. The options that seem plausible to me, depending on your ultimate goal, would be to leave them out, leave them in, or convert them to words as well. Leaving them out would mean that all the words would run together if there were multiple words in the string. Leaving them in would mean that when you encountered a space or a period, you'd look it up in the dictionary, see that it is not there, and just add it into the string you are building without changing it. Thus words would be separated by spaces and punctuation just as they were in the original string. The third option would be to add entries in the dictionary like "space" for space and "period" for period (or STOP, if you want to be more telegrammatically correct).

    The other item to look at is the StringBuilder. You could build up the result using concatenation. It wouldn't be as fast as the StringBuilder, but then again, you'd never see the difference unless you started converting some truly large strings.
    My usual boring signature: Nothing

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    The alphabet can be described pretty easily in a Dictionary(Of Char, String) where the Key would represent the letter and the Value would represent the phonetic equivalent.

    Once you have the dictionary setup, you would need to iterate over each character in a String, check if the key exists in the dictionary, and either throw an exception if it isn't or append the phonetic equivalent to the returned String.

    Here is an example:
    Code:
    Private Function ToPhoneticAlphabet(input As String, Optional ByVal ignoreWhitespace As Boolean = False) As String
            input = input.ToLowerInvariant()
            Dim collection = New StringBuilder()
            For Each character In input
                If (Char.IsWhiteSpace(character) AndAlso ignoreWhitespace) Then
                    Continue For
                End If
                If (Not _phoneticAlphabet.ContainsKey(character)) Then
                    Throw New ArgumentException("input contains an invalid character: " & character)
                End If
                collection.Append(_phoneticAlphabet(character))
            Next
        Return collection.ToString()
    End Function
    Example: Live Demo

    Edit - It looks like Shaggy and I were on the same page, though I used a Char as the Key and didn't consider punctuation (or "STOP"s).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    Yeah, very much on the same page, but you showed an example, where I just talked about it.
    My usual boring signature: Nothing

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,044

    Re: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    here a version with the stringbuilder

    Code:
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim s1 As String = "Car.?bra" 'do you have invalid characters ?
            Dim sb As New StringBuilder()
            Dim s2 As String = Convert.ToString(s1)
            For Each c In s2.ToUpperInvariant().ToCharArray() 'make it uppercase to eliminate case sensitivity
                Select Case c
                    Case ("A"c)
                        sb.Append("Alfa")
                    Case ("B"c)
                        sb.Append("Bravo")
                    Case ("C"c)
                        sb.Append("Charlie")
                        'etc...
                End Select
    
            Next c
            Debug.WriteLine(sb.ToString())
        End Sub
    didn't try a Linq version
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    For proper usage it is : ALPHA BETA CHARLIE, not ALPHABETACHARLIE. Spaces between full words are dropped, periods are noted by Break. End of message is Break Break. Punctuation doesn't exist. Don't forget numbers. 1028 is ONE ZERO TWO EIGHT

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    XML / LINQ based solution
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Clear()
            Dim phoneXML As XElement
            phoneXML = <phonetic_alphabet>
                           <item id="a">Alpha</item>
                           <item id="b">Bravo</item>
                           <item id="c">Charlie</item>
                           <item id="d">Delta</item>
                           <item id="e">Echo</item>
                           <item id="f">Foxtrot</item>
                           <item id="g">Golf</item>
                           <item id="h">Hotel</item>
                           <item id="i">India</item>
                           <item id="j">Juliett</item>
                           <item id="k">Kilo</item>
                           <item id="l">Lima</item>
                           <item id="m">Mike</item>
                           <item id="n">November</item>
                           <item id="o">Oscar</item>
                           <item id="p">Papa</item>
                           <item id="q">Quebec</item>
                           <item id="r">Romeo</item>
                           <item id="s">Sierra</item>
                           <item id="t">Tango</item>
                           <item id="u">Uniform</item>
                           <item id="v">Victor</item>
                           <item id="w">Whiskey</item>
                           <item id="x">X-ray</item>
                           <item id="y">Yankee</item>
                           <item id="z">Zulu</item>
                       </phonetic_alphabet>
    
            For Each c As Char In TextBox1.Text.ToLower
                'find letter if there is one
                Dim ie As IEnumerable(Of XElement) = From el In phoneXML.Elements
                                                     Where el.@id = c
                                                     Select el Take 1
    
                If ie.Count = 1 Then
                    'show phoenetic
                    RichTextBox1.AppendText(ie(0).Value)
                    RichTextBox1.AppendText(ControlChars.Cr)
                Else
                    'not present
                    RichTextBox1.AppendText("* " & c)
                    RichTextBox1.AppendText(ControlChars.Cr)
                End If
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Converting string to Phonetic Alphabet (Alpha, Beta, Charlie)

    Just use a Dictionary(Of String, String)

    Code:
    Dim alphabet As New Dictionary(Of String, String)
    ‘ load dictionary keys and values
    MsgBox(String.Concat(Array.ConvertAll(inputString, Function(c) If(alphabet.ContainsKey(c), alphabet(c), c)))
    Last edited by .paul.; Jan 19th, 2021 at 11:36 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width