Comparison.score.lexicon
Comparison.score.lexicon ¶
lexicon(
lexicon: Lexicon
| Mapping[str, float]
| IntoFrame
| None = None,
lexicon_reference: Lexicon
| Mapping[str, float]
| IntoFrame
| None = None,
lexicon_comparison: Lexicon
| Mapping[str, float]
| IntoFrame
| None = None,
lexicon_token_col: str | None = None,
lexicon_score_col: str | None = None,
reference_score: float
| Literal["mean", "center"]
| Expr = 0,
scored_in_one_lexicon: float
| ScoredInOneLexicon
| Expr = "borrow",
) -> Comparison
Calculates the difference in lexicon-weighted averages between the reference and comparison corpora.
Example
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. Each corpus is summarized by its lexicon-weighted average \(\Phi = \sum_\tau p_\tau \phi_\tau\), and this method takes the difference between the weighted averages of the reference corpus \(R\) and the comparison corpus \(C\):
where \(p_\tau^{(C)}\) is the normalized frequency of a token \(\tau\) in \(C\) and \(\phi_\tau^{(C)}\) is the score the lexicon assigns it, and likewise for \(R\).
For how each token contributes to that difference, and how the reference_score shapes the
contributions, see more on
word shift graphs.
Warning
A lexicon dataframe is converted to Polars when this method is called. Polars dataframes are used as given, so a LazyFrame stays lazy. Dataframes from other lazy backends, like DuckDB and PySpark, are materialized in memory.
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] | IntoFrame | None
|
Lexicon associating tokens with scores. Accepts a mapping |
None
|
|
Lexicon | Mapping[str, float] | IntoFrame | None
|
Lexicon associating tokens with scores for the reference corpus. Accepts the same
forms as |
None
|
|
Lexicon | Mapping[str, float] | IntoFrame | None
|
Lexicon associating tokens with scores for the comparison corpus. Accepts the same
forms as |
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 0. If
"center' and a WordLevel lexicon is provided, uses the center of that lexicon's
numerical scale. If |
0
|
|
float | ScoredInOneLexicon | Expr
|
How to handle a token that is scored by one of |
'borrow'
|
Returns:
| Type | Description |
|---|---|
Comparison
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no lexicon is supplied ( |
ValueError
|
If |
ValueError
|
If |