Rudimentary Translator. Write a program that requests an English sentence as inp
ID: 3534996 • Letter: R
Question
Rudimentary Translator. Write a program that requests an English sentence as input and translates it into French and German.
Here's what I have and it is giving me the same answer( " " not found retype a valid sentence)
Option Strict On
Option Explicit On
Imports System.IO
Public Class frmTranslator
Structure triWord
Public english As String 'English word.
Public french As String 'French word.
Public german As String 'German word.
End Structure
Public Sub swap(ByRef words() As triWord, ByVal pos1 As Integer, ByVal pos2 As Integer)
Dim temp As triWord = words(pos1)
words(pos1) = words(pos2)
words(pos2) = temp
End Sub
Public Sub sortDict(ByRef words() As triWord, ByVal numEntries As Integer)
Dim smallest As Integer
For i As Integer = 0 To numEntries - 1
smallest = i
For j As Integer = i + 1 To numEntries - 1
If words(j).english < words(smallest).english Then
smallest = j
End If
Next
swap(words, smallest, i)
Next
End Sub
Public Function binarySearchDict(ByRef words() As triWord, ByVal thisWord As String) As Integer
Dim left, middle, right As Integer
left = 0
right = words.Length - 1
While (right >= left)
middle = CInt(CInt(left + right) / 2)
If ((thisWord = words(middle).english) Or (Microsoft.VisualBasic.Left(thisWord, thisWord.Length - 1) = words(middle).english)) Then
Return middle
End If
If (thisWord < words(middle).english) Then
right = middle - 1
Else
left = middle + 1
End If
End While
Return -1
End Function
Private Sub BtnTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTranslate.Click
If TxtEnglish.Text = "" Then
MessageBox.Show(" No text to translate: Please type a valid sentence")
Exit Sub
End If
Dim Words() As String = TxtEnglish.Text.Split(CChar(" "))
Dim wordCount As Integer = 16
Dim dictArray(0 To wordCount - 1) As triWord
Dim sr As IO.StreamReader = IO.File.OpenText("dictionary.txt")
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("dictionary.txt")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim lineCount As Integer = 48
Dim currCount As Integer = 0
While Not MyReader.EndOfData
Try
Dim currentRow() As String = MyReader.ReadFields()
dictArray(currCount).english = currentRow(0)
dictArray(currCount).french = currentRow(1)
dictArray(currCount).german = currentRow(2)
currCount = currCount + 1
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox(ex.Message & "Skipping invalid line")
End Try
End While
MyReader.Close()
End Using
sortDict(dictArray, wordCount)
Dim englishText, frenchText, germanText As String
frenchText = ""
germanText = ""
englishText = TxtEnglish.Text
Dim englishWords() As String = Split(englishText)
For k As Integer = 0 To englishWords.Length - 1
Dim wordPosition As Integer
wordPosition = binarySearchDict(dictArray, englishWords(k))
If (wordPosition >= 0) Then
frenchText = frenchText + " " + dictArray(wordPosition).french
germanText = germanText + " " + dictArray(wordPosition).german
Else
MessageBox.Show(englishWords(k) & " not found - Please retype a valid sentence")
TxtFrench.Text = ""
TxtGerman.Text = ""
Exit Sub
End If
Next
TxtFrench.Text = frenchText + "."
TxtGerman.Text = germanText + "."
End Sub
End Class
Explanation / Answer
I have stored it in an online notepad file. There are two codes. With slight change. I have written the configuration too.
Hope that helps you.
Don't forget to rate.
Cheers!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.