Mercurial > hg > NetworkVis
annotate ismi-python-neo4jrestclient/ismi-vis.py @ 29:2564732249b3
search for authors names uses normalization now.
author | casties |
---|---|
date | Thu, 10 Dec 2015 16:00:49 -0500 |
parents | 80010fb9dbce |
children | a6b2a09ea413 |
rev | line source |
---|---|
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
1 #!/usr/bin/env python |
29 | 2 from json import dumps, loads |
3 import urllib.request | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
4 |
16 | 5 from flask import Flask, Response, request, send_from_directory |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
6 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
7 from neo4jrestclient.client import GraphDatabase, Node, Relationship |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
8 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
9 app = Flask(__name__, static_url_path='/static/') |
13 | 10 app.debug = True |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
11 gdb = GraphDatabase("http://localhost:7474", username="neo4j", password="neo5j") |
29 | 12 ismi_db_baseurl = "http://localhost:18080/ismi-richfaces/" |
13 | |
14 def loadJSON(url): | |
15 #print("JSON loading %s"%url) | |
16 wsh=urllib.request.urlopen(url) | |
17 txt = wsh.read() | |
18 return loads(txt.decode("utf-8")) | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
19 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
20 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
21 @app.route("/") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
22 def get_index(): |
29 | 23 return app.send_static_file('commentaries_authors2.html') |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
24 |
9 | 25 @app.route("/commentaries.html") |
26 def get_commentaries(): | |
27 return app.send_static_file('commentaries.html') | |
28 | |
13 | 29 @app.route("/commentaries_authors.html") |
30 def get_commentaries_authors(): | |
23 | 31 return app.send_static_file('commentaries_authors2.html') |
16 | 32 |
33 @app.route('/static/<filename>') | |
34 def get_file(filename): | |
35 return send_from_directory('static', filename) | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
36 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
37 @app.route("/graph") |
16 | 38 def get_graph_commentaries(): |
39 query = ("match (t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)" | |
40 " return t1,t2" | |
41 " limit {limit}") | |
42 results = gdb.query(query, returns=(Node,Node), | |
43 params={"limit": int(request.args.get("limit", 100))}) | |
44 | |
45 n4j_nodes = {} | |
46 node_idx = {} | |
47 nodes = [] | |
48 rels = [] | |
49 i = 0 | |
50 for text1, text2 in results: | |
51 # source text | |
52 id1 = text1['ismi_id'] | |
53 if id1 not in n4j_nodes: | |
54 n4j_nodes[id1] = text1 | |
55 nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT", "ismi_id": id1}) | |
56 node_idx[id1] = i | |
57 source = i | |
58 i += 1 | |
59 | |
60 else: | |
61 source = node_idx[id1] | |
62 | |
63 # target text | |
64 id2 = text2['ismi_id'] | |
65 if id2 not in n4j_nodes: | |
66 n4j_nodes[id2] = text2 | |
67 nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT", "ismi_id": id2}) | |
68 node_idx[id2] = i | |
69 target = i | |
70 i += 1 | |
71 | |
72 else: | |
73 target = node_idx[id2] | |
74 | |
75 # relation is_commentary_on | |
76 rels.append({"source": source, "target": target}) | |
77 | |
78 return Response(dumps({"nodes": nodes, "links": rels}), | |
79 mimetype="application/json") | |
80 | |
81 | |
82 @app.route("/graph_commentaries_authors") | |
83 def get_graph_commentaries_authors(): | |
13 | 84 query = ("match (a1:PERSON)<-[:was_created_by]-(t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)-[:was_created_by]->(a2:PERSON)" |
85 " return a1,t1,t2,a2" | |
86 " limit 100") | |
87 results = gdb.query(query, returns=(Node,Node,Node,Node)) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
88 n4j_nodes = {} |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
89 node_idx = {} |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
90 nodes = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
91 rels = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
92 i = 0 |
13 | 93 for author1, text1, text2, author2 in results: |
94 # source text | |
95 id1 = text1['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
96 if id1 not in n4j_nodes: |
13 | 97 n4j_nodes[id1] = text1 |
98 nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
99 node_idx[id1] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
100 source = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
101 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
102 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
103 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
104 source = node_idx[id1] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
105 |
13 | 106 # target text |
107 id2 = text2['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
108 if id2 not in n4j_nodes: |
13 | 109 n4j_nodes[id2] = text2 |
110 nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
111 node_idx[id2] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
112 target = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
113 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
114 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
115 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
116 target = node_idx[id2] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
117 |
13 | 118 # relation is_commentary_on |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
119 rels.append({"source": source, "target": target}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
120 |
13 | 121 # source author |
122 id3 = author1['ismi_id'] | |
123 if id3 not in n4j_nodes: | |
124 n4j_nodes[id3] = author1 | |
125 nodes.append({"title": "%s [%s]"%(author1['label'],id3), "label": "PERSON"}) | |
126 node_idx[id3] = i | |
127 s_author = i | |
128 i += 1 | |
129 | |
130 else: | |
131 s_author = node_idx[id3] | |
132 | |
133 # relation was_created_by | |
134 rels.append({"source": source, "target": s_author}) | |
135 | |
136 # target author | |
137 id4 = author1['ismi_id'] | |
138 if id4 not in n4j_nodes: | |
139 n4j_nodes[id4] = author2 | |
140 nodes.append({"title": "%s [%s]"%(author2['label'],id4), "label": "PERSON"}) | |
141 node_idx[id4] = i | |
142 t_author = i | |
143 i += 1 | |
144 | |
145 else: | |
146 t_author = node_idx[id4] | |
147 | |
148 # relation was_created_by | |
149 rels.append({"source": source, "target": t_author}) | |
150 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
151 return Response(dumps({"nodes": nodes, "links": rels}), |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
152 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
153 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
154 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
155 @app.route("/search") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
156 def get_search(): |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
157 try: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
158 q = request.args["q"] |
29 | 159 norm_js = loadJSON(ismi_db_baseurl + "jsonInterface?method=normalize_string&type=arabic_translit&text=%s"%q) |
160 nq = norm_js.get("normalized_text") | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
161 except KeyError: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
162 return [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
163 else: |
29 | 164 #query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON {ismi_id: {id}})" |
165 # " RETURN t,p,exists((t)-[:is_commentary_on]->()),exists(()-[:is_commentary_on]->(t))") | |
166 query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON)" | |
167 " WHERE p._n_label =~ {name}" | |
17 | 168 " RETURN t,p,exists((t)-[:is_commentary_on]->()),exists(()-[:is_commentary_on]->(t))") |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
169 results = gdb.query( |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
170 query, |
17 | 171 returns=(Node,Node,bool,bool), |
29 | 172 params={"name": ".*%s.*"%nq.lower()} |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
173 ) |
13 | 174 # {"name": "(?i).*" + q + ".*"} |
175 print("search for %s returned %s results"%(repr(q),len(results))) | |
17 | 176 data = [{"text": t.properties, |
177 "author": a.properties, | |
178 "is_commentary": is_com, | |
179 "has_commentaries": has_com} | |
180 for [t,a,is_com,has_com] in results] | |
181 return Response(dumps(data), | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
182 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
183 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
184 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
185 @app.route("/text/<text_id>") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
186 def get_text(text_id): |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
187 query = ("MATCH (text:TEXT {ismi_id:{text_id}}) " |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
188 " RETURN text" |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
189 " LIMIT 1") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
190 results = gdb.query(query, returns=Node, params={"text_id": int(text_id)}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
191 node = results[0][0] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
192 print("node:%s"%repr(node)) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
193 return Response(dumps({"title": node['label'], "attrs": node.properties}), |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
194 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
195 |
13 | 196 @app.route("/textandcommentaries/<text_id>") |
197 def get_textandcommentaries(text_id): | |
198 query = ("match (t:TEXT {ismi_id:{text_id}})" | |
14 | 199 " optional match (t)-[:was_created_by]->(a:PERSON)" |
200 " optional match (s:TEXT)<-[:is_commentary_on]-(t)" | |
201 " optional match (s)-[:was_created_by]->(sa:PERSON)" | |
202 " optional match (t)<-[:is_commentary_on]-(c:TEXT)" | |
203 " optional match (c)-[:was_created_by]->(ca:PERSON)" | |
21 | 204 " return t,a.label,a.ismi_id,s.label,s.ismi_id,sa.label,c.label,c.ismi_id,ca.label") |
13 | 205 print("query:%s"%query) |
21 | 206 results = gdb.query(query, returns=(Node,str,str,str,str,str,str,str,str), |
13 | 207 params={"text_id": int(text_id)}) |
208 | |
209 print("result:%s"%results) | |
210 text = None | |
14 | 211 author = None |
13 | 212 scs = {} |
213 cs = {} | |
21 | 214 for [t,a_label,a_id,s_label,s_id,sa_label,c_label,c_id,ca_label] in results: |
13 | 215 text = t |
14 | 216 author = a_label |
21 | 217 author_id = a_id |
13 | 218 if s_id is not None and s_id != "None": |
14 | 219 scs[int(s_id)] = {"title": s_label, "author":sa_label} |
13 | 220 |
221 if c_id is not None and c_id != "None": | |
14 | 222 cs[int(c_id)] = {"title": c_label, "author":ca_label} |
13 | 223 |
224 print("text:%s scs:%s cs:%s"%(text, scs, cs)) | |
225 return Response(dumps({"title": text['label'], "attrs": text.properties, | |
21 | 226 "author": {"label": author, "ismi_id": author_id}, |
13 | 227 "commenting": scs, "commentaries": cs}), |
228 mimetype="application/json") | |
229 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
230 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
231 if __name__ == '__main__': |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
232 app.run(port=8080) |