ner ¶
Reusable entity mentions and an optional spaCy backend for prepared text.
NameRules dataclass ¶
NameRules(name: str = 'identity', strip_person_titles: bool = False, strip_person_initials: bool = False, strip_person_suffixes: bool = False, organization_blocklist: frozenset[str] = frozenset(), person_aliases: Mapping[str, str] = dict(), organization_aliases: Mapping[str, str] = dict(), organization_weights: Mapping[str, float] = dict(), organization_intercept: float = 0.0, organization_threshold: float | None = None)
Named rules for output spelling, aliases, and organization filtering.
Rules affect serialized entity names while source spans retain their text.
from_json classmethod ¶
Load name-normalization rules from a UTF-8 JSON file or package resource.
Source code in src/gdelt_regkg/fields/ner.py
NERUnavailableError ¶
Bases: RuntimeError
The requested optional NER dependency or trained model is unavailable.
EntityMention dataclass ¶
A recognized entity with source text and character offsets.
Attributes:
| Name | Type | Description |
|---|---|---|
text | str | Exact source span. |
label | str | Normalized |
start | int | Zero-based start offset. |
end | int | Exclusive end offset. |
source_label | str | None | Original recognizer label, when available. |
NameMention dataclass ¶
A broad named span with source text, start, exclusive end, and source label.
Names can include events, laws, and products alongside entity categories.
NERResult dataclass ¶
NERResult(text: str, mentions: tuple[EntityMention, ...], backend_name: str = 'supplied-mentions', name_policy: str = 'surface', name_rules: NameRules | None = None, name_mentions: tuple[NameMention, ...] | None = None)
Validated entity and name spans for an unchanged input body.
Attributes:
| Name | Type | Description |
|---|---|---|
text | str | Analyzed body. |
mentions | tuple[EntityMention, ...] | Entities sorted by source position. |
backend_name | str | Recognizer identity. |
name_policy | str | Output spelling policy: |
name_rules | NameRules | None | Optional output normalization and filtering rules. |
name_mentions | tuple[NameMention, ...] | None | Broad name spans; defaults to the supplied entity spans. |
organizations property ¶
Organization mentions in source order.
locations property ¶
Place mentions awaiting geographic resolution.
to_gkg ¶
Serialize person or organization names using the configured name policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label | str |
| required |
enhanced | bool | Include offsets for every mention. Standard output keeps distinct names in first-match order. | False |
Returns:
| Type | Description |
|---|---|
str | Semicolon-separated entries, or an empty string for no output names. |
Raises:
| Type | Description |
|---|---|
ValueError | If |
NotImplementedError | If |
Source code in src/gdelt_regkg/fields/ner.py
EntityRecognizer ¶
Bases: Protocol
Interface for backends implementing analyze(text) with an NERResult.
SpacyNER ¶
SpacyNER(nlp, *, name: str | None = None, filter_spans: bool = True, name_policy: str = 'surface', name_rules: NameRules | None = None)
Reusable entity recognizer backed by a loaded spaCy pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nlp | Language | Loaded pipeline containing | required |
name | str | None | Optional backend identity. | None |
filter_spans | bool | Apply built-in span quality filters. | True |
name_policy | str |
| 'surface' |
name_rules | NameRules | None | Optional output normalization and filtering rules. | None |
Source code in src/gdelt_regkg/fields/ner.py
from_model classmethod ¶
from_model(model: str = 'en_core_web_sm', *, name_policy: str = 'gdelt-full-names', name_rules: NameRules | None = None, resources_dir: StrPath | None = None) -> SpacyNER
Load an installed spaCy model for entity and broad-name extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | str | Installed model name or path. | 'en_core_web_sm' |
name_policy | str | Output spelling policy. Defaults to | 'gdelt-full-names' |
name_rules | NameRules | None | Custom name rules; the default full-name policy loads configured rules when omitted. | None |
resources_dir | StrPath | None | Local root for default name rules. | None |
Returns:
| Type | Description |
|---|---|
SpacyNER | A reusable recognizer. |
Raises:
| Type | Description |
|---|---|
OptionalDependencyError | If spaCy is unavailable. |
NERUnavailableError | If the model cannot be loaded. |
ResourceUnavailableError | If required default name rules are unavailable. |
Source code in src/gdelt_regkg/fields/ner.py
analyze ¶
Analyze one body, preserving its source text and character offsets.
Source code in src/gdelt_regkg/fields/ner.py
analyze_many ¶
Analyze bodies in batches while preserving input order and offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts | Iterable[str] | Bodies to analyze. | required |
batch_size | int | Positive number of documents per model batch. | 32 |
Yields:
| Type | Description |
|---|---|
NERResult | One NER result per input body. |
Raises:
| Type | Description |
|---|---|
TypeError | If an input body is not a string. |
ValueError | If |
Source code in src/gdelt_regkg/fields/ner.py
organization_features cached ¶
Binary word, bigram, and bounded character features for name filtering.
Source code in src/gdelt_regkg/fields/ner.py
canonical_entity_name ¶
Approximate native name spelling without altering source spans.
Source code in src/gdelt_regkg/fields/ner.py
default_name_rules ¶
Load the fixed development-selected cell normalization and ORG filter.
default_recognizer cached ¶
Load the installed English model lazily; never download it automatically.
analyze_entities ¶
analyze_entities(text: str, *, recognizer: EntityRecognizer | None = None, mentions: Iterable[EntityMention] | None = None) -> NERResult
Extract entities or validate supplied mentions against an input body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Prepared body. | required |
recognizer | EntityRecognizer | None | Custom backend. Defaults to the installed English spaCy model. | None |
mentions | Iterable[EntityMention] | None | Existing entity spans; supply these instead of | None |
Returns:
| Type | Description |
|---|---|
NERResult | Validated entity and broad-name spans for the unchanged body. |
Raises:
| Type | Description |
|---|---|
TypeError | If the body or supplied mentions have invalid types. |
ValueError | If NER inputs conflict or spans do not match the body. |
NERUnavailableError | If the default model is unavailable. |
OptionalDependencyError | If the default recognizer's dependency is unavailable. |