Thesaurus Implementation

J

johnerics

Hi Everybody!

Hoping that every body would be fine at this group.

I am learning c sharp. Please, if anybody can make this program, i
would be highly obliged.

Implement a thesaurus (a dictionary of synonyms) that implements the
IThesaurus interface
Interface IThesaurus
{
// Adds a word and its synonyms to the thesaurus
void AddWord(string word, string[] synonyms);
// Removes a word from the thesurus
void RemoveWord(string word);
// Find the synonyms associated with a word
string[] FindSynonyms(string word);
// Returns a sorted list of all the words in the //thesaurus
string[] WordList();
}
Discuss your implementation. What kind of performance can you expect
from the operations? What should be done to make it threadsafe?

Thanks in Advance.

John
 
M

Martin CLAVREUIL

Hi,

Did you try by yourself first ?
Nobody here will do a program your asked to do but we can help.
Try to use a hashtable in order to store the data.

Good work,

Martin
 
J

johnerics

This is the code i tried so far, but it is giving error.

using System;
using System.Collections;

namespace Thesaurus
{

public interface IThesaurus
{
// Adds a word and its synonyms to the thesaurus
void AddWord(string word, string[] synonyms);
// Removes a word from the thesurus
void RemoveWord(string word);
// Find the synonyms associated with a word
string[] FindSynonyms(string word);
// Returns a sorted list of all the words in the //thesaurus
string[] WordList();
}

public class ImpIThesaurus:IThesaurus
{
#region IThesaurus Members
public Hashtable ThesDict;

public ImpIThesaurus()
{
ThesDict = new Hashtable();
}


public void AddWord(string word, string[] synonyms)
{
for ( int i = 0; i < (synonyms.Length - 1); i++ )
{
ThesDict.Add(word,synonyms);
}


}

public void RemoveWord(string word)
{
// TODO: Add ImpIThesaurs.RemoveWord implementation
}

public string[] FindSynonyms(string word)
{
// TODO: Add ImpIThesaurs.FindSynonyms implementation
return null;
}

public string[] WordList()
{
// TODO: Add ImpIThesaurs.WordList implementation
return null;
}

#endregion

}

/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ImpIThesaurus trs = new ImpIThesaurus();
string[] list = {"a","b","c"};
trs.AddWord("A",list);

//
// TODO: Add code to start application here
//
}
}
}

Martin said:
Hi,

Did you try by yourself first ?
Nobody here will do a program your asked to do but we can help.
Try to use a hashtable in order to store the data.

Good work,

Martin

Hi Everybody!

Hoping that every body would be fine at this group.

I am learning c sharp. Please, if anybody can make this program, i
would be highly obliged.

Implement a thesaurus (a dictionary of synonyms) that implements the
IThesaurus interface
Interface IThesaurus
{
// Adds a word and its synonyms to the thesaurus
void AddWord(string word, string[] synonyms);
// Removes a word from the thesurus
void RemoveWord(string word);
// Find the synonyms associated with a word
string[] FindSynonyms(string word);
// Returns a sorted list of all the words in the //thesaurus
string[] WordList();
}
Discuss your implementation. What kind of performance can you expect
from the operations? What should be done to make it threadsafe?

Thanks in Advance.

John
 
J

Jon Skeet [C# MVP]

This is the code i tried so far, but it is giving error.

Yes, because you can only have any particular key once in a hashtable.
You'll need to make the value for the entry the array of strings, or a
list, or something like that.

Jon
 
J

johnerics

Hi,

Thanks for the instant reply.

I am not allowed to do that. I have to follow the interface only.
Therefore, i cannot use string array for one value. Thus, i have no
clue what to do.

Please if anyone can help me out.
 
J

Jon Skeet [C# MVP]

Thanks for the instant reply.

I am not allowed to do that. I have to follow the interface only.
Therefore, i cannot use string array for one value. Thus, i have no
clue what to do.

Please if anyone can help me out.

The interface doesn't specify how you have to implement it. You could
easily follow the interface and use an array (or an ArrayList) as the
value in your hashtable, copying contents where necessary. How you
store things internally doesn't have to have an impact on how you
expose the data to the outside world.

Jon
 
J

johnerics

Hi Jon,

I have implemented the method like this now:

public void AddWord(string word, string[] synonyms)
{
string[] wrd = new string[synonyms.Length];

for ( int i = 0; i < (synonyms.Length - 1); i++ )
{
wrd = word;
ThesDict.Add(wrd,synonyms);
}


}

But it still give error.

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll

Additional information: Item has already been added. Key in
dictionary: "A" Key being added: "A"

The program '[3064] Thesaurus.exe' has exited with code 0 (0x0).
 
J

johnerics

Hi Jon,

I have implemented the method like this now:

public void AddWord(string word, string[] synonyms)
{
string[] wrd = new string[synonyms.Length];

for ( int i = 0; i < (synonyms.Length - 1); i++ )
{
wrd = word;
ThesDict.Add(wrd,synonyms);
}


}

But it still give error.

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll

Additional information: Item has already been added. Key in
dictionary: "A" Key being added: "A"

The program '[3064] Thesaurus.exe' has exited with code 0 (0x0).
 
J

Jon Skeet [C# MVP]

I have implemented the method like this now:

public void AddWord(string word, string[] synonyms)
{
string[] wrd = new string[synonyms.Length];

for ( int i = 0; i < (synonyms.Length - 1); i++ )
{
wrd = word;
ThesDict.Add(wrd,synonyms);
}


}

But it still give error.


Yes, it would - you're still adding the same key multiple times, adding
just a single word each time.

Instead, make a copy of the array (eg into an ArrayList) and call Add a
single time. You'll also need to check for existing values in case
someone calls
Add ("x", firstArray);
Add ("x", secondArray);

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top