2016-12-06 17 views
-1

因此,我有一個VB.NET控制檯程序,用戶可以輸入一個句子,然後句子被每個單詞分割並給出值。程序然後檢查任何重複的單詞,並將重複單詞出現的位置與第一個單詞相加。VB.NET:從文本文件中解壓縮/重寫一個句子編輯:w/code

例如(如我明白,可能沒有被很好的解釋): - 用戶輸入「我的名字是你好」 - 計劃拆分句和指定值作爲這樣的:「你好1,我的2,名稱3 ,是4,你好5「(每個單詞和數字對在數組中) - 程序檢測到重複的單詞並將數字添加到第一個,如:」你好1 5,我的2,名稱3,是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

如果你想幫助調試你的代碼,你應該在你的代碼中包含你的代碼。 – Mark

+0

@Mark好的,謝謝我通常這樣做,但我認爲這個解釋已經足夠,因爲我知道問題出在哪裏,只需要一個修復 –

回答

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

希望它可以幫助

+0

謝謝你,雖然我不確定這是我需要的。我已經編輯了這篇文章以包含我的代碼,以便您能夠看到我已經擁有的內容。 –

+0

嗨。唯一不同的地方似乎是我使用過的類(我使用過數組,而你使用過Dictionary)以及輸入輸入文本的方法(爲了簡潔起見,我已將它們包含在一個變量中)。當你說這不是你所需要的,我可以問你的意思嗎?在我看來,結果是正確的。謝謝 – hypnos

+0

由於我的壓縮文字和數字沒有像您那樣寫入文本文件,所以我不確定它會起作用 –