Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My file of words is too big to fit in my program. I have two columns for the Bad words, and Good words.

I just want to replace the bad words with the Good words. Please help!

Here is my dictionary file:


https://docs.google.com/document/d/1eTZaIUh7Q9zkNzjZQ-lPV9yxS5eSlNs5ht2-Pl-LS14/edit?usp=sharing[^]


Here is my code:

VB
Public Class Form1
    Friend WithEvents RichTextBox1 As New RichTextBox With {.Dock = DockStyle.Fill}
    Friend WithEvents ReplaceMenu As New ContextMenuStrip

    Private replacements As New Dictionary(Of String, IEnumerable(Of TextReplacement))
    Private nextCheckIndex As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Controls.Add(RichTextBox1)
        replacements.Add("a bad advice", {New TextReplacement("a bad suggestion", "Replace advice with suggestion."),
                                      New TextReplacement("some bad advice", "Replace a with some.")})
        replacements.Add("irregardless", {New TextReplacement("regardless", "Irregardless is not a word."),
                                      New TextReplacement("in spite", "Reword the phrase.")})
        RichTextBox1.Text = "You provided a bad advice, irregardless of your intention. You provided a bad advice, irregardless of your intention. "
    End Sub

Public Class TextReplacement
    Public Property Text As String
    Public Property Reason As String
    Public Sub New(replacementText As String, replacementReason As String)
        Text = replacementText
        Reason = replacementReason
    End Sub
End Class
Posted
Updated 25-May-16 22:39pm
v7
Comments
ZurdoDev 8-Dec-15 16:05pm    
What exactly is your question?
ZurdoDev 8-Dec-15 16:13pm    
I fixed the pre tags so your code is visible now.

However, I'm still lost as to what your question is? It sounds like you are asking someone to read through all your code and make changes so it uses a dictionary object instead? You'll need to narrow down much better what you need help with.
[no name] 9-Dec-15 7:02am    
I want to be able to use a dictionary instead of a preloaded list
ZurdoDev 9-Dec-15 7:02am    
You have said that many times. That does not tell us where you are stuck or how we can help.
[no name] 9-Dec-15 7:11am    
I want to use a dictionary file instead of a preloaded list. I have just edited the question.

1 solution

This cromulent code should embiggen your knowledge.

C#
public class Replacement
{
    public string Word { get; set; }
    public List<string> Replacements { get; set; }
}


If you want to process the file without reading it all in memory then you can do something like this

C#
StreamReader sr = new StreamReader(@"c:\words.txt");

string word = string.Empty;
string lastWord = string.Empty;
string line = string.Empty;
char[] sep = new char[]{'|'};

Replacement r = null;

while ((line = sr.ReadLine()) != null)
{
    string[] values = line.Split(sep, StringSplitOptions.RemoveEmptyEntries);
    if (values.Length == 2)
    {
        lastWord = word;
        word = values[0];
        string replacement = values[1];

        if (word != lastWord)
        {
            Process(r);
            r = new Replacement();
            r.Replacements = new List<string>();
            r.Word = word;

            r.Replacements.Add(replacement);
        }
        else
        {
            r.Replacements.Add(replacement);
        }
    }
}

Process(r);


C#
public static void Process(Replacement replacement)
{
    if (replacement == null)
    {
        return;
    }

    Console.WriteLine("Possible replacements for {0}", replacement.Word);

    int i = 0;
    foreach(string r in replacement.Replacements)
    {
        Console.WriteLine("{0}: {1}", ++i, r);
    }
}


If you want to just create the dictionary from the file but keep the whole dictionary in memory then

C#
StreamReader sr = new StreamReader(@"c:\words.txt");

string word = string.Empty;
string lastWord = string.Empty;
string line = string.Empty;
char[] sep = new char[]{'|'};

Dictionary<string, Replacement> data = new Dictionary<string, Replacement>();

Replacement r = null;

while ((line = sr.ReadLine()) != null)
{
    string[] values = line.Split(sep, StringSplitOptions.RemoveEmptyEntries);
    if (values.Length == 2)
    {
        lastWord = word;
        word = values[0];
        string replacement = values[1];

        if (word != lastWord)
        {
            if (r != null)
            {
                data.Add(r.Word, r);
            }
            r = new Replacement();
            r.Replacements = new List<string>();
            r.Word = word;

            r.Replacements.Add(replacement);
        }
        else
        {
            r.Replacements.Add(replacement);
        }
    }
}

data.Add(r.Word, r);

foreach (string key in data.Keys)
{
    Process(data[key]);
}
 
Share this answer
 
Comments
[no name] 9-Dec-15 7:50am    
I will see how I can convert it to VB.
[no name] 12-Dec-15 13:58pm    
I was afraid to ask, how do you do this in VB?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900