Skip to content

names

Broad named-span extraction for the GKG all-names field.

NameResult dataclass

NameResult(text: str, mentions: tuple[NameMention, ...], backend_name: str)

Named spans for an input body, including the recognizer's identity.

Attributes:

Name Type Description
text str

Analyzed body.

mentions tuple[NameMention, ...]

Broad name spans with source offsets and labels.

backend_name str

Recognizer identity.

to_gkg

to_gkg()

Serialize names and offsets, or an empty string for no matches.

Source code in src/gdelt_regkg/fields/names.py
def to_gkg(self):
    """Serialize names and offsets, or an empty string for no matches."""
    return ";".join(
        f"{text_component(m.text, separators=';,')},{m.start}" for m in self.mentions
    )

analyze_names

analyze_names(text: str, *, recognizer: EntityRecognizer | None = None, ner: NERResult | None = None) -> NameResult

Collect broad named spans from a recognizer or precomputed NER result.

Parameters:

Name Type Description Default
text str

Prepared body.

required
recognizer EntityRecognizer | None

Entity recognizer. Defaults to the installed English model.

None
ner NERResult | None

Precomputed NER for this exact body; supply it instead of recognizer.

None

Returns:

Type Description
NameResult

Name spans and the recognizer identity, preserving source coordinates.

Raises:

Type Description
TypeError

If text is not a string.

ValueError

If both NER inputs are supplied or the result describes another body.

NERUnavailableError

If the default model is unavailable.

OptionalDependencyError

If the default recognizer's dependency is unavailable.

Source code in src/gdelt_regkg/fields/names.py
def analyze_names(
    text: str, *, recognizer: EntityRecognizer | None = None, ner: NERResult | None = None
) -> NameResult:
    """Collect broad named spans from a recognizer or precomputed NER result.

    Args:
        text: Prepared body.
        recognizer: Entity recognizer. Defaults to the installed English model.
        ner: Precomputed NER for this exact body; supply it instead of `recognizer`.

    Returns:
        Name spans and the recognizer identity, preserving source coordinates.

    Raises:
        TypeError: If `text` is not a string.
        ValueError: If both NER inputs are supplied or the result describes another body.
        NERUnavailableError: If the default model is unavailable.
        OptionalDependencyError: If the default recognizer's dependency is unavailable.
    """
    if not isinstance(text, str):
        raise TypeError("text must be str")
    if recognizer is not None and ner is not None:
        raise ValueError("Supply recognizer or ner, not both")
    ner = analyze_entities(text, recognizer=recognizer) if ner is None else ner
    if not isinstance(ner, NERResult) or ner.text != text:
        raise ValueError("NER result must refer to the unchanged body")
    return NameResult(text, ner.all_names, ner.backend_name)