Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
GoncaloKLopes committed Dec 6, 2017
1 parent 23836d3 commit 5b7ecf3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
30 changes: 26 additions & 4 deletions ex2/2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
exec(open('../functions.py').read())
exec(open('priors.py').read())
exec(open('weights.py').read())
tt=0
weights1 = 0

def build_graph_matrix(sentences,weight_func,t):
if not callable(weight_func):
return 'Not functions!'
nsents = len(sentences)
weights = np.zeros([nsents,nsents])

cos_matrix = get_cosine_similarities_matrix(sentences)
#create weights
for i in range(len(sentences)):
for j in range(len(sentences)):
weights[i][j] = weight_func(i,j,sentences,cos_matrix,t)
return weights

def build_priors(sentences,graph,prior_func):
if not callable(prior_func):
Expand Down Expand Up @@ -58,9 +73,10 @@ def get_top_n(array,n):
return top

def build_summary(sentences,prior_func,weight_func,t):

weights = build_graph_matrix(sentences,weight_func,t)
# print(np.sum(weights))
priors = build_priors(sentences,weights,prior_func)
#print(priors)
ranks = rank(weights,priors,50,0.15)
top = get_top_n(ranks,5)
summary = ""
Expand All @@ -82,16 +98,22 @@ def build_summary(sentences,prior_func,weight_func,t):
source_texts = os.listdir(source_path)

for thresh in tvals:
tt=thresh
print('---',tt)
MAP = 0
for text_file in source_texts:
with open(source_path + text_file,'r',encoding='Latin-1') as file: #source_path + text_file
text = file.read()
sentences = text_to_sentences(text)
summary = build_summary(sentences,degree_centrality_prior,uniform_weight,thresh)
with open(sums_path+ 'Ext-' + text_file,'r',encoding='Latin-1') as summary_file: #sums_path+ 'Ext-' + text_file
summary = build_summary(sentences,uniform_prior,cos_sim_weight,thresh)
with open(source_path + text_file,'r',encoding='Latin-1') as summary_file: #sums_path+ 'Ext-' + text_file '../ex1/textsum.txt'
MAP += AP(summary,summary_file.read())
MAP /= len(source_texts)
#print(MAP)
MAP /= len(source_texts)
print(MAP)
results.append(MAP)
# if tt==0.05:
# break
print(results)
max = np.argmax(results)
print("Best summary with MAP =",results[max],' for threshold =',tvals[max])
6 changes: 6 additions & 0 deletions ex2/priors.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
def uniform_prior(sent_index,graph,sentences):
return 1/len(sentences)

#receives graph matrix for convenience...
def degree_centrality_prior(sent_index,graph,sentences):
links = graph[sent_index]
nonzero = np.nonzero(links)[0]
return len(nonzero)/len(links)
7 changes: 7 additions & 0 deletions ex2/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

exec(open('../functions.py').read())

#----------------------weights--------------------------------#
def is_edge(sent1_index,sent2_index,cosine_matrix,t):
return (cos_sim(sent1_index,sent2_index,cosine_matrix) >= t) and (sent1_index != sent2_index)

Expand All @@ -14,3 +15,9 @@ def uniform_weight(sent1_index,sent2_index,sentences,cosine_matrix,t):
else:
return 0

def cos_sim_weight(sent1_index,sent2_index,sentences,cosine_matrix,t):
if is_edge(sent1_index,sent2_index,cosine_matrix,t):
return cos_sim(sent1_index,sent2_index,cosine_matrix)
else:
return 0

23 changes: 4 additions & 19 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,30 @@ def get_cosine_similarities_matrix(sentences):

#receives cossim matrix for performance reasons
#receives indexes for performance reasons aswell...
#also doubles as a weight, ignore t and sentences
def cos_sim(sent1_index,sent2_index,cosine_matrix):
return cosine_matrix[sent1_index][sent2_index]

def degree_centrality(sent_index,sentences,t=0.2):
graph = build_graph_matrix(sentences,t)
graph = build_graph_uniform(sentences,t)
return degree_centrality_prior(sent_index,graph,sentences)



#----------------------weights--------------------------------#
def is_edge(sent1_index,sent2_index,cosine_matrix,t):
return (cos_sim(sent1_index,sent2_index,cosine_matrix) >= t) and (sent1_index != sent2_index)

def uniform_weight(sent1_index,sent2_index,sentences,cosine_matrix,t):
if is_edge(sent1_index,sent2_index,cosine_matrix,t):
return 1
else:
return 0

#---------------------priors---------------------------------#
#receives graph matrix for convenience...
def degree_centrality_prior(sent_index,graph,sentences):
links = graph[sent_index]
nonzero = np.nonzero(links)[0]
return len(nonzero)/len(links)


#--------------------------graph building stuff------------------------#
def build_graph_matrix(sentences,weight_func=uniform_weight,t=0.2):
def build_graph_uniform(sentences,t=0.2):
if not callable(weight_func):
return 'Not functions!'
nsents = len(sentences)
weights = np.zeros([nsents,nsents])

cos_matrix = get_cosine_similarities_matrix(sentences)
#create weights
for i in range(len(sentences)):
for j in range(len(sentences)):
weights[i][j] = weight_func(i,j,sentences,cos_matrix,t)
weights[i][j] = (cos_sim(sent1_index,sent2_index,cosine_matrix) >= t) and (sent1_index != sent2_index)
return weights


Expand Down

0 comments on commit 5b7ecf3

Please sign in to comment.