繁体   English   中英

C#词典-ContainsKey函数返回错误的值

[英]C# Dictionary - ContainsKey Function Return Wrong Value

我试图使用Dictionary of映射一些单词(int并不那么相关)。 将单词插入dic后(我检查了它),我尝试遍历整个文档并查找特定单词。

当我这样做时,即使单词存在于dic中,它也会返回false。

可能是什么问题,我该如何解决?

public string RemoveStopWords(string originalDoc){
        string updatedDoc = "";
        string[] originalDocSeperated = originalDoc.Split(' ');
        foreach (string word in originalDocSeperated)
        {
            if (!stopWordsDic.ContainsKey(word))
            {
                updatedDoc += word;
                updatedDoc += " ";
            }
        }
        return updatedDoc.Substring(0, updatedDoc.Length - 1); //Remove Last Space
    }

例如:dic包含停用词,如单词“ the”。 当我从originalDoc中得到一个单词“ the”,然后想要检查它是否不存在时,它仍会输入IF语句,并且两者都写相同! 不区分大小写

Dictionary<string, int> stopWordsDic = new Dictionary<string, int>();

string stopWordsContent = System.IO.File.ReadAllText(stopWordsPath);
            string[] stopWordsSeperated = stopWordsContent.Split('\n');
            foreach (string stopWord in stopWordsSeperated)
            {
                stopWordsDic.Add(stopWord, 1);
            }

stopWords文件是在每一行中都有一个单词的文件

快照: 在此处输入图片说明

谢谢

这只是一个猜测(对于注释来说太长了),但是当您在Dictionary中插入时,您将被\\n分割。

因此,如果您正在使用的文本文件中的实际分隔符为\\r\\n ,则在插入的键上将留下\\r ,因此在ContainsKey上找不到它们。

因此,我将从string[] stopWordsSeperated = stopWordsContent.Split(new string[] { "\\r\\n", "\\n" }, StringSplitOptions.None); 然后修剪


附带说明一下,如果您不使用字典的int值作为任何内容,则最好使用HashSet<string>Contains而不是ContainsKey

你有一个 ! (而不是)if语句中的运算符。 您正在检查字典是否不包含键。 从条件开始时删除感叹号。

创建字典时,您需要执行以下操作:

var stopWords= new Dictionary<string, int>(
    StringComparer.InvariantCultureIgnoreCase);

最重要的部分是InvariantCultureIgnoreCase。

public string RemoveStopWords(string originalDoc){
    return String.Join(" ", 
           originalDoc.Split(' ')
              .Where(x => !stopWordsDic.ContainsKey(x))
    );
}

此外,您应该更改字典的填充方式(这会在创建字典时从字典中消除所有非单词符号):

        // Regex to find the first word inside a string regardless of the 
        // preleading symbols. Cuts away all nonword symbols afterwards
        Regex validWords = New Regex(@"\b([0-9a-zA-Z]+?)\b");

        string stopWordsContent = System.IO.File.ReadAllText(stopWordsPath);
        string[] stopWordsSeperated = stopWordsContent.Split('\n');

        foreach (string stopWord in stopWordsSeperated)
        {
            stopWordsDic.Add(validWords.Match(stopWord).Value, 1);
        }

我看到您正在将1设置为所有条目的值。 列表可能会更适合您的需求:

List<string> stopWordsDic = new List<string>();

string stopWordsContent = System.IO.File.ReadAllText(stopWordsPath);
string[] stopWordsSeperated = stopWordsContent.Split(Environment.NewLine);
foreach (string stopWord in stopWordsSeperated)
{
    stopWordsDic.Add(stopWord);
}

然后使用Contains()检查元素

public string RemoveStopWords(string originalDoc){
    string updatedDoc = "";
    string[] originalDocSeperated = originalDoc.Split(' ');
    foreach (string word in originalDocSeperated)
    {
        if (!stopWordsDic.Contains(word))
        {
            string.Format("{0}{1}", word, string.Empty);
            //updatedDoc += word;
            //updatedDoc += " ";
        }
    }
    return updatedDoc.Substring(0, updatedDoc.Length - 1); //Remove Last Space
}

暂无
暂无

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

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