Graphs
kenon.graphs.build_semantic_graph(embedder, corpus, similarity_threshold=0.4, k_neighbors=None, stopwords=None)
Build a semantic similarity graph from corpus-internal embeddings.
Nodes are vocabulary tokens. An edge (u, v) exists when the cosine
similarity between u and v exceeds similarity_threshold. Edge weight
is the cosine similarity value.
If k_neighbors is set, a k-NN graph is used to restrict connectivity
before applying the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedder
|
EmbedderProtocol
|
A fitted or unfitted embedder implementing |
required |
corpus
|
list[str]
|
List of document strings. Used both to fit the embedder and to determine the vocabulary. |
required |
similarity_threshold
|
float
|
Minimum cosine similarity for an edge. Must be in [0, 1]. |
0.4
|
k_neighbors
|
int | None
|
If not |
None
|
stopwords
|
frozenset[str] | None
|
Tokens to exclude from the graph nodes. |
None
|
Returns:
| Type | Description |
|---|---|
SemanticGraph
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Contract
- The graph is undirected.
- No self-loops (diagonal excluded).
- All edge weights are in [0, 1].
- Node labels are vocabulary token strings.
Example
from kenon.embeddings import TfidfEmbedder emb = TfidfEmbedder() corpus = ["cat mat sat", "dog ran fast", "cat ran fast"] * 5 g = build_semantic_graph(emb, corpus, similarity_threshold=0.1) isinstance(g.number_of_nodes(), int) True
Source code in kenon/graphs.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
kenon.graphs.cosine_similarity_matrix(embedder, corpus)
Return the full pairwise cosine similarity matrix for the vocabulary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedder
|
EmbedderProtocol
|
A fitted or unfitted embedder. Will be fitted on |
required |
corpus
|
list[str]
|
Corpus used to fit the embedder. |
required |
Returns:
| Type | Description |
|---|---|
Matrix
|
A tuple of |
list[Token]
|
|
tuple[Matrix, list[Token]]
|
token |
Contract
- Diagonal values are 1.0 (self-similarity).
- Matrix is symmetric.
- All values are in [-1, 1].
Example
from kenon.embeddings import TfidfEmbedder emb = TfidfEmbedder() corpus = ["cat mat", "dog ran"] * 3 sim, vocab = cosine_similarity_matrix(emb, corpus) sim.shape[0] == sim.shape[1] == len(vocab) True
Source code in kenon/graphs.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
kenon.graphs.save_graph(graph, path, fmt='graphml')
Persist a graph to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
SemanticGraph
|
The graph to save. |
required |
path
|
str | PathLike[str]
|
Destination file path. |
required |
fmt
|
str
|
Format string. Supported: |
'graphml'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Contract
- The file is written atomically (or as atomically as the format allows).
"graphml"and"gml"produce human-readable output.
Example
import tempfile, os, networkx as nx g = nx.Graph(); g.add_edge("a", "b", weight=0.5) with tempfile.NamedTemporaryFile(suffix=".graphml", delete=False) as f: ... save_graph(g, f.name) ... os.path.exists(f.name) True
Source code in kenon/graphs.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
kenon.graphs.load_graph(path, fmt='graphml')
Load a graph from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Source file path. |
required |
fmt
|
str
|
Format string. Must match the format used when saving. |
'graphml'
|
Returns:
| Type | Description |
|---|---|
SemanticGraph
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Contract
- Loaded graph preserves all node and edge attributes from the original.
"pickle"format is not safe for untrusted files.
Example
import tempfile, networkx as nx g = nx.Graph(); g.add_edge("x", "y", weight=0.9) with tempfile.NamedTemporaryFile(suffix=".graphml", delete=False) as f: ... save_graph(g, f.name) ... g2 = load_graph(f.name) g2["x"]["y"]["weight"] 0.9
Source code in kenon/graphs.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |