我尝试创建一个倒排索引,但它不起作用。我的代码没有错误,但不起作用。它出什么问题了?
我每次都得到这个异常: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