A node is an entity or vertex in a graph. Edges connect nodes to represent relationships, making graphs useful for modeling social ties, ideas, and conversation flow. section 6.4.6
Graphs distinguish nodes by the relationships connecting them:
- Undirected graphs connect nodes with reciprocal relationships, such as mutual friendship. section 6.4.6
- Directed graphs connect nodes with one-way relationships, such as following someone on Twitter. section 6.4.6
- In a chatbot conversation graph, a node represents the current conversation state, including relevant context and prior messages. section 12.3.2
Nodes can represent sentences rather than people or dialogue states. Sentence embeddings turn each sentence into a vector; highly similar sentences become connected nodes in an undirected graph, allowing related ideas to cluster visually. section 6.4.6
Building nodes from sentence similarity
A Python workflow loads manuscript text with spaCy, extracts sentence vectors, normalizes them, computes an affinity matrix, and adds nodes and weighted edges when similarity exceeds 0.95. NetworkX and a spring layout then display the resulting concept network. section 6.4.6
Node as a tutoring state
A counting chatbot can use nodes such as welcome, question, correct answer, and wrong answer. Each state sends messages and routes user input through triggers to the next node. section 12.3.2
Listing 6.19 Creating the undirected graph
>>> import re >>> import networkx as nx >>> similarity_matrix = np.triu(similarity_matrix, k=1) #1 >>> iterator = np.nditer(similarity_matrix, ... flags=['multi_index'], order='C') >>> node_labels = dict() >>> G = nx.Graph() >>> pattern = re.compile( ... r'[\w\s]*[\'\"]?[\w\s]+\-?[\w\s]*[\'\"]?[\w\s]*' ... ) #2 >>> for edge in iterator: ... key = 0 ... value = '' ... if edge > 0.95: #3 ... key = iterator.multi_index[0] ... value = str(noun_phrases[iterator.multi_index[0]]) ... if (pattern.fullmatch(value) ... and (value.lower().rstrip() != 'figure')): ... node_labels[key] = value ... G.add_node(iterator.multi_index[0]) ... G.add_edge(iterator.multi_index[0], ... iterator.multi_index[1], weight=edge)
Inspect executable NetworkX code that creates nodes and weighted edges from sentence-similarity scores. section 6.4.6


















