英语句子作为输入并将其翻译成Textese

时间:2016-12-05 20:57:45

标签: vb.net

在我输入文本文件并在“英语”文本框中编写句子的项目中工作,只需单击一个按钮,程序就会将其翻译成“textese”类型的语言。它只会更改文本文件中列出的单词,而忽略其他所有单词。下面是我的代码,并想知道出了什么问题,它无法运行并突出显示我的If Then语句,所以我认为我的问题在那里,但不确定是什么。文本文件中的内容以及如何使用逗号对它们进行排序/分隔的示例。

任何人,NE1

是,R

吃,8

带,B和

是,B

之前,B4

忙,BZ

计算机,帕特

Public Class frmTextese

Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click

    Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")

    Dim english As Integer = 0
    Dim englishSentence As String = txtEnglish.Text

    Dim result() As String = englishSentence.Split(" "c)

    Do While english < (result.Length - 1)
        Dim line As String
        Dim data() As String
        Dim englishArray As String
        Dim texteseArray As String
        For i As Integer = 0 To (inputData.Length - 1)
            line = inputData(i)
            data = line.Split(","c)
            englishArray = line.Split(","c)(0)
            texteseArray = line.Split(","c)(1)

            If result(i).StartsWith(englishArray(i)) Then
                If englishArray(i).Equals(texteseArray(i)) Then
                    result(i) = texteseArray(i)
                End If
            End If
            txtTextese.Text = result(i)
        Next
    Loop

End Sub

End Class

1 个答案:

答案 0 :(得分:2)

您只需将result(i)englishArray进行比较即可。此外,您的while循环是无限循环。搜索textese数组时,一旦找到匹配项,就可以退出搜索并继续查看下一个英文单词。最后,您应该注意使用描述其目的的变量名称。

查看此代码(未经测试)。我使字符串比较不区分大小写,但这不是必需的。

Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click

    'Get all the textese definitions
    Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")

    Dim english As Integer = 0
    Dim englishSentence As String = txtEnglish.Text

    Dim result() As String = englishSentence.Split(" "c)

    Do While english < (result.Length - 1)
        Dim line As String
        Dim data() As String
        Dim englishArray As String
        Dim texteseArray As String
        For i As Integer = 0 To (inputData.Length - 1)

            'Split the textese entry into two parts
            line = inputData(i)
            data = line.Split(","c)
            englishArray = data(0)
            texteseArray = data(1)

            'Compare the word in the english sentence against the word in the textese array
            'using a case insensitive comparison (not required)
            If result(i).Equals(englishArray, StringComparison.CurrentCultureIgnoreCase) Then
                'Replace the word in the sentence with its textese version
                result(i) = texteseArray

                'If we found the word, there is no need to continue searching, so 
                'skip to the next word in the english sentence
                Exit For
            End If
        Next

        'Increment the loop counter to avoid an endless loop
        english = english + 1
    Loop

    'Take the elements of the result array and join them together, separated by spaces
    txtTextese.Text = String.Join(" ", result)

End Sub