Skip to content

Quickstart

Extract an individual field, then generate and write a complete Global Knowledge Graph (GKG) record. The library accepts article bodies as plain text. HTML retrieval and body extraction are the caller's responsibility.

Prerequisites

Use Python 3.12 or newer. For all examples on this page, install the English named entity recognition (NER) backend and small model, then build the standard resources:

python -m pip install "gdelt-regkg[ner]"
python -m spacy download en_core_web_sm
gdelt-regkg-cli build all --sources-dir downloads/resources --download-sources --output local-resources

Run Python from the directory containing local-resources/. Setup explicitly downloads external inputs. Extraction uses the prepared files. See Installation and setup for feature-specific dependencies and Build resources for sources and profiles.

Standalone fields

In this example Counts and Dates fields are generated. Analyzer processes data once, then the structured result can be serialized:

from gdelt_regkg.fields.counts import analyze_counts
from gdelt_regkg.fields.dates import analyze_dates

result = analyze_counts("12 people were killed.")
print(result.to_gkg())
print(result.to_gkg(enhanced=True))
print(analyze_dates("02/03/2025", numeric_order="dmy").to_gkg())
CRISISLEX_T03_DEAD#12#people#0######;KILL#12#people#0######;
CRISISLEX_T03_DEAD#12#people#0#######0;KILL#12#people#0#######0;
3#3#2#2025#0

Import additional analyzers from their corresponding gdelt_regkg.fields modules. The GKG field reference provides formats and examples for each column.

Generate a GKG record

from gdelt_regkg import RecordMetadata, Resources, generate_gkg, write_gkg
from gdelt_regkg.fields.ner import SpacyNER

text = "Flooding killed 12 people in London."
metadata = RecordMetadata(
    identifier="https://example.org/story",
    batch_time="20261001091500",
    sequence=1,
    sharing_image="",
    related_images=[],
    social_image_embeds=[],
    social_video_embeds=[],
    extras={},
)
resources = Resources(
    resources_dir="local-resources",
    recognizer=SpacyNER.from_model("en_core_web_sm"),
)
record = generate_gkg(text, metadata, resources, strict=True)
assert len(record.to_row()) == 27
assert record["V1COUNTS"] == record.counts
with open("articles.gkg.tsv", "w", encoding="utf-8", newline="") as stream:
    write_gkg([record], stream)

The output file contains one headerless, tab-separated row with 27 columns and a final newline. Counts receive geography from the shared NER and location analysis. The record supports typed attributes and mapping access through native column names.

Note

The empty media values and extras={} explicitly declare that these inputs are available and empty. Omitting them leaves those fields unavailable. strict=True rejects incomplete records.

Next steps

  • Generate records with publication metadata, overrides or precomputed NER.
  • Select resources or supply custom dictionaries.
  • Translation before extraction and preserve the analyzed body for enhanced offsets.
  • Results before interpreting reconstructed fields as native GKG values.