Here is the rest of the code in the file just in case you do not have it.
sim_weibos = {}
sim_list = [] # these are the values for the dict sim_weibos
weibo_id = [] # these are the keys for the dict sim_weibos
for i in range(len(final_weibos_new)):
sim = model.n_similarity(weibo_test, final_weibos_new[i])
sim_list.append(sim)
weibo_id.append(i)
st.write(8)
#print("")
#print("")
#print(weibo_id, sim_list)
#print("")
for i in weibo_id:
sim_weibos[i] = sim_list[i]
#print(sim_weibos)
#print("")
sim_weibos_sorted_ascending = dict(sorted(sim_weibos.items(), key=lambda item: item[1]))
sim_weibos_sorted_descending = dict(sorted(sim_weibos.items(), key=lambda item: item[1], reverse=True))
st.write(9)
#print(sim_weibos_sorted_ascending)
#print("\r")
##print(sim_weibos_sorted_descending) #Uncomment this if you want to see the similar posts before the weibo_test is removed
#print("What weibos are similar to weibo id " +str(weibo_input) +" :")
st.write("What weibos are similar to weibo id" +str(weibo_input) +" :")
#print(weibo_test)
st.write(weibo_test)
# remove the first key/value from the dict since it is comparing the weibo to itself
# since we sort by ascending order the first key will always be the same as the weibo we're looking to compare
# we use first_key when we dont know the key. Comment the line below if you know the key such as
# when you request input from the user
st.write(10)
#first_key = next(iter(sim_weibos_sorted_descending)) # gets first key in the dict Use this when we dont know the the key like if we hardcode the weibo to check
# If you request input from the user then we know the key/weibo id that we should remove from the
# comparison. If we know the key use the code line below for same key
same_key = weibo # use this when we request input from the user to select the weibo id
st.write(11)
#sim_weibos_sorted_descending.pop(first_key) # removes the first key when we dont know the key, like if we hardcode the key weibo1 for testing
sim_weibos_sorted_descending.pop(same_key)
print("\r")
print(sim_weibos_sorted_descending)
print("\r")
st.write(12)
print("Top 3 Most Similar Weibos to Weibo ID " +str(weibo_input) + ":")
st.write("Top 3 Most Similar Weibos to Weibo ID " +str(weibo_input) + ":")
# Get the keys from sim_weibos_sorted_descending to display the top 3 similar posts
sim_weibo_ids = list(sim_weibos_sorted_descending.keys()) # list of similar weibo ids compared to the weibo_test
print("\r")
for i in range(len(sim_weibo_ids[0:3])): # sim_weibo_ids[0:4] we only select the top 3 similar weibos
#print("Weibo ID",sim_weibo_ids[i],":",final_weibos_new[i])
st.write("Weibo ID",sim_weibo_ids[i],":",final_weibos_new[i])
st.write(13)
# In[56]:
# WE DONT NEED THIS CODE. IT IS ONLY USED TO FIND BAD CHARACTERS AND VERIFY THAT THEY WERE REMOVED IN PREVIOUS STEPS
#print(final_weibos_new[110])
#print(final_weibos[110])
#word = '27'
#word = '""""'
#for i in range(len(final_weibos_new)):
# if word in final_weibos_new[i]:
# print(i,word)
#for i in range(len(final_weibos_new)):
# final_weibos_new[i] = list(filter(('""""').__ne__, final_weibos_new[i]))
#print(final_weibos[110])
#print(final_weibos_new[110])
# In[423]:
####################################################################################################
# Compute the cosine similarity between the first 10 posts and store in sim_matrix
####################################################################################################
temp_list = []
num_weibos = 10
sim_matrix = [0]*num_weibos
#print(sim_matrix)
for i in range(num_weibos):
for j in range(num_weibos):
sim_i_j = model.n_similarity(final_weibos_new[i], final_weibos_new[j])
temp_list.append(sim_i_j)
sim_matrix[i] = list(temp_list)
temp_list = []
#print(len(sim_matrix))
#print(sim_matrix)
st.table(sim_matrix)
#print(max(sim_matrix))
st.table(max(sim_matrix))
st.write(14)
# In[424]:
# Program to plot 2-D Heat map
# using seaborn.heatmap() method
import numpy as np
import seaborn as sns
import matplotlib.pylab as plt
import streamlit as st
fig = plt.figure(figsize = (15, 10))
print(type(sim_matrix))
print(len(sim_matrix))
data_set = sim_matrix
data_set = sim_matrix
ax = sns.heatmap( data_set , linewidth = 0.2 , cmap = 'coolwarm' )
plt.title( "2-D Heat Map" )
plt.show()
st.pyplot(fig)
st.write(15)
# In[426]:
sim_input = input("Enter a weibo id between 0 and " + str(num_weibos-1) + " to find the most similar weibo: ")
sim_input = int(sim_input)
remove_input = sim_matrix[sim_input].pop(sim_input) # remove the weibo you're searching from the row since it will always be the largest
#print(remove_input)
sim_row = sim_matrix[sim_input] # get the row of the weibo
val,idx = max((val, idx) for (idx, val) in enumerate(sim_row))
#print(val,idx)
st.write(val)
st.write(idx)
#most_sim = max(sim_matrix[sim_input])
#print(most_sim)
st.write(16)
# convert list of floats to string
#result = ", ".join([str(i) for i in sim_matrix[sim_input]])
#print(result)
#most_sim_idx = result.index(str(most_sim))
#print("The most similar weibo is " +str(most_sim)+ " at index", most_sim_idx)
#print(sorted(sim_matrix[sim_input].index, reverse=True))
#print(sorted(sim_matrix[sim_input], reverse=True))
Where you see st.write(7) is the last number I see when I run the code I have to input a number in the input I showed you above but when that input runs afterwards I get the st.experimental_rerun error.