实现字典接口



这是IEnumerable接口,其中添加一些单词并在最后两种方法中返回。

这里的想法是,您有一个 Dicitionary,它将每个单词与作为同义词的单词列表相关联。AddSynonyms 方法将采用同义词的枚举,并从创建唯一单词的列表开始,以防有任何重复。然后,它会遍历每个单词并检查它是否已添加。如果有,那么它会遍历该单词的所有同义词(单词列表减去单词(,并检查它们是否在与该单词关联的列表中。如果不是,则同义词将添加到列表中。如果该单词尚未在 Dicitionary 中,则将其与其同义词列表一起添加(再次是单词列表减去单词(。

然后,GetSynonymns 方法只返回与给定单词关联的列表。如果该单词不在同义词库中,则返回一个空枚举(如果需要,您可以将其更改为引发异常(。

GetWords 方法只是返回 Dicitionary 的所有键,这些键将是添加的每个单词。

summary>
/// Represents a thesaurus.
/// </summary>
public interface IThesaurus
{
/// <summary>
/// Adds the given synonyms to the thesaurus
/// </summary>
/// <param name="synonyms">The synonyms to add.</param>
void AddSynonyms(IEnumerable<string> synonyms);
/// <summary>
/// Gets the synonyms for a given word.
/// </summary>
/// <param name="word">The word the synonyms of which to get.</param>
/// <returns>A <see cref="string"/> with all synonyms for the given word.</returns>
IEnumerable<string> GetSynonyms(string word);
/// <summary>
/// Gets all words from the thesaurus.
/// </summary>
/// <returns>An <see cref="IEnumerable<string>"/> containing
/// all the words in the thesaurus.</returns>
IEnumerable<string> GetWords();

} }

这就是我走多远,但似乎卡在了这个上面。程序无法正确编译。我无法使用 obj 调用我的方法。GetSynonyms((;.我的公共同义词库类的底部 公共 IEnumerable GetWords(( { 返回查找。键((;} } 不起作用,我只是不明白为什么它不起作用。我真的无法发现代码的问题。

namespace Thesaurus
{
public interface IThesaurus
{
IEnumerable<string> AddSynonyms();
IEnumerable<string> GetSynonyms(string word);
IEnumerable<string> GetWords();
}

public class Thesaurus : IThesaurus
{
private Dictionary<string, List<string>> lookup =
new Dictionary<string, List<string>>();
public IEnumerable<string> AddSynonyms() //public void AddSynonyms(IEnumerable<string> synonyms)
{
var words = AddSynonyms.Distinct().ToList();
foreach (var word in words)
{
var currentWordSynonyms = words.Where(s => s == word);
if (lookup.ContainsKey(word))
{
foreach (var synonym in currentWordSynonyms)
{
if (!lookup[word].Contains(synonym))
lookup[word].Add(synonym);
}
}
else
{
lookup.Add(word.currentWordSynonyms);
}
}
}
public IEnumerable<string> GetSynonyms(string word)
{
if (lookup.ContainsKey(word))
return lookup[word];
return Enumerable.Empty<string>();
// Or throw an exception.
}
public IEnumerable<string> GetWords()
{
return lookup.Keys();
}
}
class MainClass
{
static void Main()
{
// Declare an interface instance.
IThesaurus obj = new Thesaurus();

// Call the member.
obj.AddSynonyms();
obj.GetSynonyms();
obj.GetWords();
}
}

}

发现编译器错误:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thesaurus
{

class MainClass
{
static void Main(string[] args)
{
// Declare an interface instance.
IThesaurus obj = new Thesaurus();
List<string> words = new List<string>();
// Call the member.
obj.AddSynonyms(words);
obj.GetSynonyms(words[0]);
obj.GetWords();
}
}
//<summary>
/// Represents a thesaurus.
/// </summary>
public interface IThesaurus
{
/// <summary>
/// Adds the given synonyms to the thesaurus
/// </summary>
/// <param name="synonyms">The synonyms to add.</param>
void AddSynonyms(IEnumerable<string> synonyms);
/// <summary>
/// Gets the synonyms for a given word.
/// </summary>
/// <param name="word">The word the synonyms of which to get.</param>
/// <returns>A <see cref="string"/> with all synonyms for the given word.</returns>
IEnumerable<string> GetSynonyms(string word);
/// <summary>
/// Gets all words from the thesaurus.
/// </summary>
/// <returns>An <see cref="IEnumerable<string>"/> containing
/// all the words in the thesaurus.</returns>
IEnumerable<string> GetWords();
}
public class Thesaurus : IThesaurus
{
private Dictionary<string, List<string>> lookup =
new Dictionary<string, List<string>>();

public void AddSynonyms(IEnumerable<string> allWords) //public void AddSynonyms(IEnumerable<string> synonyms)
{
var words = allWords.Distinct().ToList();
foreach (var word in words)
{
var currentWordSynonyms = words.Where(s => s == word);
if (lookup.ContainsKey(word))
{
foreach (var synonym in currentWordSynonyms)
{
if (!lookup[word].Contains(synonym))
lookup[word].Add(synonym);
}
}
else
{
List<string> newSynonyms =  new List<string>();
newSynonyms.AddRange(currentWordSynonyms);
lookup.Add(word, newSynonyms);
}
}
}
public IEnumerable<string> GetSynonyms(string word)
{
if (lookup.ContainsKey(word))
return lookup[word];
return Enumerable.Empty<string>();
// Or throw an exception.
}
public IEnumerable<string> GetWords()
{
return lookup.AsEnumerable().Select(x => x.Key).ToList();
}
}

}

最新更新