0

Basically I'm creating a tool which while is looking through lines of "file.txt" to replace a word from a textbox's content with that line if the line is containing that word.

Basically if the line is: pizza-cheese-potatoes, all the words containing "pizza" or "cheese" or "potatoes" to be replaced with "pizza-cheese-potatoes"

Here is what I have until now. But it freeze and I don't really know why. Please help me. :)

    Dim PATH As String = "C:\test.txt"

Sub Repl(x As String)
    For Each line As String In File.ReadLines(PATH)
        Dim myList = New List(Of String)(line.Split("|"c))
        For Each item As String In myList
            If x Is item Then
                TextBox1.Text.Replace(x, line)
            End If
        Next
    Next
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each word As String In TextBox1.Text.Split(" "c)
        Repl(word)
    Next
End Sub

Thank you in advance!

Nana
  • 13
  • 5

1 Answers1

0

Try doing it like this instead.

Public Sub DoWork()
    Dim lines = IO.File.ReadAllLines(PATH)
    For Each line In lines
        Dim myList = New List(Of String)(line.Split("-"c))
        For Each item In myList
            If TextBox1.Text.Contains(item) Then
                TextBox1.Text = TextBox1.Text.Replace(item, line)
            End If
        Next
    Next
End Sub

Your reader reads the file multiple times which is massively inefficient. This code reads it once, then we just loop over each line with no need to worry about exit conditions etc.

However it's not really doing anything useful as you're not pausing after each line so you'll only be able to review the final one.

There is also no need to test if the textbox contains the text, just simply do a replace then you only search the text once.

        For Each item In myList
            TextBox1.Text = TextBox1.Text.Replace(item, line)
        Next

-------- edit --------

To fix the issue with replacing the word you've already replaced you could try using a replacement placeholder.

        For Each item In myList
            TextBox1.Text = TextBox1.Text.Replace(item, "#")
        Next
        TextBox1.Text = TextBox1.Text.Replace("#", line)

---------- edit 2 ------------------

You want to try and build up a new string, word by word, instead of replacing the text in the textbox. This is so that words you've substituted already don't get converted.

Function ReplaceWord(word As String, lines As String())
    For Each line As String In File.ReadLines(PATH)
        Dim myList = New List(Of String)(line.Split("|"c))
        For Each item As String In myList
            If word = item Then
                Return line
            End If
        Next
    Next
    Return word
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim result As New System.Text.StringBuilder
    Dim lines = File.ReadLines(PATH)
    For Each word As String In TextBox1.Text
        result.Append(ReplaceWord(word, lines)).Append(" ")
    Next
    Textbox1.Text = result.ToString()
End Sub
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • Thank you. It works, but not as I expected. There is a kinda conflicts between both For Each and somehow it does replace same world multiple times, since the replaced words are also in the line of the file. Example from the settings input and motion: http://prntscr.com/cg3m7h – Nana Sep 09 '16 at 17:37
  • I've posted an edit with another approach to the replacements – FloatingKiwi Sep 09 '16 at 17:44
  • Now that is genius. Wish I could think out of the box like this. Thank you very much FloatingKiwi. You are amazing! – Nana Sep 09 '16 at 17:52
  • Nevermind. It works with just a few words, but If I'm adding a sentence here is the result: http://prntscr.com/cg40p8/direct – Nana Sep 09 '16 at 18:09
  • Can you post what your input file + textbox contents was as an edit. And the expected output. – FloatingKiwi Sep 09 '16 at 18:20
  • File Input: http://pastebin.com/raw/rYgRhhXy Textbox Content: "The boy choose 4. He is great." Expected Output: "The boy choose 4|four. He is fine|great|excellent|first-rate|first-class|exceptional|nice|pleasant|quality|best|high-quality|satisfactory." – Nana Sep 09 '16 at 18:31
  • I can upload and send the whole project too, if you want to. – Nana Sep 09 '16 at 18:33
  • take a look at `great`. It's mentioned multiple times in your dictionary. So you'll get `He is fine|great|excellent|first-rate|first-class|exceptional|nice|pleasant|quality|best|high-quality|satisfactory` then move onto the next line and each of those lines that has an entry will also get expanded (including great). – FloatingKiwi Sep 09 '16 at 18:37
  • I understand. But there is nothing else I can do to bypass this? As all the words has multiple definitions and it's really important to keep them as they are. :( – Nana Sep 09 '16 at 18:45
  • Instead of working line by line, work through each word in the textbox. Lookup it's synonyms and add the line to a result string (or the original word if the result doesn't exist). Then at the end set TextBox1.Text = result – FloatingKiwi Sep 09 '16 at 18:46
  • Thank you. I will try this and I will come with a update of the result. – Nana Sep 09 '16 at 18:50
  • I tried what you said. The code seems fine for me, but unfortunately it doesn't work. What did I do wrong? I've updated the question. – Nana Sep 09 '16 at 19:03