Catalog ¶
Catalog(
data: DataFrame
| LazyFrame
| IntoFrame
| Catalog
| CatalogData
| DictOfFreqs
| ListOfFreqs
| Dataset,
token_col: str | None = None,
corpora: Sequence[str] | None = None,
)
The workspace for corpora and making comparisons between them.
For specific construction functions, see the construction documentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | LazyFrame | IntoFrame | Catalog | CatalogData | DictOfFreqs | ListOfFreqs | Dataset
|
The corpora token-frequency data. Accepts a Polars DataFrame or LazyFrame, a dataframe
from any other backend supported by Narwhals,
|
required |
token_col
|
str | None
|
If the |
None
|
corpora
|
Sequence[str] | None
|
If the |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Methods:
| Name | Description |
|---|---|
from_catalog |
Creates a new catalog from an existing one. |
from_dataframe |
Creates a |
from_dataset |
Creates a |
from_dict_of_freqs |
Creates a |
from_list_of_freqs |
Creates a |
from_polars |
Creates a |
with_comparisons |
Returns a new catalog with comparisons added to it. |
Attributes:
| Name | Type | Description |
|---|---|---|
comparisons |
Mapping[str, ComparisonResult]
|
Comparisons made between corpora in the catalog, keyed by each comparison's alias. |
df |
DataFrame
|
Dataframe representing the frequencies of tokens in the corpora in the catalog. |
plot |
Plotter
|
Namespace for plotting methods. See available plots. |
Attributes¶
comparisons
property
¶
Comparisons made between corpora in the catalog, keyed by each comparison's alias.
df
property
¶
Dataframe representing the frequencies of tokens in the corpora in the catalog.
Methods:¶
from_catalog
classmethod
¶
Creates a new catalog from an existing one.
The new catalog copies the data of the original one without mutating it.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
Catalog
|
An existing |
required |
Returns:
| Type | Description |
|---|---|
Catalog
|
A new |
from_dataframe
classmethod
¶
from_dataframe(
df: IntoFrame,
token_col: str,
corpora: Collection[str],
normalize: bool = True,
) -> Catalog
Creates a Catalog of corpora from any dataframe supported by Narwhals.
The dataframe is converted to Polars before the catalog is constructed.
Warning
The catalog's engine is backed by Polars. Any dataframe it returns is a Polars dataframe, regardless of the backend that the data came from.
Polars dataframes are used as given, so a LazyFrame stays lazy. Dataframes from other lazy backends, like DuckDB and PySpark, are materialized in memory.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
IntoFrame
|
A dataframe containing corpus information, from any backend supported by Narwhals. Rows are tokens and columns are their frequencies in different corpora |
required |
token_col
|
str
|
The column that contains the tokens (words, subwords, n-grams, etc.) |
required |
corpora
|
Collection[str]
|
The columns that include the token frequencies for each corpus |
required |
normalize
|
bool
|
Whether to normalize the corpus frequencies for each corpus so that they are in the range \([0, 1]\) and sum to 1. The original corpus frequencies will be preserved |
True
|
Returns:
| Type | Description |
|---|---|
Catalog
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
KeyError
|
If |
KeyError
|
If any column in |
ValueError
|
If fewer than two columns are provided in |
from_dataset
classmethod
¶
Creates a Catalog from a WordLevel pre-made dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset
|
A WordLevel dataset |
required |
corpora
|
Collection[str] | None
|
The names of the corpora to include from the dataset |
None
|
Returns:
| Type | Description |
|---|---|
Catalog
|
A |
Raises:
| Type | Description |
|---|---|
KeyError
|
If any corpus in |
ValueError
|
If fewer than two |
from_dict_of_freqs
classmethod
¶
Creates a Catalog of corpora from named token-to-frequency mappings.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
DictOfFreqs
|
A mapping of corpus names to token-to-frequency mappings, one per corpus. Each
mapping is |
required |
Returns:
| Type | Description |
|---|---|
Catalog
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two corpora are provided in |
ValueError
|
If any corpus in |
from_list_of_freqs
classmethod
¶
Creates a Catalog of corpora from a sequence of token-to-frequency mappings, each
representing the frequencies of tokens in a particular corpus.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
ListOfFreqs
|
A sequence of token-to-frequency mappings, one per corpus. Each mapping is
|
required |
corpora
|
Sequence[str] | None
|
The names of the corpora, listed in the same order as |
None
|
Returns:
| Type | Description |
|---|---|
Catalog
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If fewer than two corpora are provided in |
ValueError
|
If any corpus in |
from_polars
classmethod
¶
from_polars(
df: DataFrame | LazyFrame,
token_col: str,
corpora: Collection[str],
normalize: bool = True,
) -> Catalog
Creates a Catalog of corpora from a Polars dataframe.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | LazyFrame
|
A Polars DataFrame or LazyFrame containing corpus information. Rows are tokens and columns are their frequencies in different corpora |
required |
token_col
|
str
|
The column that contains the tokens (words, subwords, n-grams, etc.) |
required |
corpora
|
Collection[str]
|
The columns that include the token frequencies for each corpus |
required |
normalize
|
bool
|
Whether to normalize the corpus frequencies for each corpus so that they are in the range \([0, 1]\) and sum to 1. The original corpus frequencies will be preserved |
True
|
Returns:
| Type | Description |
|---|---|
Catalog
|
A |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
KeyError
|
If any column in |
ValueError
|
If fewer than two columns are provided in |
with_comparisons ¶
Returns a new catalog with comparisons added to it.
New comparisons will overwrite existing comparisons with the same name.
Example
import wordlevel as wl
cl = wl.Catalog.from_dict_of_freqs(
{
"before": {"good": 12, "bad": 8, "happy": 6, "sad": 4},
"after": {"good": 5, "bad": 7, "happy": 3, "sad": 9},
}
)
cl = cl.with_comparisons(
wl.comp("before", "after").score.kld().alias("kld_comparison"),
wl.comp("before", "after").score.jsd().alias("jsd_comparison"),
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparisons
|
Comparison
|
The comparisons to add to the catalog |
()
|
Returns:
| Type | Description |
|---|---|
Catalog
|
The catalog with the comparisons added to it |