Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashtable vs. Vector

Status
Not open for further replies.

duan

Technical User
May 30, 2001
9
GB
Dear,

I'm now working on a project which aims to do:

1. Read in a ASCII txt file which is a on-line English Dictionary.

I don't know which data structure I should use Hashtable or...? Hashtable seems suitable because it can be filled in with word and its definitions (correspond to key and elements) extract from the dictionary. But I don't know how to do it. You got any idea?

2. tokenize a text into words swquence and count the frequency of each words.

vector or hashtable? this component will communicate with the first one I just described above.

3. replace each words in the text just has been tokenized with the definations in the dictionary.

If you know how to deal with, please give me a hand.

Thanks very much.

duan
 
My suggestion: Use a HashTable for the first one (supposing you don't use synonym expressions) using the native language of the text as key and the other language as value. You can then
Code:
myHashTable.put(word, translation)
the entries.

For the second component I'd use a Vector - you don't have to map anything with anything else here. Just go through the text and fill every token into the Vector.
For the counting I'd use a HashTable again. The only "problem" here is that you can only put Objects into a HashTable, forcing you to use Integers instead of ints for the counting. You'll probably want to copy the proxy from below as an inner class that maps that:

Code:
class IntHashTable
{
  private HashTable hashTable = new HashTable;

  public void put(Object key, int value)
  {
    hashTable.put(key, new Integer(value));
  }

  public int get(Object key)
  {
    return ((Integer)hashTable.get(key)).intValue();
  }

  public void increase(Object key)
  {
    put(key, ++get(key));
  }
}[code][/color]

If you want to use other functions from the HashTable, you'll have to map them as well. The increase function is for you to figure out ;-)...
Use the values from your Vector as keys to fill the HashMap.

You can then iterate through the elements of the Vector and use the Strings you saved there before as keys for your dictionary map (the first HashMap used) and so replace the words. For efficiency reasons you can even perform the counting at the same time as you translate, using someting like the code below:

[COLOR=red][code]Vector translatedVector = new Vector();
String currentWord;
for (int i = 0; i < wordVector.size(); i++)
{
  currentWord = wordVector.get(i);
  translatedVector.add((String)translationMap.get(currentWord));
  countMap.increase(currentWord);
}

You'll get funny results with your translator though X-)

Hope this helps allow thyself to be the spark that lights the fire
 
Thanks for your suggestions. I'll try to implement it.

:)

duan
 
Tell me if you have problems - I did not test any of the code, so the error could be on my side...

Good luck! allow thyself to be the spark that lights the fire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top