LexiconLoader ¶
LexiconLoader(
cache_dir: str | Path | None = None,
hf_token: str | None = None,
revision: Literal["cached", "latest"] | str = "cached",
)
An interface for (down)loading pre-defined lexicons that map tokens to scores.
wl.lex is a ready-made loader with the default options, and is all that most uses need.
Construct a LexiconLoader to change where lexicons are cached, which HuggingFace token they
are downloaded with, or which version of each lexicon is loaded.
Lexicons are downloaded from HuggingFace and cached locally. Each lexicon is versioned separately, so updating one does not change the others.
Example
import wordlevel as wl
latest_loader = wl.LexiconLoader(revision="latest")
labmt = latest_loader.labMT("english")
# The commit the lexicon was loaded at, to load exactly the same version elsewhere
pinned_loader = wl.LexiconLoader(revision=labmt.revision)
pinned_labmt = pinned_loader.labMT("english")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str | Path | None
|
The directory that downloaded lexicons are cached in. Defaults to the user's cache directory, which is shared by every project on the machine. Pass a directory within a project to keep its lexicons separate from other projects. |
None
|
hf_token
|
str | None
|
A HuggingFace access token to authenticate downloads with. If not given, the token is
read from the |
None
|
revision
|
Literal['cached', 'latest'] | str
|
Which version of each lexicon to load. |
'cached'
|
Methods:
| Name | Description |
|---|---|
update_cached |
Updates every cached lexicon to its latest version on HuggingFace. |
Methods:¶
update_cached ¶
Updates every cached lexicon to its latest version on HuggingFace.
Only lexicons that have been updated on HuggingFace are downloaded again.
Raises:
| Type | Description |
|---|---|
LexiconConnectionError
|
If HuggingFace could not be reached |
LexiconDownloadError
|
If the lexicons could not be downloaded from HuggingFace |