Thread: Pairs of words

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Norway
    Posts
    11

    Pairs of words

    Hello!

    I am trying to write a dictionary type program where I basically just save a bunch of pairs of words, e.g. Spanish -> English. I want to be able to do quick lookups both ways (from Spanish to English, or the other way around).

    The idea is that a word will pop up every x minutes, and I have to fill in the one that matches, in the other language.

    What sort of data structure do you recommend for this? I thought about making a linked list, since that's the only structure I am somewhat familiar with. Any feedback would be greatly appreciated, and I am sure it will give me some training if I am better off implementing something other than a linked list.

    (My program will never contain more than a few hundred words, so it's not like I really need that much speed).

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for quick look-up you may implement a hash table

    actually you will build 1 table for each source language.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Norway
    Posts
    11
    Will that allow me to do lookups both ways? E.g. both lookup("cat") and the associated lookup("gato")?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you have two hash tables, yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought if you knew how to work with hash tables, you wouldn't have asked, so something simpler could be in order.

    You could use a simple array of words:
    char words[number of Words[200][30];

    and file up one half of it with English words, say 0-99, and use 100-199 for the Spanish word in this fashion:

    words[0] in English is words[100] in Spanish
    words[1] in English is words[101] in Spanish
    ....
    words[99] in English is words[199] in Spanish

    You could leave all the words unsorted, or sort the English half, and match all the swaps you make in there, with the associated swaps in the Spanish half. That would allow a much more efficient binary search for the words.

    For 100 words it's not necessary, but for 300 words or more, it will gradually become very helpful to speed up the search.

    Nothing at all wrong with using a hash table, however. Just an option that's a bit simpler.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Adak View Post
    I thought if you knew how to work with hash tables, you wouldn't have asked, so something simpler could be in order.


    You could leave all the words unsorted, or sort the English half, and match all the swaps you make in there, with the associated swaps in the Spanish half.
    Better yet to have an unsorted array of pairs (or later - bigger groups of words) and sorted array of pointers to word group.
    So one pointer array could be sorted by English words, second by Spanish, ... third by any other language added later.
    Sorting of pointers will not involve moving of main data at all.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing key, value pairs
    By tim8 in forum C Programming
    Replies: 14
    Last Post: 03-27-2013, 03:03 PM
  2. stl for pairs
    By dpp in forum C++ Programming
    Replies: 14
    Last Post: 05-18-2009, 09:46 AM
  3. vector and pairs
    By dpp in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2009, 11:24 AM
  4. prime pairs from 3 - 10,000
    By david999 in forum C Programming
    Replies: 4
    Last Post: 11-09-2005, 12:47 AM
  5. pairs of vector..
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 12-20-2002, 02:09 AM