themes ¶
Literal theme matching with source-text character offsets.
ThemeRule dataclass ¶
ThemeRule(theme: str, phrase: str, requires_all: tuple[tuple[str, ...], ...] = (), exclude_any: tuple[str, ...] = ())
A theme label and literal phrase with document-wide context conditions.
Each requires_all group needs at least one matching phrase. Any matching exclude_any phrase suppresses the rule.
ThemeLexicon dataclass ¶
Reusable named collection of literal theme rules.
Matching uses casefolded token sequences, including punctuation, and ignores whitespace between tokens. Context conditions apply across the document.
from_mapping classmethod ¶
Build a lexicon from theme codes mapped to collections of phrases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
themes | Mapping[str, Iterable[str]] | Theme codes and their literal matching phrases. | required |
name | str | Source or version identifier. | 'custom' |
Returns:
| Type | Description |
|---|---|
ThemeLexicon | A compiled lexicon ready for |
Source code in src/gdelt_regkg/fields/themes.py
from_json classmethod ¶
Load source-neutral rules or a legacy theme-code mapping.
Source code in src/gdelt_regkg/fields/themes.py
ThemeMention dataclass ¶
A theme code and matched text with zero-based start and exclusive end offsets.
ThemeResult dataclass ¶
Theme matches in source order and the name of the lexicon used.
to_gkg ¶
Serialize standard or enhanced theme entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enhanced | bool | Include offsets for every mention. Standard output keeps distinct theme codes in first-match order. | False |
Returns:
| Type | Description |
|---|---|
str | Semicolon-terminated entries, or an empty string for no matches. |
Source code in src/gdelt_regkg/fields/themes.py
analyze_themes ¶
Match literal theme phrases while preserving source character offsets.
Repeated and overlapping matches are retained. Context conditions inspect the whole document; offsets identify each rule's matching phrase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Prepared body in the lexicon's language. | required |
lexicon | ThemeLexicon | None | Reusable theme rules. Defaults to the configured local profile. | None |
Returns:
| Type | Description |
|---|---|
ThemeResult | Theme mentions in source order and the lexicon identity. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ResourceUnavailableError | If the default local profile is unavailable. |