[c617763] | 1 | from django.shortcuts import render |
---|
| 2 | |
---|
| 3 | from django.conf import settings |
---|
| 4 | |
---|
| 5 | from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError, StreamingHttpResponse |
---|
| 6 | |
---|
| 7 | import json |
---|
| 8 | import colorlib |
---|
| 9 | import itertools |
---|
| 10 | from vsm.corpus import Corpus |
---|
| 11 | from vsm.model.ldacgsmulti import LdaCgsMulti as LCM |
---|
| 12 | from vsm.viewer.ldagibbsviewer import LDAGibbsViewer as LDAViewer |
---|
| 13 | from vsm.viewer.wrappers import doc_label_name |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | path = settings.PATH |
---|
| 18 | corpus_file = settings.CORPUS_FILE |
---|
| 19 | context_type = settings.CONTEXT_TYPE |
---|
| 20 | model_pattern = settings.MODEL_PATTERN |
---|
| 21 | topics = settings.TOPICS |
---|
| 22 | corpus_name = settings.CORPUS_NAME |
---|
| 23 | icons = settings.ICONS |
---|
| 24 | #path = 'logs/aux/{0}.log' |
---|
| 25 | |
---|
| 26 | corpus_link = settings.CORPUS_LINK |
---|
| 27 | #topic_range = settings.TOPIC_RANGE |
---|
| 28 | topics_range = [int(item) for item in settings.TOPICS.split(',')] |
---|
| 29 | doc_title_format = settings.DOC_TITTLE_FORMAT |
---|
| 30 | doc_url_format = settings.DOC_URL_FORMAT |
---|
| 31 | |
---|
| 32 | #global lda_m, lda_v |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | lda_c = Corpus.load(corpus_file) |
---|
| 36 | #lda_m = LCM.load(model_pattern.format(k)) |
---|
| 37 | #lda_v = LDAViewer(lda_c, lda_m) |
---|
| 38 | label = lambda x: x |
---|
| 39 | |
---|
| 40 | def dump_exception(): |
---|
| 41 | import sys,traceback |
---|
| 42 | exc_type, exc_value, exc_traceback = sys.exc_info() |
---|
| 43 | print "*** print_tb:" |
---|
| 44 | traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) |
---|
| 45 | print "*** print_exception:" |
---|
| 46 | traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) |
---|
| 47 | return HttpResponseServerError(str(exc_value)) |
---|
| 48 | |
---|
| 49 | # Create your views here. |
---|
| 50 | |
---|
| 51 | def doc_topic_csv(request, doc_id): |
---|
| 52 | #response.content_type = 'text/csv; charset=UTF8' |
---|
| 53 | |
---|
| 54 | data = lda_v.doc_topics(doc_id) |
---|
| 55 | |
---|
| 56 | output=StringIO() |
---|
| 57 | writer = csv.writer(output) |
---|
| 58 | writer.writerow(['topic','prob']) |
---|
| 59 | writer.writerows([(t, "%6f" % p) for t,p in data]) |
---|
| 60 | |
---|
| 61 | return HttpResponse(output.getvalue()) |
---|
| 62 | |
---|
| 63 | def doc_csv(request, doc_id, threshold=0.2): |
---|
| 64 | #response.content_type = 'text/csv; charset=UTF8' |
---|
| 65 | |
---|
| 66 | data = lda_v.sim_doc_doc(doc_id) |
---|
| 67 | |
---|
| 68 | output=StringIO() |
---|
| 69 | writer = csv.writer(output) |
---|
| 70 | writer.writerow(['doc','prob']) |
---|
| 71 | writer.writerows([(d, "%6f" % p) for d,p in data if p > threshold]) |
---|
| 72 | |
---|
| 73 | return HttpResponse(output.getvalue()) |
---|
| 74 | |
---|
| 75 | def topic_json(request,k_param,topic_no, N=40): |
---|
| 76 | global lda_v |
---|
| 77 | lda_m = LCM.load(model_pattern.format(k_param)) |
---|
| 78 | lda_v = LDAViewer(lda_c, lda_m) |
---|
| 79 | #response.content_type = 'application/json; charset=UTF8' |
---|
| 80 | try: |
---|
| 81 | N = int(request.query.n) |
---|
| 82 | except: |
---|
| 83 | pass |
---|
| 84 | |
---|
| 85 | if N > 0: |
---|
| 86 | data = lda_v.dist_top_doc([int(topic_no)])[:N] |
---|
| 87 | else: |
---|
| 88 | data = lda_v.dist_top_doc([int(topic_no)])[N:] |
---|
| 89 | data = reversed(data) |
---|
| 90 | |
---|
| 91 | docs = [doc for doc,prob in data] |
---|
| 92 | doc_topics_mat = lda_v.doc_topics(docs) |
---|
| 93 | |
---|
| 94 | js = [] |
---|
| 95 | for doc_prob, topics in zip(data, doc_topics_mat): |
---|
| 96 | doc, prob = doc_prob |
---|
| 97 | js.append({'doc' : doc, 'label': label(doc), 'prob' : 1-prob, |
---|
| 98 | 'topics' : dict([(str(t), p) for t,p in topics])}) |
---|
| 99 | |
---|
| 100 | return HttpResponse(json.dumps(js)) |
---|
| 101 | |
---|
| 102 | def doc_topics(request,doc_id, N=40): |
---|
| 103 | try: |
---|
| 104 | try: |
---|
| 105 | N = int(request.query.n) |
---|
| 106 | except: |
---|
| 107 | pass |
---|
| 108 | |
---|
| 109 | #response.content_type = 'application/json; charset=UTF8' |
---|
| 110 | |
---|
| 111 | if N > 0: |
---|
| 112 | data = lda_v.dist_doc_doc(doc_id)[:N] |
---|
| 113 | else: |
---|
| 114 | data = lda_v.dist_doc_doc(doc_id)[N:] |
---|
| 115 | data = reversed(data) |
---|
| 116 | |
---|
| 117 | docs = [doc for doc,prob in data] |
---|
| 118 | doc_topics_mat = lda_v.doc_topics(docs) |
---|
| 119 | |
---|
| 120 | js = [] |
---|
| 121 | for doc_prob, topics in zip(data, doc_topics_mat): |
---|
| 122 | doc, prob = doc_prob |
---|
| 123 | js.append({'doc' : doc, 'label': label(doc), 'prob' : 1-prob, |
---|
| 124 | 'topics' : dict([(str(t), p) for t,p in topics])}) |
---|
| 125 | |
---|
| 126 | return HttpResponse(json.dumps(js)) |
---|
| 127 | except: |
---|
| 128 | return dump_exception() |
---|
| 129 | |
---|
| 130 | def topics(request): |
---|
| 131 | try: |
---|
| 132 | #response.content_type = 'application/json; charset=UTF8' |
---|
| 133 | #response.set_header('Expires', _cache_date()) |
---|
| 134 | |
---|
| 135 | # populate entropy values |
---|
| 136 | data = lda_v.topic_oscillations() |
---|
| 137 | |
---|
| 138 | colors = [itertools.cycle(cs) for cs in zip(*colorlib.brew(3,n_cls=4))] |
---|
| 139 | factor = len(data) / len(colors) |
---|
| 140 | |
---|
| 141 | js = {} |
---|
| 142 | for rank,topic_H in enumerate(data): |
---|
| 143 | topic, H = topic_H |
---|
| 144 | js[str(topic)] = { |
---|
| 145 | "H" : H, |
---|
| 146 | "color" : colors[min(rank / factor, len(colors)-1)].next() |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | # populate word values |
---|
| 150 | data = lda_v.topics() |
---|
| 151 | for i,topic in enumerate(data): |
---|
| 152 | js[str(i)].update({'words' : dict([(w, p) for w,p in topic[:10]])}) |
---|
| 153 | |
---|
| 154 | return HttpResponse(json.dumps(js)) |
---|
| 155 | except: |
---|
| 156 | return dump_exception() |
---|
| 157 | |
---|
| 158 | def docs(request): |
---|
| 159 | #response.content_type = 'application/json; charset=UTF8' |
---|
| 160 | #response.set_header('Expires', _cache_date()) |
---|
| 161 | try: |
---|
| 162 | docs = lda_v.corpus.view_metadata(context_type)[doc_label_name(context_type)] |
---|
| 163 | js = list() |
---|
| 164 | for doc in docs: |
---|
| 165 | js.append({ |
---|
| 166 | 'id': doc, |
---|
| 167 | 'label' : label(doc) |
---|
| 168 | }) |
---|
| 169 | |
---|
| 170 | return HttpResponse(json.dumps(js)) |
---|
| 171 | except: |
---|
| 172 | return dump_exception() |
---|
| 173 | |
---|
| 174 | def index(request): |
---|
| 175 | global lda_m,lda_v |
---|
| 176 | lda_m = LCM.load(model_pattern.format(10)) |
---|
| 177 | lda_v = LDAViewer(lda_c, lda_m) |
---|
| 178 | template = 'index.html' |
---|
| 179 | return render(request,template, |
---|
| 180 | {'filename':None, |
---|
| 181 | 'corpus_name' : corpus_name, |
---|
| 182 | 'corpus_link' : corpus_link, |
---|
| 183 | 'context_type' : context_type, |
---|
| 184 | 'topics_range' : topics_range, |
---|
| 185 | 'doc_title_format' : doc_title_format, |
---|
| 186 | 'doc_url_format' : doc_url_format}) |
---|
| 187 | |
---|
| 188 | def visualize(request,k_param,filename=None,topic_no=None): |
---|
| 189 | global lda_m,lda_v |
---|
| 190 | lda_m = LCM.load(model_pattern.format(k_param)) |
---|
| 191 | lda_v = LDAViewer(lda_c, lda_m) |
---|
| 192 | template = 'index.html' |
---|
| 193 | return render(request,template, |
---|
| 194 | {'filename':filename, |
---|
| 195 | 'k_param':k_param, |
---|
| 196 | 'topic_no':topic_no, |
---|
| 197 | 'corpus_name' : corpus_name, |
---|
| 198 | 'corpus_link' : corpus_link, |
---|
| 199 | 'context_type' : context_type, |
---|
| 200 | 'topics_range' : topics_range, |
---|
| 201 | 'doc_title_format' : doc_title_format, |
---|
| 202 | 'doc_url_format' : doc_url_format}) |
---|
| 203 | |
---|
| 204 | |
---|