Graph Module
The core module containing the NarrativeGraph class and related data structures.
NarrativeGraph
Bases: BaseGraph
Full narrative graph with triplet extraction, relations, and co-occurrences.
NarrativeGraph extracts subject-predicate-object triplets from text documents and builds both a directed relation graph and an undirected co-occurrence graph.
Source code in narrativegraphs/graphs.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
predicates_
property
Predicates as a pandas DataFrame.
relations_
property
Relations as a pandas DataFrame.
triplets_
property
Triplets as a pandas DataFrame.
relation_graph_
property
The full relation graph as a directed NetworkX graph.
__init__(triplet_extractor=None, cooccurrence_extractor=None, entity_mapper=None, predicate_mapper=None, sqlite_db_path=None, on_existing_db='stop', n_cpu=1)
Initialize a NarrativeGraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triplet_extractor
|
TripletExtractor
|
Extractor for subject-predicate-object triplets. |
None
|
cooccurrence_extractor
|
CooccurrenceExtractor
|
Extractor for entity co-occurrences. |
None
|
entity_mapper
|
Mapper
|
Mapper for entity normalization. |
None
|
predicate_mapper
|
Mapper
|
Mapper for predicate normalization. |
None
|
sqlite_db_path
|
str
|
Path to SQLite database file. If None, uses in-memory DB. |
None
|
on_existing_db
|
Literal['stop', 'overwrite', 'reuse']
|
Behavior when database exists: - "stop": Raise error if DB contains data - "overwrite": Delete existing DB - "reuse": Use existing DB data |
'stop'
|
n_cpu
|
int
|
Number of CPUs for parallel processing (-1 for all). |
1
|
Source code in narrativegraphs/graphs.py
fit(docs, doc_ids=None, timestamps=None, categories=None)
Fit a narrative graph from documents. The docs can be accompanied by lists with the same length of IDs, timestamps and categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
list[str]
|
Required argument, a list of documents as strings. |
required |
doc_ids
|
list[int | str]
|
Optional list of document ids. Same length as docs. |
None
|
timestamps
|
list[datetime | date]
|
Optional list of document timestamps. Same length as docs. |
None
|
categories
|
list[str | list[str]] | dict[str, list[str | list[str]]] | list[dict[str, str | list[str]]]
|
Optional list of document categories. Supports single or multiple categories. A document can have a single or multiple labels per category. See further down for examples. |
None
|
Returns:
| Type | Description |
|---|---|
NarrativeGraph
|
A fitted NarrativeGraph instance. |
Source code in narrativegraphs/graphs.py
load(file_path)
classmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
path to a SQLite database to load a NarrativeGraph from. |
required |
Returns:
| Type | Description |
|---|---|
NarrativeGraph
|
A NarrativeGraph object |