我应该使用哪种数据类型和方法?

时间:2014-05-28 11:14:13

标签: c# dictionary nlp search-engine trie

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

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

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

1 个答案:

答案 0 :(得分:2)

您正在寻找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");