#!/usr/bin/env python # coding: utf-8 # This notebooks aims to demonstrate some functionality of the python package . # In[1]: import time print(time.ctime()) get_ipython().run_line_magic('load_ext', 'nb_black') get_ipython().run_line_magic('load_ext', 'ipydex.displaytools') # In[2]: from IPython.display import SVG, display, HTML from semantictools import core as smt import nxv import importlib # for easier co-development of library and notebook importlib.reload(smt) # In[3]: len(smt.cache.wikidata_query_cache) # In[4]: # basic query smt.get_superclasses("Q1426191") # In[5]: base_node = smt.Node({"id": "Q125977", "label": "vector space"}) G = smt.build_graph(base_node, n=3) G.number_of_nodes() ##: # In[6]: style = nxv.Style( graph={"rankdir": "TB"}, node=lambda u, d: { "shape": "circle" if not u.is_top_level else "square", "fixedsize": True, "width": 1, "fontsize": 10, }, edge=lambda u, v, d: {"style": "solid", "arrowType": "normal", "label": "is a"}, ) svg_data = nxv.render(G, style, format="svg") # In[7]: get_ipython().run_cell_magic('javascript', '', 'IPython.OutputArea.auto_scroll_threshold = 9999;\n\n// necessary to prevent auto scrolling for a long image\n') # In[8]: entity_links = dict( [ ( node.id, '{0}'.format(node.id), ) for node in G.nodes.keys() ] ) # insert links to wiki data urls svg_data = svg_data.decode("utf8").format(**entity_links).encode("utf8") svg_fname = "vectorspace_superclasses_l3.svg" with open(svg_fname, "wb") as svgfile: svgfile.write(svg_data) display(SVG(svg_data)) # In[9]: display(HTML(f"{svg_fname}")) # In[10]: get_ipython().run_line_magic('time', 'G = smt.build_graph(base_node, n=13)') G.number_of_nodes() ##: # In[11]: svg_data = nxv.render(G, style, format="svg") # In[12]: entity_links = dict( [ ( node.id, '{0}'.format(node.id), ) for node in G.nodes.keys() ] ) # insert links to wiki data urls svg_data = svg_data.decode("utf8").format(**entity_links).encode("utf8") svg_fname = "vectorspace_superclasses_l13.svg" with open(svg_fname, "wb") as svgfile: svgfile.write(svg_data) display(SVG(svg_data)) # In[13]: display(HTML(f"{svg_fname}")) # Save the wikidata query results to prevent unnecessary requests # In[14]: smt.cache.save_wdq_cache() print(smt.cache.wdq_cache_path)