0

我试图使用 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 文件是一个文件,其中每一行都有一个单词

快照: 在此处输入图像描述

谢谢你

4

4 回答 4

3

这只是一个猜测(评论太长了),但是当你插入你的 时Dictionary,你会被\n.

因此,如果您正在使用的文本文件中的实际拆分器是\r\n,那么您将在插入的键上留下\r',因此在ContainsKey.

所以我会从string[] stopWordsSeperated = stopWordsContent.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);修剪开始


附带说明一下,如果您没有使用字典 int 值来表示任何内容,那么最好使用 aHashSet<string>Contains而不是ContainsKey

于 2015-11-13T09:02:37.660 回答
1

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

于 2015-11-13T08:29:57.690 回答
0

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

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);
        }
于 2015-11-13T08:48:03.503 回答
0

我看到您将 1 设置为所有条目的值。也许List会更好地满足您的需求:

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
}
于 2015-11-13T09:07:50.640 回答