【问题标题】:what's wrong with this C# code [closed]这段 C# 代码有什么问题[关闭]
【发布时间】:2013-04-30 11:20:40
【问题描述】:

我尝试创建一个倒排索引,但它不起作用。我的代码没有错误,但不起作用。有什么问题?

我每次都遇到这个异常:KeyNotFoundException was unhandled : the given Key was not present in the dictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace ConsoleApplication1
{
     class Program
     {
        static Dictionary<TItem, IEnumerable<TKey>> Invert<TKey, TItem>(Dictionary<TKey, IEnumerable<TItem>> dictionary)
        {
            return dictionary
              .SelectMany(keyValuePair => keyValuePair.Value.Select(item => new KeyValuePair<TItem, TKey>(item, keyValuePair.Key)))
            .GroupBy(keyValuePair => keyValuePair.Key)
            .ToDictionary(group => group.Key, group => group.Select(keyValuePair => keyValuePair.Value));
        }

        static void Main(string[] args)
        {
            Console.WriteLine("files: ");
            //read all the text document on the specified directory; change this directory based on your machine
            foreach (string file in Directory.EnumerateFiles("D:\\IR\\", "*.txt"))
            {
            string contents = File.ReadAllText(file);
            Console.WriteLine(contents);
            Console.Write("find: ");
            var find = Console.ReadLine();
            var dictionary = file.Split().ToDictionary(files => files, files => File.ReadAllText(file).Split().AsEnumerable());
            Console.WriteLine("{0} found in: {1}", find, string.Join(" ", Invert(dictionary)[find]));
            }
         }
     }
}

我编辑了代码,现在它可以正常工作,没有错误和异常。现在还有另一个问题,它必须只能读取所有文件一次,但我的意思不是每次都读取其中一个,然后执行查找过程,这不是我的意思,我需要像下面写的那样输出。看来我应该将foreach(string file in Dictionary.EnumerateFiles("D:\\IR\\","*.txt")) 更改为其他内容。但我不知道它是什么。

输出应该是这样的:

文件:文件1 文件2 文件3

查找:什么

在:file1 file2 中找到的内容

【问题讨论】:

  • 怎么不行?它做错了什么?
  • 通常情况下,您可以通过单步执行代码并执行完整性检查来修复此类错误,以确保您的变量具有您期望它们具有的值。只是我的两分钱。
  • 就是无法读取最后一行,你能告诉我为什么吗?
  • @Tejas 我猜这可能是“ Program.Invert(dictionary)[find] ”中的问题,不是吗?
  • 嗯,你的Invert 版本似乎工作得很好。

标签: c# information-retrieval inverted-index


【解决方案1】:

编辑:我测试了您的Invert 版本,它似乎工作得很好。您是否对默认的ToString() 输出感到困惑?如果您直接尝试打印Program.Invert(dictionary)[find],您将不会得到您期望的输出,因为默认的ToString() 不是很有帮助(因为您尝试打印的对象不会覆盖object.ToString())。您将不得不遍历您的值列表并单独打印每个值。假设TKey 覆盖ToString(),您将获得所需的输出。例如,

Console.WriteLine("{0} found in: ", find);
foreach (var val in Program.Invert(dictionary)[find])
{
    Console.WriteLine(val);
}

上一个答案(为后代保留): 好的,您提到您的 Invert 方法有问题。我尝试编写自己的Invert 方法,这就是我想出的。我已经将方法调用分解为单独的部分,以便清楚我在做什么。

static Dictionary<TItem, IEnumerable<TKey>> Invert<TKey, TItem>(Dictionary<TKey, IEnumerable<TItem>> dictionary)
{
    // Get collection of all the values which will be keys in the new inverted dictionary
    var invertedDictKeys = dictionary.SelectMany(keyValPair => keyValPair.Value).Distinct();

    // Perform the invert
    var invertedDict = 
        invertedDictKeys.Select(
            invertedKey => 
            new KeyValuePair<TItem, IEnumerable<TKey>>(invertedKey, dictionary.Keys.Where(key => dict[key].Contains(invertedKey))));

    // Convert to dictionary and return
    return invertedDict.ToDictionary(keyValPair => keyValPair.Key, keyValPair => keyValPair.Value);
}

【讨论】:

  • 不,它不能正常工作,即使在单独的版本中,我仍然有同样的错误“未找到密钥异常未处理”。当我将“ Directory.EnumerateFiles ”更改为“ Dictionary.GetFiles ”时,它起作用了,但问题是它只适用于第一个 .txt 文件,但我有五个 .txt 文件。另一个问题是,即使我获得了我唯一的第一个 .txt 文件的输出,错误仍然存​​在。我很困惑真的不知道该怎么办
  • 好吧,您应该提到这是抛出异常。在 SO 问题中提供尽可能多的信息通常是个好主意。
  • 好的,现在你怎么看?有什么办法吗?
  • 我每次都得到这个异常:'未找到密钥异常未处理:给定的密钥不在字典中'。
  • 该异常意味着您要搜索的内容实际上并不存在于倒排字典中。您能否从您正在阅读的文本文件中发布一些示例文本以及您在程序中输入的内容?
猜你喜欢
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多