コード例 #1
0
# "I just watched Alien vs. Cinderella...
#  a mind-blowing race against the clock in an absurdly spacious sewer!"

# The network is too large visualize as a whole
# (using only Pattern tools).
# But we can visualize sub-networks:
# http://www.clips.ua.ac.be/pages/pattern-graph#canvas

node = g["Lassie"]
halo = node.flatten(depth=2)

# The node "halo" is the node itself (depth 0),
# nodes connected to it (depth 1),
# nodes connected to those nodes (depth 2),
# and so on.

# Graph.copy() returns a new copy of a graph,
# optionally only including a subset of nodes.
g2 = g.copy(nodes=halo)

# Add some coloring to discern between movies and tropes.
# Each edge has movie (node1) <=> trope (node2).
for e in g2.edges:
    e.node1.stroke = (0, 0, 1, 1)  # R,G,B,A blue
    e.node2.stroke = (1, 0, 0, 1)  # R,B,B,A red

g2.export("lassie-halo")

# This should generate a "lassie-halo" folder,
# containing an "index.html". Open it in a browser...
コード例 #2
0
    print n.id

# To visualize the Darth Vader halo,
# we can use the canvas.js visualization engine bundled in Pattern.

# A Graph object has a Graph.export(folder_name) method that
# creates a new folder with HTML and canvas.js JavaScript code.
# http://www.clips.ua.ac.be/pages/pattern-graph#canvas

# A Graph object has a Graph.copy(nodes=[]) method that returns
# a new graph, containing only the nodes in the given list and
# the edges between them. 

# We can then combine halo(), Graph.copy() and Graph.export():

g.copy(nodes=halo(g["Darth Vader"])).export(pd("Darth_Graph"))

# Open the index.html in the generated folder in a browser!

# ------------------------------------------------------------------------------------

# The semantic field of a node roughly means: 
# every node that has the given node as its type.
# For example, the semantic field of "animal" is "bird", "fish", "rabbit", "albatross", ...
# Note that "albatross" is a more specific version of "bird",
# so we will need to use a spreading activation technique to find them all.

# The Node.flatten() method can also be called with a user-defined function.
# This function decides if an edge should be "followed" during the spreading
# activation. In this case, we only want to follow edges of type "is-a".