我正在开发一个 C# 应用程序,其中用户提供一组单词(通常少于 10 个),我需要检索这些单词的所有同义词。这是我第一次使用字典和这些东西。我需要知道要遵循的步骤,以及是否存在提供同义词的现有词典,我可以将其与我的应用程序集成,或者是否有开源应用程序或我可以使用的代码。
问问题
601 次
2 回答
1
回答你的第一个问题。您可以在此处找到词库下载:http ://wordpresscloaker.com/blog/download-free-english-thesaurus-format-txt.html
我对该文件的质量、准确性、合法性、使用许可或完整性不作任何承诺。但是,这将使您上路。您需要提取 mthesaur.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 的内容,但这应该会让您顺利进行。
于 2013-03-10T23:02:09.373 回答
0
如果您使用SQL 全文搜索或底层技术 - Microsoft Search Server(有一个免费的Express SKU),您将找到多种语言的词库和其他自然语言处理工具。我当然假设你正在做一个实际的项目,而不是做家庭作业......
如果您更喜欢开源,请查看Lucene.net - 它提供了一个搜索引擎,我很确定它有同义词库
于 2013-03-10T23:07:17.327 回答