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.
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.
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
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.
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
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
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.