AmazingMrBrock
AmazingMrBrock

Reputation: 197

In C# I want to check a dictionary against multiple keys

I'm sort of making my firstish program after doing lots of tutorials. Its more or less a console program that I type sentences at and it does things.

I have it working on a keyword system where it gets rid of words less than 3 characters and adds everything else to a dynamically generated dictionary.

So far I've been using ContainsKey() to check it for keys one at a time. What I really want to do is use another dictionary, or a list, or an array to hold a bunch of keys to run through the dictionary.

For example if I had a list of greetings:

{ "hi", "hey", "hello" }

and I want the program to output the same text for each of those. There must be a better way than having a separate if statement for each word I want to check the dictionary for.

I've done some web searching on this topic and I keep reading about something called an IEqualityComparer but to be honest it sounds like its beyond my abilities. Is there another way to do this or should I just jump in with the IEqualityComparer and try to muddle my way through with something I don't understand?

class MainClass
{
    static string Line;

    public static void Main (string[] args)
    {

        while (true) {
            if (Line == null){
                Console.WriteLine ("Enter Input");
            }
            WordChecker ();
        }
    }

    public static void WordChecker()
    {
        string inputString = Console.ReadLine ();
        inputString = inputString.ToLower();
        string[] stripChars = { 
            ";", ",", ".", "-", "_", "^", "(", ")", "[", "]", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9", "\n", "\t", "\r" 
        };

        foreach (string character in stripChars)
        {
            inputString = inputString.Replace(character, "");
        }

        // Split on spaces into a List of strings
        List<string> wordList = inputString.Split(' ').ToList();

        // Define and remove stopwords
        string[] stopwords = new string[] { "and", "the", "she", "for", "this", "you", "but" };
        foreach (string word in stopwords)
        {
            // While there's still an instance of a stopword in the wordList, remove it.
            // If we don't use a while loop on this each call to Remove simply removes a single
            // instance of the stopword from our wordList, and we can't call Replace on the
            // entire string (as opposed to the individual words in the string) as it's
            // too indiscriminate (i.e. removing 'and' will turn words like 'bandage' into 'bdage'!)
            while ( wordList.Contains(word) )
            {
                wordList.Remove(word);
            }
        }

        // Create a new Dictionary object
        Dictionary<string, int> dictionary = new Dictionary<string, int>();

        // Loop over all over the words in our wordList...
        foreach (string word in wordList)
        {
            // If the length of the word is at least three letters...
            if (word.Length >= 3)
            {
                // ...check if the dictionary already has the word.
                if ( dictionary.ContainsKey(word) )
                {
                    // If we already have the word in the dictionary, increment the count of how many times it appears
                    dictionary[word]++;
                }
                else
                {
                    // Otherwise, if it's a new word then add it to the dictionary with an initial count of 1
                    dictionary[word] = 1;
                }
            }
            if (dictionary.ContainsKey ("math")) {
                Console.WriteLine ("What do you want me to math?");
                Math ();
            }
            if(dictionary.ContainsKey("fruit"))
            {Console.WriteLine("You said something about fruit");}
        }
    }

    public static void Math()
    {
        Console.WriteLine ("input a number");
        string input = Console.ReadLine ();
        decimal a = Convert.ToDecimal (input);

        Console.WriteLine("Tell me math function");
        string mFunction = Console.ReadLine();

        Console.WriteLine ("tell me another number");
        string inputB = Console.ReadLine();
        decimal b = Convert.ToDecimal (inputB);

        if (mFunction == "add")
        {
            Console.WriteLine (a + b);
        }
        else if (mFunction == "subtract")
        {
            Console.WriteLine (a - b);
        }
        else if (mFunction == "multiply")
        {
            Console.WriteLine (a * b);
        }
        else if (mFunction == "divide")
        {
            Console.WriteLine (a / b);
        }
        else
        {
            Console.WriteLine ("not a math");
        }
    }

    public static void Greetings()
    {

    }
}

Note: I got the dynamic dictionary and word parser form an example I found online and modified it a bit to suit my needs. I wouldn't have come up with it on my own but I do feel like I understand the code in it.

Upvotes: 0

Views: 3093

Answers (1)

Despertar
Despertar

Reputation: 22362

What I really want to do is use another dictionary, or a list, or an array to hold a bunch of keys to run through the dictionary.

Here is an example of using one dictionary that holds the keys. Multiple values can map to the same key. If this isn't what you want perhaps you could clarify what exactly you are struggling with?

Dictionary<string, string> keyLookup = new Dictionary<string, string>();
keyLookup["hey"] = "greeting";
keyLookup["hi"] = "greeting";
keyLookup["hello"] = "greeting";

Dictionary<string, int> wordLookup = new Dictionary<string, int>();
wordLookup["greeting"] = 1;

public int GetWordCount(string word)
{
    string foundKey = keyLookup[word];
    int numOccurences = wordLookup[foundKey];
    return numOccurences;
}

Upvotes: 1

Related Questions