【发布时间】:2026-01-06 02:50:01
【问题描述】:
我正在为一些大数字制作一个布尔检索系统。文档,我在其中制作了一个哈希集字典,字典中的条目是术语,哈希集包含找到该术语的文档ID。 现在,当我想搜索单个单词时,我只需输入该单词,然后我将使用查询中输入的单词索引字典并打印出相应的哈希集。 但是我也想搜索句子,在这种情况下,我会将查询拆分为单个单词并按这些单词索引字典,现在取决于查询中的单词数,现在将返回许多哈希集我将想要取这些哈希集的交集,以便我可以返回文档 id,在其中我可以找到查询中的单词。 我的问题是获取这些哈希集交集的最佳方法是什么?
目前我将哈希集放入一个列表中,然后我取这些 n 的交集。一次两个哈希集,然后取前两个结果的交集,然后是第三个,依此类推...
这是代码
Dictionary<string, HashSet<string>> dt = new Dictionary<string, HashSet<string>>();//assume it is filled with data...
while (true)
{
Console.WriteLine("\n\n\nEnter the query you want to search");
string inp = Console.ReadLine();
string[] words = inp.Split(new Char[] { ' ', ',', '.', ':', '?', '!', '\t' });
List<HashSet<string>> outparr = new List<HashSet<string>>();
foreach(string w in words)
{
HashSet<string> outp = new HashSet<string>();
if (dt.TryGetValue(w, out outp))
{
outparr.Add(outp);
Console.WriteLine("Found {0} documents.", outp.Count);
foreach (string s in outp)
{
Console.WriteLine(s);
}
}
}
HashSet<string> temp = outparr.First();
foreach(HashSet<string> hs in outparr)
{
temp = new HashSet<string>(temp.Intersect(hs));
}
Console.WriteLine("Output After Intersection:");
Console.WriteLine("Found {0} documents: ", temp.Count);
foreach(string s in temp)
{
Console.WriteLine(s);
}
}
【问题讨论】:
-
所以你的代码目前可以工作?那么这个问题会更适合 CodeReview.SE
-
如果在任何文档中都找不到任何单词,outparr.First() 会抛出异常吗?
标签: c# c#-4.0 dictionary hashset