wire ¶
Parse, validate, and serialize GKG count and theme cells.
text_component ¶
Replace reserved separators and control characters with spaces.
Use this helper to prepare individual text values for field serialization. The replacements discard information, so retain the original text for NLP analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | str | Text to normalize before including it in a field component. | required |
separators | str | Characters to replace with spaces in addition to Unicode control characters (category | '' |
Returns:
| Type | Description |
|---|---|
str | The normalized text with leading and trailing whitespace removed and |
str | whitespace runs collapsed to single spaces. Empty or whitespace-only |
str | input returns an empty string. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
Source code in src/gdelt_regkg/wire.py
parse_counts_cell ¶
Parse a standard or enhanced GKG counts cell into string components.
Entries are separated by semicolons, with an optional trailing semicolon. Each entry has ten hash-separated components: label, quantity, object, location type, location name, country code, ADM1 code, latitude, longitude, and feature ID. Enhanced entries add a final quantity character offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell | str | Encoded counts cell, or an empty string for no entries. | required |
enhanced | bool | Whether to require the eleventh component containing the offset. | False |
Returns:
| Type | Description |
|---|---|
tuple[str, ...] | A tuple of component tuples in input order, preserving duplicates and |
... | component spelling. Quantities and offsets remain strings. An empty cell |
tuple[tuple[str, ...], ...] | returns an empty tuple. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If the cell contains tabs, carriage returns, or newlines; an entry has the wrong number of components; a label does not match |
Source code in src/gdelt_regkg/wire.py
parse_themes_cell ¶
Parse a standard or enhanced GKG themes cell into string components.
Entries are separated by semicolons, with an optional trailing semicolon. Standard entries contain only a theme label. Enhanced entries contain a label and a character offset separated by a comma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell | str | Encoded themes cell, or an empty string for no entries. | required |
enhanced | bool | Whether to require a comma-separated offset after each label. | False |
Returns:
| Type | Description |
|---|---|
tuple[str, ...] | A tuple of one-component or two-component tuples in input order, |
... | preserving duplicates and component spelling. Offsets remain strings. |
tuple[tuple[str, ...], ...] | An empty cell returns an empty tuple. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If the cell contains tabs, carriage returns, or newlines; an entry has the wrong number of components; a label does not match |
Source code in src/gdelt_regkg/wire.py
serialize_counts_cell ¶
Validate and serialize count entries into a GKG counts cell.
Components follow the order accepted by parse_counts_cell. Entry order, duplicates, and component spelling are preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entries | Iterable[Iterable[str]] | Count entries containing ten string components each, or eleven when | required |
enhanced | bool | Whether entries include a final quantity character offset. | False |
Returns:
| Type | Description |
|---|---|
str | A cell with hash-separated components and a semicolon after every entry. |
str | An empty iterable returns an empty string. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If a component contains a hash, semicolon, tab, carriage return, or newline; an entry has the wrong number of components; or a label, quantity, or enhanced offset fails counts-cell validation. |
Source code in src/gdelt_regkg/wire.py
serialize_themes_cell ¶
Validate and serialize theme entries into a GKG themes cell.
Entry order, duplicates, and component spelling are preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entries | Iterable[Iterable[str]] | Entries containing a single label string, or a label and an offset string when | required |
enhanced | bool | Whether to include a comma-separated character offset after each label. | False |
Returns:
| Type | Description |
|---|---|
str | A cell with a semicolon after every entry and comma-separated components |
str | for enhanced entries. An empty iterable returns an empty string. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If a component contains a reserved separator, tab, carriage return, or newline; an entry has the wrong number of components; or a label or enhanced offset fails themes-cell validation. |