Thesaurus Implementation

  • Thread starter Thread starter johnerics
  • Start date Start date
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
 
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
 
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
 
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
 
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.
 
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
 
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).
 
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).
 
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
 
Back
Top