C# 字典 API(词法)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/335517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:41:46  来源:igfitidea点击:

Dictionary API (lexical)

c#.netapi

提问by flesh

Does anyone know a good .NET dictionary API? I'm not interested in meanings, rather I need to be able to query words in a number of different ways - return words of x length, return partial matches and so on...

有谁知道一个好的 .NET 字典 API?我对意义不感兴趣,而是我需要能够以多种不同的方式查询单词 - 返回 x 长度的单词,返回部分匹配等等......

采纳答案by James Orr

Grab the flat text file from an open source spellchecker like ASpell (http://aspell.net/) and load it into a List or whatever structure you like.

从像 ASpell ( http://aspell.net/)这样的开源拼写检查器中获取纯文本文件并将其加载到列表或您喜欢的任何结构中。

for example,

例如,

List<string> words = System.IO.File.ReadAllText("MyWords.txt").Split(new string[]{Environment.NewLine}).ToList();

// C# 3.0 (LINQ) example:

    // get all words of length 5:
    from word in words where word.length==5 select word

    // get partial matches on "foo"
    from word in words where word.Contains("foo") select word

// C# 2.0 example:

    // get all words of length 5:
    words.FindAll(delegate(string s) { return s.Length == 5; });

    // get partial matches on "foo"
    words.FindAll(delegate(string s) { return s.Contains("foo"); });

回答by Jon Skeet

You might want to look for a Trieimplementation. That will certainly help with "words starting with XYZ" as well as exact matches. You may well want to have all of your data in multiple data structures, each one tuned for the particular task - e.g. one for anagrams, one for "by length" etc. Natural language dictionaries are relatively small compared with RAM these days, so if you really want speedy lookup, that's probably the way to go.

您可能想要寻找Trie实现。这肯定有助于“以 XYZ 开头的单词”以及精确匹配。您可能希望将所有数据保存在多个数据结构中,每个数据结构都针对特定任务进行调整 - 例如,一个用于字谜,一个用于“按长度”等。如今,与 RAM 相比,自然语言词典相对较小,因此如果你真的想要快速查找,这可能是要走的路。

回答by rmeador

Depending on how involved your queries are going to be, it might be worth investigating WordNet, which is basically a semantic dictionary. It includes parts of speech, synonyms, and other types of relationships between the words.

根据您的查询涉及的程度,可能值得研究WordNet,它基本上是一个语义词典。它包括词性、同义词和词之间的其他类型的关系。

回答by Mike Hall

NetSpell (http://www.loresoft.com/netspell/) is a spell checker that's written in .NET that has word listings in several languages that you could use.

NetSpell ( http://www.loresoft.com/netspell/) 是一个用 .NET 编写的拼写检查器,它具有您可以使用的多种语言的单词列表。

回答by Anthony Mastrean

I'm with Barry Fandangoon this one, but you can do it without LINQ. .NET 2.0 has some nice filtering methods on the List(T) type. The one I suggest is

我和Barry Fandango 一起讨论这个问题,但你可以在没有 LINQ 的情况下做到这一点。.NET 2.0 在 List(T) 类型上有一些不错的过滤方法。我建议的是

List(T).FindAll(Predicate(T)) : List(T)

This method will put every element in the list through the predicate method and return the list of words that return 'true'. So, load your words as suggested from an open source dictionary into a List(String). To find all words of length 5...

此方法将通过谓词方法将每个元素放入列表中,并返回返回“true”的单词列表。因此,按照开源词典中的建议将您的单词加载到 List(String) 中。查找所有长度为 5 的单词...

List(String) words = LoadFromDictionary();
List(String) fiveLetterWords = words.FindAll(delegate(String word)
    {
        return word.Length == 5;
    });

Or for all words starting with 'abc'...

或者对于所有以 'abc' 开头的单词...

List(String) words = LoadFromDictionary();
List(String) abcWords = words.FindAll(delegate(String word)
    {
        return word.StartsWith('abc');
    });