0

I am trying to get the list of Synonyms and Samples given the wordid. After a lot of trial and error I can get the samples for all the synsets but not the actual synonyms. Here is my query which gives me the following results.

select senses.wordid, senses.synsetid, senses.sensekey, synsets.definition FROMsenses LEFT OUTER JOINsynsetsON senses.synsetid = synsets.synsetid where senses.wordid = 79459

enter image description here

I know you can get the synonyms by submiting the synsetid back to the senses table which gives you unique wordid and sensekey which you can then join with the words table. My problem is I can't seem to build that query.

I would like to get these columns if possible. If not synsetid, lemma and definition would do. The current database is mySql but I am hoping the answer would also be applicable to sqlite, since I am using this for an android APP.

wordid, lemma, senseid, synsetid, definition

schema:

CREATE TABLE `synsets` (
  `synsetid` int(10) unsigned NOT NULL DEFAULT '0',
  `pos` enum('n','v','a','r','s') NOT NULL,
  `definition` mediumtext,
  PRIMARY KEY (`synsetid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


CREATE TABLE `words` (
  `wordid` int(10) unsigned NOT NULL DEFAULT '0',
  `lemma` varchar(80) NOT NULL,
  `mantiq` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`wordid`),
  UNIQUE KEY `unq_words_lemma` (`lemma`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


CREATE TABLE `senses` (
  `wordid` int(10) unsigned NOT NULL DEFAULT '0',
  `synsetid` int(10) unsigned NOT NULL DEFAULT '0',
  `senseid` int(10) unsigned DEFAULT NULL,
  `sensekey` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`wordid`,`synsetid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `samples` (
  `synsetid` int(10) unsigned NOT NULL DEFAULT '0',
  `sampleid` smallint(5) unsigned NOT NULL DEFAULT '0',
  `sample` mediumtext NOT NULL,
  PRIMARY KEY (`synsetid`,`sampleid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Link to database: https://cloud.generatedesign.com/index.php/s/LA2G8ZvqNClqHFN

Yasin Yaqoobi
  • 1,888
  • 3
  • 27
  • 38
  • I know what a synonym is, but what is a "Sample"? – Uueerdo May 17 '16 at 18:49
  • a sample is a sentence i.e 'Dog" -> "Dog is men's best friend." Here is the link to the sample database. Thank you. https://cloud.generatedesign.com/index.php/s/Z152Je3JFaKEooI – Yasin Yaqoobi May 17 '16 at 18:51
  • Think my answer to a similar question [here](http://stackoverflow.com/questions/35601052/wordnet-query-to-return-example-sentences) may help. – Steve Chambers May 28 '16 at 10:42

2 Answers2

2

I'm not sure I exactly understand the question, but wouldn't something like this work?

SELECT s1.wordid, s1.synsetid, s1.sensekey, synsets.definition
   , s2.wordid AS matchedWordID, w.*  -- Additional info not from question's query
FROM senses AS s1
   LEFT JOIN synsets ON s1.synsetid = synsets.synsetid
   LEFT JOIN senses AS s2 ON s1.synsetid = s2.synsetid AND s1.wordid <> s2.wordid
   LEFT JOIN words AS w ON s2.wordid = w.wordid
WHERE s1.wordid = 79459
;

Note: ... is just short hand for the list of fields you actually want.

Note#2: You can of course JOIN to samples using the synsets reference, but keep in mind the results would be repeated for every word pair and sample; and it is possible some word pairs may be repeated if they are synonyms in multiple meanings.

Uueerdo
  • 15,723
  • 1
  • 16
  • 21
  • I understand. That is not a problem. Can you please write the full query. I am getting an error when I added field names. – Yasin Yaqoobi May 17 '16 at 19:02
  • @YasinYaqoobi I am guessing you used the table names in the field list, instead you need the aliases; since I am not sure exactly which final fields you will want I will update with the equivalents of your original selections. – Uueerdo May 17 '16 at 19:53
  • I am getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM senses AS s1 LEFT JOIN synsets ON s1.synsetid = synsets.synsetid LEFT' at line 2 – Yasin Yaqoobi May 26 '16 at 01:52
  • @YasinYaqoobi Are you trying to run this exact query, or your own version of it? Try taking out the selection field list (& comment) and just `SELECT *` to see if that fixes it; perhaps I misspelled one of the field names. MySQL tends to indicate errors at the first thing it doesn't understand, which means something in the selection list likely made it so it did not expect the FROM clause at that point. – Uueerdo May 26 '16 at 17:00
  • Yes, I am running the exact query and tried to debug the issue myself but couldn't find what's causing it. Also double checked the field names and they seem to be correct. I have added the link to the actual database file. Maybe you can run it locally ? https://cloud.generatedesign.com/index.php/s/LA2G8ZvqNClqHFN – Yasin Yaqoobi May 26 '16 at 17:08
  • @YasinYaqoobi runs fine in mine; I am running an older MySQL (5.5.40), but it should not affect it. However, I ran it through MySQL workbench, and I notice your db dump came from sequelpro. What are you using to run the query? – Uueerdo May 26 '16 at 17:19
  • Hmm that's very strange. I am running this in sequel pro. – Yasin Yaqoobi May 26 '16 at 17:49
  • I tried your query on the work computer and it runs perfectly. Not sure what's going on? Will this query work for sqlite ? Going to use this in an android app. Thank you for all the help. Will test again when I get home. – Yasin Yaqoobi May 26 '16 at 17:52
  • @YasinYaqoobi I am not very familiar with the capabilities of sqlite, but I would think so; I believe this is fairly typical sql, it's not using anything MySQL specific like `GROUP_CONCAT` or `USING` (for join conditions). – Uueerdo May 26 '16 at 18:03
  • Thank you. This worked great except it returns null when matchedWordID is the same as wordid, but i will take it. – Yasin Yaqoobi May 29 '16 at 15:18
  • @YasinYaqoobi Oh, I assumed you did not want the initial word in the results; if that is the case just remove ` AND s1.wordid <> s2.wordid` as that is what prevents it from being included... though it should only result in null values if the word shares no meanings with other words. – Uueerdo May 31 '16 at 16:02
  • Yea, I figured that out. Thank you. The query is pretty solid and allowed me to tweak it in various ways and works perfectly in sqlite android. – Yasin Yaqoobi May 31 '16 at 16:21
0

EDIT after schema publication

It appears that the senses table captures all relationships between each word and all its synsets and should be used in conjunction with inner join with words and synsets tables to to unravel all relationships

select sen.wordid,
         w.lemma,
         w.mantiq,
       sen.senseid,
       sen.synsetid,
       syn.definition,
from senses sen 
inner join words w on sen.wordid = w.wordid
inner join synsets syn on sen.synsetid = syn.synsetid
order by sen.wordid, sen.synsetid;

You do not need a LEFT JOIN since the fields you join upon do not appear to be nullable.

Spade
  • 2,220
  • 1
  • 19
  • 29
  • Thank you. I had to change from synsetid to syn.synsetid because it was throwing an error. Now I get all the results except the lemma is actually the same word repeated and not the synonyms. – Yasin Yaqoobi May 08 '16 at 23:38
  • Would you post the schema for all your tables if you need more help? – Spade May 09 '16 at 02:07
  • Schema added. Just remembered I would also like to included 'mantiq' in the results as well. Lemma is the word or it can be a synonym since a synonym is just a word. Mantiq = meaning. – Yasin Yaqoobi May 09 '16 at 02:36
  • @yasinyaqoobi : If this doesn't solve your problem, do provide sample data in tables as well as your expected results. – Spade May 09 '16 at 04:39
  • Thank you for all your help. I hope you still have time to look over this. A week later still can't figure it out. Here is a link where you can download the sample data. Thank you. https://cloud.generatedesign.com/index.php/s/Z152Je3JFaKEooI – Yasin Yaqoobi May 15 '16 at 02:55