【问题标题】:Which datatype and methods should I use?我应该使用哪种数据类型和方法?
【发布时间】:2014-09-03 22:39:37
【问题描述】:

我正在尝试编写一种简单的搜索引擎。我有一定数量的与特定关键字相关的主要主题。目的是从输入的部分关键字中识别主要主题。我正在考虑使用:Dictionary<string, List<string>>。我必须在这本词典中搜索并找到,例如,所有以 3 个字符 string 开头的关键字及其相关联的主要主题。

我的解决方案是最好的吗?以及如何有效地查看这些数据,而无需手动检查每个 Liststringstring

如果我不清楚,请告诉我。

【问题讨论】:

  • 那么您是只搜索关键字还是关键字和内容?我之所以问,是因为 我怎样才能有效地查看这些数据,而不必手动检查每个列表,逐个字符串 不适合只查看关键字。
  • 您是在寻找精确的键匹配还是以开头?
  • 不,我将按部分关键字进行搜索。所以就像斯里拉姆所说的那样,“开始于”。

标签: c# dictionary nlp search-engine trie


【解决方案1】:

您正在寻找Trie data structure,这是推荐的从搜索开始的方式。这是blog post 谈论它。你可以找到source here

这里是如何使用上面的实现,代码来自上面的文章。

//Create trie
Trie < string > trie = new Trie < string > ();

//Add some key-value pairs to the trie
trie.Put("James", "112");
trie.Put("Jake", "222");
trie.Put("Fred", "326");

//Search the trie
trie.Matcher.NextMatch('J'); //Prefix thus far: "J"
trie.Matcher.GetPrefixMatches(); //[112, 222]
trie.Matcher.IsExactMatch(); //false
trie.Matcher.NextMatch('a');
trie.Matcher.NextMatch('m'); //Prefix thus far: "Jam"
trie.Matcher.GetPrefixMatches(); //[112]
trie.Matcher.NextMatch('e');
trie.Matcher.NextMatch('s'); //Prefix thus far: "James"
trie.Matcher.IsExactMatch(); //true
trie.Matcher.GetExactMatch(); //112

//Remove a string-value pair
trie.Remove("James");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多