SentimentModel

The pyFin-sentiment model class. Use this to download the model with SentimentModel.download(...) and instanciate it using SentimentModel(model_name).

Raises:

Type Description
ValueError

When you try to instanciate a model that does not exist.

FileNotFoundError

When your cache directory does not exist or is not writable.

__init__

__init__(model_name: str = 'small', cache_dir: str = None)

Initialize a pyFin-sentiment model by name. The model artifact needs to be downloaded first using SentimentModel.download(model_name).

Parameters:

Name Type Description Default
model_name str

Model name, one of: ["small"]. Valid names are also listed in SentimentModel.AVAILABLE_MODELS. Defaults to "small".

'small'
cache_dir str

Directory in which model artifacts are located. Defaults to ~/.cache/pyfin-sentiment.

None

Raises:

Type Description
ValueError

When you try to instanciate a model that does not exist.

Tip

If downloading or loading the model fails with the implicit cache_dir, try setting cache_dir to a writable directory that already exists.

download classmethod

download(model_name: str = 'small', cache_dir: str = None)

Download the model artifact into cache_dir.

Parameters:

Name Type Description Default
model_name str

Model name, one of: ["small"]. Valid names are also listed in SentimentModel.AVAILABLE_MODELS. Defaults to "small".

'small'
cache_dir str

Directory to which model artifacts are saved. Defaults to ~/.cache/pyfin-sentiment.

None

predict

predict(texts: Union[list, np.ndarray]) -> np.ndarray

Predict sentiment class from raw texts. No prior preprocessing is necessary as it will be done internally.

Parameters:

Name Type Description Default
texts Union[list, np.ndarray]

Input texts

required

Returns:

Type Description
np.ndarray

np.ndarray: Predicted sentiment class for each text. "1" = positive, "2" = neutral, "3" = negative.

predict_proba

predict_proba(texts: Union[list, np.ndarray]) -> np.ndarray

Predict sentiment class probabilites from raw texts. No prior preprocessing is necessary as it will be done internally.

Parameters:

Name Type Description Default
texts Union[list, np.ndarray]

Input texts

required

Returns:

Type Description
np.ndarray

np.ndarray: Predicted sentiment class probabilities p for each text. p[:, 0] = positive, p[:, 1] = neutral, p[:, 2] = negative.