In: Computer Science
in this assignment you will create and use a database to find meanings and synonyms for given phrases. To do so you will use tables of synsets -- sets of one or more synonyms (specific phrases) that share the same meaning.
Your program should:
For example, for the phrase "jail"
there are 2 meanings and 11 unique synonyms
Meaning 1:
lock up or confine, in or as in a jail
Synset ID:
2494356
Synonyms:
gaol
Meaning 2:
A correctional institution used to detain persons who are in the lawful custody of the government (either accused persons awaiting trial or convicted persons serving a sentence)
Synset ID:
3592245
Synonyms:
clink
Only use the material covered in this module -- do not use more advanced functions not covered yet in the course
Submit a .py file!
import sqlite3
import pandas as pd
conn = sqlite3.connect('nimdvir.db')
c = conn.cursor()
c.execute('''CREATE TABLE synsets([SynsetID] INTEGER,[Definition] text)''')
c.execute('''CREATE TABLE phrases([SynsetID] INTEGER,[phrase] text)''')
conn.commit()
with open('/content/synsets.txt', 'r') as f:
for line in f:
data = line.split('\t')
c.execute('INSERT INTO synsets (SynsetID, Definition) VALUES (?, ?)', (data[0], data[1].strip()))
with open('/content/phrases.txt', 'r') as f:
for line in f:
data = line.split('\t')
c.execute('INSERT INTO phrases (SynsetID, phrase) VALUES (?, ?)', (data[0], data[1].strip()))
word = str(input("Enter the phrase: "))
query = 'SELECT * FROM phrases WHERE phrase=' + "'"+ word.lower() + "'"
df = pd.read_sql_query(query, conn)
if df.empty:
print("please try again!")
else:
result_query = 'SELECT DISTINCT s.SynsetID,Definition FROM phrases p INNER JOIN synsets s ON s.SynsetID=p.SynsetID WHERE phrase=' + "'"+ word.lower() + "'"
result_df = pd.read_sql_query(result_query, conn)
lst = result_df['SynsetID'].values.tolist()
query = 'SELECT DISTINCT phrase,SynsetID FROM phrases WHERE SynsetID IN (SELECT DISTINCT s.SynsetID FROM phrases p INNER JOIN synsets s ON s.SynsetID=p.SynsetID WHERE phrase=' + "'"+ word.lower() + "') AND phrase<>"+ "'"+ word.lower() + "'"
df = pd.read_sql_query(query, conn)
syn = df['phrase'].values.tolist()
print("There are "+ str(len(lst)) +" meanings and "+ str(len(syn)) +" unique synonyms")
j=1
for i in lst:
print("Meaning - ",j)
print(result_df[result_df['SynsetID']==i]['Definition'].values[0])
print("Synset ID: ")
print(i)
print("Synonyms:")
words = df[df['SynsetID']==i]['phrase'].values.tolist()
for w in words:
if w!=word.lower():
print(w)
j=j+1
ANy query you can ask in the comment. Thanks.