VB.NET:解压缩/改造文本文件中的句子编辑:w / code

时间:2016-12-06 21:37:11

标签: vb.net compression

所以我有一个VB.NET控制台程序,用户可以输入一个句子,然后将句子按每个单词拆分并给出一个值。程序然后检查任何重复的单词,并添加重复单词与第一个单词出现的位置。

例如(据我所知,可能没有很好地解释): - 用户输入"你好,我的名字是你好" - 程序拆分句子并按如下方式分配值:"你好1,我的2,名字3,是4,你好5" (每个单词和数字对都在数组中) - 程序检测到重复的单词并将数字添加到第一个单词中:" hello 1 5,my 2,name 3,is 4" - 单词和数字写入文本文件

现在需要从文本文件中读取单词。我正在使用LineInput(1)/ File Open函数从文本文件中读取并将文本文件的每一行拆分为空格,以便数字和单词是分开的。但是,当将单词作为一个句子打印回控制台时,具有重复项的单词只出现一次,因为它们只在文本文件中出现一次但有两个数字。

我理解这个问题但是找不到修复它的方法,虽然它可能只是我需要以不同的方式/格式写入文本文件。非常感谢任何可以帮助/给我提示的人。

Module Module1
Dim i As Integer = 0
Dim textInput As String
Dim finalTextInput(1) As String

Sub Main()

    Console.WriteLine("Please enter your sentence(s)")
    Dim sentence As String = Console.ReadLine()

    Dim sentenceSplit() As String = sentence.Split(" ")

    For Each element In sentenceSplit
        sentenceSplit(i) = element & " " & i
        i += 1
    Next

    Dim output As New Dictionary(Of String, String)

    For Each current In sentenceSplit
        ' split current input
        Dim currentSplited = current.Split(" ")
        Dim word = currentSplited(0)
        Dim trailingNumbers = currentSplited(1)

        ' if it already exists
        If output.ContainsKey(word) Then
            ' add trailing numbers
            output(word) = output(word) & " " & trailingNumbers
        Else
            ' new input
            output.Add(word, trailingNumbers)
        End If
    Next

    ' create new array from dictionary
    Dim newArray = output.Select(Function(x) x.Key & " " & x.Value).ToArray()

    FileOpen(1, "compressed.txt", OpenMode.Output)

    For Each element In newArray
        PrintLine(1, element)
    Next
    FileClose(1)


    Read()
End Sub
Sub Read()
    Console.WriteLine("Would you like to decompress your sentences? Y/N")
    Dim input As String = Console.ReadLine()

    If input.ToLower() = "y" Then

        FileOpen(1, "compressed.txt", OpenMode.Input)

        Do Until EOF(1)
            textInput = LineInput(1)

            finalTextInput = textInput.Split(" ")

            Console.Write(finalTextInput(0) & " ")
        Loop

        FileClose(1)
    ElseIf input.Tolower() = "n" Then
        Console.WriteLine("Thank you for using our system")
        Threading.Thread.Sleep(2000)
        End
    Else
        Console.WriteLine("Please choose one of the options")
        Threading.Thread.Sleep(1500)
        Console.Clear()
        Read()
    End If
    Console.ReadKey()
End Sub

结束模块

1 个答案:

答案 0 :(得分:0)

我建议您使用以下代码。为了压缩字符串,我们按空格分割输入文本,然后 - 为每个单词循环 - 我们继续检查数组中出现相等字符串的位置。在这种情况下,我们用原始索引替换每个相等的单词。

如果一个单词已被处理,它将用数字表示:所以,如果我们找到一个数字,我们跳到下一个单词。

在完成循环时,我们保存文件。

解压缩方法正好相反。

    '-- Compress and save
    Dim ftext As String = "hello my name is hello and my greeting is hello is end"
    Dim flist() As String = ftext.Split(" ")

    For i As Integer = 0 To flist.Count - 1

        If IsNumeric(flist(i)) Then Continue For

        For k As Integer = i + 1 To flist.Count - 1
            If flist(k) = flist(i) Then flist(k) = i.ToString()
        Next

    Next

    IO.File.WriteAllText("c:\temp\hello.txt", String.Join(" ", flist.ToArray()))

    '-- Read and decompress
    Dim text As String = IO.File.ReadAllText("c:\temp\hello.txt")
    Dim tlist As List(Of String) = text.Split(" ").ToList

    For i As Integer = 0 To tlist.Count - 1
        If IsNumeric(tlist(i)) Then
            tlist(i) = tlist(Val(tlist(i)))
        End If
    Next

    MsgBox(String.Join(" ", tlist.ToArray()))

在压缩程序之后,我的字符串" 你好,我的名字是你好,我的问候语是你好的结束"变

enter image description here

并且在解压缩之后,它看起来像原来的

enter image description here

希望有所帮助