1

I have an object in my data model like so:

// A word in the English language.
public class Word {
    private Long id;                 // Ex: 35
    private String text;             // Ex: "catlike"
    private Integer length;          // Ex: 7
    private String definition;       // Ex: "resembling a cat; stealthy"
    private List<Word> synonyms;     // Ex: "quick", "nimble"
    private List<Word> antonyms;     // Ex: "sluggish", "slow"

    // Getters, setters, ctors, etc. omitted for brevity; this is a simple POJO/bean
}

As you can see, it contains 2 "recursive" lists of other words (synonyms and antonyms). A Word has a many-to-many relationship with both it's synonyms and it's antonyms. For instance: a synonym for "cat-like" might be "quick", but "quick" is also a synonym for "fast", etc.

My plan is to model Words in the database with the use of 3 different tables: 1 main words table, and 2 crosswalk/join tables:

  • words
  • synonyms (crosswalk)
  • antonyms (crosswalk)

The tables and their fields:

[words]
    id            PRIMARY KEY AUTO INCREMENT
    text          VARCHAR
    length        INTEGER
    definition    VARCHAR

[synonyms]
    id            PRIMARY KEY AUTO INCREMENT
    word_1_id     FOREIGN KEY (words)
    word_2_id     FOREIGN KEY (words)

[antonyms]
    id            PRIMARY KEY AUTO INCREMENT
    word_1_id     FOREIGN KEY (words)
    word_2_id     FOREIGN KEY (words)

So my thinking here is, if I wanted to query for any word that had "metal" in its description, or has any synonyms that have "metal" in them, my query might look like this:

SELECT
    *
FROM
    words w
INNER JOIN
    synonyms s
ON
    w.id = s.word_1_id
WHERE
    w.definition LIKE '%metal%'
    OR
    ??? (synonym of w).definition LIKE '%metal%'

Am I modelling this recrusive relationship correctly?!? I feel like I'm either dead-on, or completely off-base and overlooking something glaringly obvious. Thanks in advance!

7
  • 1
    Why is this recursive? Are you looking for words that have other words in their synonym chain deeper than one? For example, are you looking for your query to return words such that one of the synonyms of their synonym synonyms has 'metal' in it, or should only the immediate synonyms be examined? Oct 12, 2013 at 11:16
  • Thanks @dasblinkenlight (+1) - I might be using recursive incorrectly here (in which case, please correct me!). But the important thing here is that I'm tryin to accomplish 2 things: (1) I want tables that correctly mode my Java POJO (where every Word can have 0+ Word synonyms and antonyms), and (2) I can query for any word that has "metal" in its definition, as well as any of its synonyms that also have "metal" in their descriptions. Thanks again!
    – user1768830
    Oct 12, 2013 at 11:28
  • 1
    You should realise that the synonyms and antonyms relations are not symmetric: there are situations where you cannot replace "quick" by "catlike" e.g in "quick service". Oct 12, 2013 at 13:37
  • Good point @wildplasser (+1) - please see my comment under dasblinkenlight's response...I have the same question for you!
    – user1768830
    Oct 12, 2013 at 14:19
  • There is also the additional issue of homonyms, which might spoil your party. Oct 12, 2013 at 14:23

2 Answers 2

0

I want tables that correctly mode my Java POJO (where every Word can have 0+ Word synonyms and antonyms)

Your DB model is fine, it should be able to support your model on the Java side. Note that synonyms and antonyms are directional, so you should either add records for both directions, or treat a single record as bidirectional in your join conditions.

I can query for any word that has "metal" in its definition, as well as any of its synonyms also have "metal" in their descriptions.

Your query does not need a join at the top, and can be completed by using an EXISTS operator, like this:

SELECT
    *
FROM words w
WHERE
    w.definition LIKE '%metal%'
    OR
    EXISTS (
        SELECT *
        FROM words ww
        JOIN synonyms ss ON ww.id = ss.word_1_id
        WHERE ww.definition LIKE '%metal%'
    )

Note that this query will give you the word that fits the condition, not its synonyms/antonyms. These should be queried separately to complete the data needed for your Java representation. In order to complete this part of the query you may need to build a recursive query that gives you all words related to a given one through a set of many-to-many tables. Take a look at this answer for an example.

To treat your many-to-many tables symmetrically, change

ww.id = ss.word_1_id

condition to this:

ww.id = ss.word_1_id OR ww.id = ss.word_2_id

This would model the meaning "word W has a synonym Q, or is a synonym of Q".

2
  • Thanks again @dasblinkenlight (+1) - I guess I do want to make sure my relationships are properly directional since synonyms and anotnyms are not always symmetric, like you say. As another user pointed out, it may be fine to use "quick" in place of "catlike", but you might not always be able use "catlike" in place of "quick". So I'll re-phrase: is my data model correct for handling this asymmetric relationship between synonyms/antonyms? If not, how should it change, and how would your suggested query above (using EXISTS) change? Thanks again!
    – user1768830
    Oct 12, 2013 at 14:19
  • @TicketMonster The query assumes that the relations are not symmetric, i.e. the presence of a record {word1_id, word2_id} implies that word2 is a synonym of word1, but it does not imply that word1 is also a synonym of word2. Your data model is fine for that. Oct 12, 2013 at 14:23
0
select * from (
SELECT
    *
FROM
    words w
WHERE
    w.definition LIKE '%metal%'
) UNION (
SELECT
    w1.*
FROM
    words w1,
    words w2,
    synonyms s
WHERE
    w1.id = s.word_1_id and
    w2.id = s.word_2_id and
    w2.definition LIKE '%metal%'
)

)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.