Skip to content

Instantly share code, notes, and snippets.

#Outlet classifier:
classes_All = np.asarray([1 for i in range(len(n_s_b))] + \
[2 for i in range(len(n_s_p))] + [3 for i in range(len(n_s_a))] + \
[4 for i in range(len(n_s_n))])
#Bias classifier:
classes_Bias = np.asarray([1 for i in range(len(n_s_b))] + \
[1 for i in range(len(n_s_p))] + [2 for i in range(len(n_s_a))] + \
[2 for i in range(len(n_s_n))])
#Density:
p = 0.4
df = pd.read_csv('articles2.csv',header=None,skiprows=lambda i: 1>0 and random.random() > p)
#First batch:
n_s_a = df[df[3] == 'Atlantic']
n_s_p = df[df[3] == 'New York Post']
#Second batch:
df = pd.read_csv('articles1.csv',header=None,skiprows=lambda i: 1>0 and random.random() > p)
n_s_b = df[df[3] == 'Breitbart']
n_s_n = df[df[3] == 'New York Times']
#This code is to find most similar quote from a given author:
l_all = list_all_rom + list_all_rea + list_all_sur
#merge all names and repeat names as many times as quotes per author:
names_mult = []
for i in l_all:
names_mult = names_mult + [eval(i)[0]] * int(len(eval(i))-1)
#Find most similar quote from author function:
#to plot quotes heatmap:
plt.figure(figsize=(18, 16))
sns.heatmap(data=sM_All,annot=False,linewidths=0.1, cmap="YlGnBu")
plt.show()
#to render graph:
#Import array to netowrkx object with a given threshold:
G = nx.from_numpy_array(sM_All>0.25)
#figure layout and size:
pos = nx.kamada_kawai_layout(G)
plt.figure(figsize=(4, 3))
sns.heatmap(confusion_matrix(class_test, class_pred), \
annot=True,linewidths=2, cmap="YlGnBu",fmt="g", \
xticklabels=['Romanticism','Realism','Surrealism'],\
yticklabels=['Romanticism','Realism','Surrealism'])
#This pipeline allow us to change the type of classifier:
def class_pipeline(features, class_ground, clf):
#Call Shuffle splitter:
ss = StratifiedShuffleSplit(n_splits=50)
#sm = EditedNearestNeighbours()
#[features, class_ground] = sm.fit_resample(features, class_ground)
#Alocate variables:
class_pred_all = []
#Function to compute all embeddings and distance between all possible quote pairs:
def similarity_matrix(merge_list):
#initialize distance array:
similarity_matrix = np.zeros([len(merge_list), len(merge_list)])
#initialize embeddings array:
emb_all = np.zeros([len(merge_list),512])
#Outer for loop
for i in range(0,len(merge_list)):
#Here is where we run the previously started session, so it is important to run previous step succesfully:
i_emb = session.run(embedded_text, feed_dict={text_input: [merge_list[i]]})
#This code is a cheap adaptation of example code from tf's hub website:
# More at: https://tfhub.dev/google/universal-sentence-encoder-multilingual/1
#Initialize tf graph:
g = tf.Graph()
with g.as_default():
text_input = tf.placeholder(dtype=tf.string, shape=[None])
#Here you can specify your path
embed = hub.Module('/path/to/model/')
#Or downlload it directly from tf's hub:
def merge_list(list_text):
l = len(list_text)
c = 1
merge = eval(list_text[0])[1:]
while c < l:
merge = merge + eval(list_text[c])[1:]
c += 1
return merge