-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
78 lines (62 loc) · 2.12 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#%%
import json
import numpy as np
#%%
# helper function to create hyperlinked text for html
def create_anchor(link_data):
"""Helper function to generate anchored html from link data.
Args:
link_data ([dict]): A row of data as dict
Returns:
[list]: Same data with hyperlink
"""
title = link_data["title"]
url = link_data["link"]
hyperlink = '<a href=' + url + ' target="_blank">' + title + '</a>'
other_data = [
link_data["final_score"],
link_data["author"],
link_data["views"],
link_data["likes"],
' '.join(link_data["keywords"])
]
res = [hyperlink] + other_data
return res
# To get score from the scored list (with anchored title).
# This is to enable sorting of the list containing data.
def get_score(row):
return row[1]
# To create html table from data and write to filename
def write_to_html(target_folder, filename, sorted_list):
table = "<table>\n"
# Create the table's column headers
header = ['Title', 'Score', 'Author', 'Views', 'Likes', 'Keywords']
table += " <tr>\n"
for column in header:
table += " <th>{0}</th>\n".format(column.strip())
table += " </tr>\n"
# Create the table's row data
for row in sorted_list:
table += " <tr>\n"
for column in row:
table += " <td>{0}</td>\n".format(column)
table += " </tr>\n"
table += "</table>"
with open(target_folder+filename, "w") as f:
f.writelines(table)
def get_author_counts_dict(path_to_author_counts_dict="author_counts.json"):
try:
with open(path_to_author_counts_dict, 'r') as fp:
author_counts_dict = json.load(fp)
return author_counts_dict
except Exception:
return {}
def normalize_dictionary(dictionary):
factor=1.0/sum(dictionary.values())
normalised_dictionary = {k: v*factor for k, v in dictionary.items()}
return normalised_dictionary
def get_quantile_of_frontier(frontier, quantile):
all_scores = np.array([tuple[0] for tuple in frontier])
quantile = np.quantile(all_scores,quantile)
return quantile
# %%