在c#中,我想根据多个键检查字典
本文关键字:检查 字典 | 更新日期: 2023-09-27 18:15:11
在做了很多教程之后,我正在制作我的第一个程序。它或多或少是一个控制台程序,我在上面输入句子,然后它做一些事情。
我有一个关键字系统,它摆脱了少于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;
}