Comparison.score.lexicon
Comparison.score.lexicon ¶
lexicon(
lexicon: Lexicon
| Mapping[str, float]
| DataFrame
| LazyFrame
| None = None,
lexicon_reference: Lexicon
| Mapping[str, float]
| DataFrame
| LazyFrame
| None = None,
lexicon_comparison: Lexicon
| Mapping[str, float]
| DataFrame
| LazyFrame
| None = None,
lexicon_token_col: str | None = None,
lexicon_score_col: str | None = None,
reference_score: float
| Literal["mean", "center"]
| Expr = 0,
impute: float
| Literal["borrow", "mean"]
| Expr = "borrow",
) -> Comparison
Calculates the difference in lexicon-weighted averages between the reference and comparison corpora.
The supplied lexicon associates a score with each token. The user can provide a single lexicon to use for both corpora, or one lexicon for each. The overall score for each corpus individually is the lexicon-weighted average. Specifically, if \(p_\tau\) is the normalized frequency of a token \(\tau\) in a corpus, and \(\phi_\tau\) is the score assigned to it by the lexicon, then the corpus-level score is \(\Phi = \sum_\tau p_\tau \phi_\tau\). This method takes the difference in weighted averages between the reference and comparison corpora, \(\Phi^{(C)} - \Phi^{(R)}\).
Example
When the reference_score is 0, the contribution \(\delta_\tau\) of a token \(\tau\) is:
where \(\phi_\tau^{(C)}\) is the score of token \(\tau\) in the comparison corpus, and \(p_\tau^{(C)}\) is its normalized frequency. Similarly, \(\phi_\tau^{(R)}\) and \(p_\tau^{(R)}\) are the score and normalized frequency of \(\tau\) in the reference corpus.
Each token is interpreted by whether it reinforces or offsets the corpus-level difference in weighted averages \(\Phi^{(C)} - \Phi^{(R)}\). A contribution with the same sign as the overall difference reinforces it, making its magnitude larger; a contribution with the opposite sign offsets it, pulling the difference back toward zero. For example, if \(\Phi^{(C)} - \Phi^{(R)} > 0\) (the comparison's weighted average is higher), positive contributions reinforce the difference and negative contributions offset it; the interpretation flips when \(\Phi^{(C)} - \Phi^{(R)} < 0\).
If the reference_score is non-zero, and the score of a word is the same in both corpora,
i.e. \(\phi_\tau = \phi^{(C)}_\tau = \phi^{(R)}_\tau\), then a token's contribution is:
where \(\Phi^{(\text{ref})}\) is the user-provided reference score. If the reference_score
is non-zero, and the score of a word is corpus dependent, then a token's contribution is:
Reference
For more details, see the following paper:
Gallagher, R. J., Frank, M. R., Mitchell, L., Schwartz, A. J., Reagan, A. J., Danforth, C. M., & Dodds, P. S. (2021). Generalized word shift graphs: a method for visualizing and explaining pairwise comparisons between texts. EPJ Data Science, 10(1), 4.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Lexicon | Mapping[str, float] | DataFrame | LazyFrame | None
|
Lexicon associating tokens with scores. Accepts a mapping |
None
|
|
Lexicon | Mapping[str, float] | DataFrame | LazyFrame | None
|
Lexicon associating tokens with scores for the reference corpus |
None
|
|
Lexicon | Mapping[str, float] | DataFrame | LazyFrame | None
|
Lexicon associating tokens with scores for the comparison corpus |
None
|
|
str | None
|
Name of the token column when a dataframe is supplied for the lexicon |
None
|
|
str | None
|
Name of the score column when a dataframe is supplied for the lexicon |
None
|
|
float | Literal['mean', 'center'] | Expr
|
Baseline against which per-token contributions are interpreted. Defaults to the
"center" of a lexicon's numerical scale when a |
0
|
|
float | Literal['borrow', 'mean'] | Expr
|
How to score tokens when a score exists in one lexicon for the reference or
comparison but not the other. |
'borrow'
|
Returns:
| Type | Description |
|---|---|
Comparison
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no lexicon is supplied ( |
ValueError
|
If |