如何进行查询扩展

本文关键字:扩展 查询 何进行 | 更新日期: 2023-09-27 18:03:11

我正在开发一个c#应用程序,其中用户提供了一组单词(通常少于10个),我需要检索这些单词的所有同义词。这是我第一次使用字典和这些东西。我需要知道要遵循的步骤,如果有一个现有的字典,提供同义词,我可以集成到我的应用程序,或者如果有一个开源的应用程序或代码,我可以使用。

如何进行查询扩展

回答你的第一个问题。你可以在这里找到一个词典下载:http://wordpresscloaker.com/blog/download-free-english-thesaurus-format-txt.html

我不保证该文件的质量、准确性、合法性、使用许可或完整性。然而,这会让你继续前进。您需要提取mthesaurr .txt并将其添加到您的项目文件夹中。

接下来,您需要执行以下操作来读入文本文件:

var reader = new StreamReader(File.OpenRead(@"C:'mthesaur.txt"));
var dict = new Dictionary<string, string>();
while (!reader.EndOfStream)
{
    // Read the file line by line.
    var line = reader.ReadLine();
    // If the line isn't null, we can use it.  This shouldn't happen but it is a good sanity check.
    if (line == null) continue;
    // Split the line by the delimiter (a comma) so we can get the main word, the first one on the line.
    var splitLine = line.Split(',');
    var mainWord = splitLine[0];
    // To save us from having to loop through and only get the indexes above 0 (eg, skip the main word) we will just simply remove it from the line so we have just synonyms.
    line = line.Replace(mainWord + ",", string.Empty);
    // Now we make use of the dictionary type in C# and add the mainword as the key and the synonyms as the value.
    try
    {
        dict.Add(mainWord, line);
    }
    catch (ArgumentException argEx)
    {
        Console.WriteLine("Attempted to add {0} to the dictionary but it already exists.", mainWord);
    }
}

现在我们在c#中有了键/值字典中的所有内容,您可以使用LINQ查询输入的单词的同义词。这可以通过使用包含字典中所有键值的下拉框(不推荐,因为这将是一个非常大的下拉框,用户很难导航),ListBox(更好,更容易导航)或纯文本搜索框来完成。虽然这并不能完全回答你的问题,因为这里没有任何关于为用户处理GUI的内容,但这应该会让你在你的路上走得很好。

如果您使用SQL全文搜索或底层技术- Microsoft search Server(有一个免费的Express SKU),您将找到多种语言和其他自然语言处理工具的同义词库。我当然假设你是在做一个实际的项目,而不是在做家庭作业。

如果你对开源更感兴趣,可以看看Lucene.net——它提供了一个搜索引擎,我很确定它有搜索引擎