Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

import json 

import logging 

import multiprocessing 

from pathlib import Path 

 

import flask 

 

import database 

import utils 

import workflow 

 

 

web, process = utils.init_app("topicsexplorer") 

 

@web.route("/") 

def index(): 

"""Home page. 

""" 

if process.is_alive(): 

process.terminate() 

utils.init_logging(logging.DEBUG) 

utils.init_db(web) 

return flask.render_template("index.html", 

help=True) 

 

@web.route("/help") 

def help(): 

"""Help page. 

""" 

return flask.render_template("help.html", 

go_back=True) 

 

@web.route("/modeling", methods=["POST"]) 

def modeling(): 

"""Modeling page. 

""" 

process = multiprocessing.Process(target=workflow.wrapper) 

process.start() 

return flask.render_template("modeling.html", 

help=True, 

abort=True) 

 

@web.route("/overview-topics") 

def overview_topics(): 

presence = list(utils.get_topic_presence()) 

return flask.render_template("overview-topics.html", 

current="topics", 

help=True, 

reset=True, 

topics=True, 

documents=True, 

document_topic_distributions=True, 

export_data=True, 

presence=presence) 

 

@web.route("/overview-documents") 

def overview_documents(): 

titles = sorted(database.select("titles")) 

return flask.render_template("overview-documents.html", 

current="documents", 

help=True, 

reset=True, 

topics=True, 

documents=True, 

document_topic_distributions=True, 

export_data=True, 

titles=titles) 

 

@web.route("/document-topic-distribution") 

def document_topic_distributions(): 

return flask.render_template("document-topic-distribution.html", 

current="document-topic-distributions", 

help=True, 

reset=True, 

topics=True, 

documents=True, 

document_topic_distributions=True, 

export_data=True) 

 

@web.route("/topics/<topic>") 

def topics(topic): 

document_topic, topics, topic_similarites = database.select("topic-overview") 

# Get related documents: 

related_docs = document_topic[topic].sort_values(ascending=False)[:30] 

related_docs = list(related_docs.index) 

 

# Get related words: 

loc = document_topic.columns.get_loc(topic) 

related_words = topics[loc][:20] 

 

# Get similar topics: 

similar_topics = topic_similarites[topic].sort_values(ascending=False)[1:4] 

similar_topics = list(similar_topics.index) 

return flask.render_template("detail-topic.html", 

current="topics", 

help=True, 

reset=True, 

topics=True, 

documents=True, 

document_topic_distributions=True, 

export_data=True, 

topic=topic, 

similar_topics=similar_topics, 

related_words=related_words, 

related_documents=related_docs) 

 

@web.route("/documents/<title>") 

def documents(title): 

text, document_topic, topics, document_similarites = database.select("document-overview", title=title) 

 

# Get related topics: 

related_topics = document_topic[title].sort_values(ascending=False) * 100 

distribution = list(related_topics.to_dict().items()) 

related_topics = related_topics[:20].index 

 

# Get similar documents: 

similar_docs = document_similarites[title].sort_values(ascending=False)[1:4] 

similar_docs = list(similar_docs.index) 

 

text = text[:5000] + "..." 

text = text.split("\n\n") 

return flask.render_template("detail-document.html", 

current="documents", 

help=True, 

reset=True, 

topics=True, 

documents=True, 

document_topic_distributions=True, 

export_data=True, 

title=title, 

text=text, 

distribution=distribution, 

similar_documents=similar_docs, 

related_topics=related_topics) 

 

# API STUFF 

 

@web.route("/api/status") 

def status(): 

"""API: Current modeling status. 

""" 

return utils.get_status() 

 

@web.route("/api/document-topic") 

def document_topic_overview(): 

document_topic = database.select("document-topic") 

x = list() 

for key, value in document_topic.to_dict().items(): 

x.append({"name": key, "data": [{"x": title, "y": wert} for title, wert in value.items()]}) 

return json.dumps(x) 

 

 

 

@web.route("/export/<filename>") 

def export(filename): 

utils.export_data() 

path = Path(utils.TEMPDIR, filename) 

return flask.send_file(filename_or_fp=str(path)) 

 

@web.after_request 

def add_header(r): 

r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" 

r.headers["Pragma"] = "no-cache" 

r.headers["Expires"] = "0" 

r.headers["Cache-Control"] = "public, max-age=0" 

return r 

 

@web.teardown_appcontext 

def close_connection(exception): 

db = getattr(flask.g, "_database", None) 

if db is not None: 

db.close() 

 

 

if __name__ == "__main__": 

web.run(debug=True)