tone ¶
Dictionary-frequency scoring for the GKG tone field.
ToneLexicon dataclass ¶
ToneLexicon(name: str, positive: frozenset[str], negative: frozenset[str], activity: frozenset[str], self_group: frozenset[str])
Named positive, negative, activity, and self-group token collections.
Categories are normalized and frozen at construction. Empty categories and overlap between categories are supported.
from_json classmethod ¶
Load once outside row loops; JSON needs name and all four categories.
ToneResult dataclass ¶
ToneResult(tone: float, positive_score: float, negative_score: float, polarity: float, activity_reference_density: float, self_group_reference_density: float, word_count: int, lexicon_name: str)
Tone scores, token count, and the lexicon identity.
Attributes:
| Name | Type | Description |
|---|---|---|
tone | float | Positive score minus negative score. |
positive_score | float | Percentage of tokens matching positive words. |
negative_score | float | Percentage of tokens matching negative words. |
polarity | float | Percentage matching either positive or negative words. |
activity_reference_density | float | Percentage matching activity words. |
self_group_reference_density | float | Percentage matching self-group words. |
word_count | int | Total tokens, including repetitions. |
lexicon_name | str | Name of the dictionary used. |
to_gkg ¶
Serialize six scores and the word count in GKG tone field order.
Source code in src/gdelt_regkg/fields/tone.py
tokenize_tone ¶
Return casefolded word/number tokens, retaining internal apostrophes/hyphens.
This explicit local policy is not a verified GDELT tokenizer. Punctuation alone is not a token; stopwords and repeated words stay in the denominator. Input must already be article text, not HTML.
Source code in src/gdelt_regkg/fields/tone.py
benchmark_tone_lexicon ¶
Compatibility alias for the benchmark-refined default dictionary.
baseline_tone_lexicon ¶
Version 1, retained for reproducible dictionary builds and comparisons.
Source code in src/gdelt_regkg/fields/tone.py
analyze_tone ¶
Compute dictionary-based tone scores for article text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Plain article text in the lexicon's language. The default lexicon expects English text or an English translation. | required |
lexicon | ToneLexicon | None | Reusable tone dictionary. If omitted, load the configured default English dictionary from local resources. | None |
Returns:
| Type | Description |
|---|---|
ToneResult | The seven tone components and the name of the dictionary used. |
Raises:
| Type | Description |
|---|---|
TypeError | If text is not a string or lexicon is not a ToneLexicon. |
ResourceUnavailableError | If the default local dictionary is unavailable. |
Examples:
>>> lexicon = ToneLexicon("example", {"good"}, {"bad"}, set(), set())
>>> result = analyze_tone("good good bad", lexicon=lexicon)
>>> result.word_count
3
>>> round(result.tone, 2)
33.33