<jupyter_start><jupyter_text>Create Synset Dataset<jupyter_code>from nltk.corpus import wordnet
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
import sys
sys.path.append(r'C:\Temp\SoundOfAI\rg_text_to_sound\tts_pipeline\src')
import ipdb
import pandas as pd
import numpy as np
from tqdm import tqdm
from match_word_to_words import prepare_dataset,word_to_wordpair_estimator,word_to_words_matcher,prepare_dataset
import matplotlib.pyplot as plt
cb = wordnet.synset('cookbook.n.01')

k=20 #maximum number of synonyms we might need
target_word_pairs = [('bright', 'dark'), ('full', 'hollow'),( 'smooth', 'rough'), ('warm', 'metallic'), ('clear', 'muddy'), ('thin', 'thick'), ('pure', 'noisy'), ('rich', 'sparse'), ('soft', 'hard')]
target_words = np.array(target_word_pairs).ravel().tolist()
wordnet.synset?
target_word = target_words[0]

def get_synonyms(target_word):
    syns = wordnet.synsets(target_word)
    mys = set()
    for syn in syns:
        #print(syn.name())
        mys |=set([m.name() for m in syn.lemmas()])
    return mys

def tolist(myset,length):
    l = list(myset)
    if len(l)<length:
        l+=['' for i in range(length-len(myset))]
        return l
    else:
        return l[:length]
    
    
df = pd.DataFrame([pd.Series(tolist(get_synonyms(targetword),k) ,name = targetword) for targetword in target_words]).T
df_sorted = df.where(~df.isin(df.columns),'').apply(lambda x:sorted(x,reverse=True))
df_sorted
df = pd.read_csv('text_to_qualities.csv')
dfnew = prepare_dataset(df)
dfnew[dfnew.description.str.match('\'')]
dfnew['description'] = dfnew.description.str.replace("'","")
dfnew['description']=dfnew.description.str.lower().str.replace('(\(not.*\))','',regex=True)
dfnew = dfnew[~dfnew.description.str.match('\(.*\)')]
df_score = dfnew.iloc[:,1:]
dfnew.head()
wordpairnames = df_score.columns.tolist()
wordpairnames_to_wordpair_dict = {s:t for s,t in zip(wordpairnames,target_word_pairs)}
df_sorted.to_excel('Training_dataset.xlsx',index=False)
target_word_pairs
df_sorted = pd.read_excel('Training_dataset.xlsx',index_col=None)
df_sorted

for ind,row in df_sorted.T.iterrows():
    target_word = row.name
    test_words = pd.Series([x for x in row if x!=''])
    print(target_word,':',','.join(test_words.dropna().tolist()))
lang_model = 'en_core_web_lg'

w2wpe = word_to_wordpair_estimator()
w2wpe.build(wordpairnames,target_word_pairs,lang_model=lang_model)

acc_dict = dict()
cm_dict = dict()
print('Mean accuracy over Synset words:')
for wordpairname,targetwordpair in zip(wordpairnames,target_word_pairs):
    leftwords = df_sorted[targetwordpair[0]]
    leftwords = leftwords[leftwords!=''].dropna()
    rightwords = df_sorted[targetwordpair[1]]
    rightwords = rightwords[rightwords!=''].dropna()
    
    leftpred = [w2wpe.match_word_to_wordpair(word,wordpairname,verbose=False)['closest word'] for word in leftwords]
    if len(leftpred)>0:
        bool_left = np.array(leftpred)==targetwordpair[0]
    else:
        bool_left = np.array([])
    rightpred = [w2wpe.match_word_to_wordpair(word,wordpairname,verbose=False)['closest word'] for word in rightwords]
    if len(rightpred)>0:
        bool_right = np.array(rightpred)==targetwordpair[1]
    else:
        bool_right = np.array([])
    correct_l = np.sum(bool_left)
    correct_r = np.sum(bool_right)
    y = np.hstack((bool_left,bool_right))    
    accuracy = np.mean(y)
    print(f'{wordpairname:<20}:{accuracy:5.2f} ({leftwords.size:2} "{targetwordpair[0]}" words, {rightwords.size:2} "{targetwordpair[1]}" words) ({correct_l}/{correct_r} correct) ')
    acc_dict[wordpairname]=accuracy
    cm = confusion_matrix(leftpred,np.array([targetwordpair[0] for i in range(len(leftpred))]),labels=targetwordpair)+confusion_matrix(rightpred,np.array([targetwordpair[1] for i in range(len(rightpred))]),labels=targetwordpair)
    cm_dict[wordpairname]=cm
accuracy = pd.Series(acc_dict)
accuracy
for key, val in cm_dict.items():
    print(key)
    print(val)<jupyter_output>bright_vs_dark
[[12  4]
 [ 0 15]]
full_vs_hollow
[[13  6]
 [ 0  2]]
smooth_vs_rough
[[ 6  0]
 [10 19]]
warm_vs_metallic
[[9 0]
 [0 1]]
clear_vs_muddy
[[20  5]
 [ 0 14]]
thin_vs_thick
[[16  8]
 [ 0 11]]
pure_vs_noisy
[[16  0]
 [ 2  0]]
rich_vs_sparse
[[6 0]
 [6 0]]
soft_vs_hard
[[11  0]
 [ 6 20]]
{'fraction_non_alphanumeric': 0.15527661552766156, 'fraction_numerical': 0.017201301720130173, 'mean_word_length': 5.393759286775632, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '330050', 'license': 'MIT', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1619, 'n_words': 302, 'path': 'playground/beat_toedtli/word2word_matching/Synset/Synset_Evaluation.ipynb', 'repo_name': 'archity/rg_text_to_sound', 'size': '4302'}