繁体   English   中英

在C#中,我想针对多个键检查字典

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

经过大量的教程之后,我有点想做我的第一手程序。 它或多或少是一个控制台程序,可在其中键入句子并执行操作。

我让它在一个关键字系统上工作,该系统可以删除少于3个字符的单词,并将其他所有内容添加到动态生成的字典中。

到目前为止,我一直在使用ContainsKey()检查一次键。 我真正想做的是使用另一个字典,一个列表或一个数组来保存一串要在字典中运行的键。

例如,如果我有一个问候列表:

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

我希望程序为每个输出相同的文本。 对于要检查字典的每个单词,必须有一个单独的if语句更好的方法。

我已经在这个主题上进行了一些网络搜索,并且继续阅读有关IEqualityComparer但是老实说,这听起来像是我无法承受的。 还有另一种方法可以做到这一点,还是我应该跳入IEqualityComparer并尝试用我不理解的东西弄乱我的方式?

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()
    {

    }
}

注意:我在网上找到了一个示例,提供了动态字典和单词解析器,并对其做了一些修改以满足我的需要。 我不会自己提出它,但是我确实感觉自己理解其中的代码。

我真正想做的是使用另一个字典,一个列表或一个数组来保存一串要在字典中运行的键。

这是使用一个保存密钥的字典的示例。 多个值可以映射到同一键。 如果这不是您想要的,也许您可​​以弄清楚自己正在苦苦挣扎的是什么?

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM