bertagent package

Subpackages

Submodules

bertagent.bertagent module

BERTAgent main module.

class bertagent.bertagent.BERTAgent(model_path=None, tokenizer_path=None, tokenizer_params={'add_special_tokens': True, 'max_length': 128, 'padding': 'max_length', 'return_attention_mask': True, 'truncation': True}, device='cuda', revision=None, factor=1.0, bias=0.0, log0=<Logger dummy (WARNING)>)[source]

Bases: object

Evaluates agency in a list of sentences.

Parameters
  • model_path (Union[str, pathlib.Path]) – path to huggingface repository or a local directory containing the fine-tuned model (e.g., BERTAgent)

  • tokenizer_path (Union[str, pathlib.Path]) – path to text tokenizer

  • tokenizer_params (Dict) – tokenizer parameters dictionary, see examples below (TOKENIZER_PARAMS)

  • device (Union[str, torch.device] = “cuda”) – torch device to use (default = “cuda”)

  • factor (float) – response scaling factor (default = 1)

  • bias (float = 0.0) – response shifting factor (default = 0)

  • log0 (logging.Logger) – optional logger to use

Examples

Process a list of sentences.

>>> # Imports
>>> import pathlib
>>> from bertagent import BERTAgent
>>>
>>> # Load BERTAgent
>>> ba0 = BERTAgent()
>>>
>>> sents = [
>>>     "stiving to achieve my goals",
>>>     "struglling to survive",
>>>     "hardly working individual",
>>>     "hard working individual",
>>> ]
>>> vals = ba0.predict(sents)
>>> for item in zip(sents, vals):
>>>     print(item)
#
# ('stiving to achieve my goals', 0.7477692365646362)
# ('struglling to survive', 0.043704114854335785)
# ('hardly working individual', -0.5707859396934509)
# ('hard working individual', 0.43518713116645813)
#
# NOTE: exact values may differ slightly from the above
# depending on the BERTAgent model used and version.

Process a texts in pandas dataframe.

>>> # Imports.
>>> import pathlib
>>> import pandas as pd
>>> from tqdm import tqdm
>>> from bertagent import BERTAgent
>>> from bertagent import EXAMPLE_SENTENCES as sents
>>> tqdm.pandas()
>>>
>>> # Load BERTAgent.
>>> ba0 = BERTAgent()
>>>
>>> # Prepare dataframe.
>>> df0 = pd.DataFrame(dict(text=sents))
>>>
>>> # Extract sentences from text.
>>> # NOTE: This is not an optimal method to get sentences from real data!
>>> df0["sents"] = df0.text.str.split(".")
>>>
>>> print(df0.head(n=4))
>>> # Evaluate agency
>>> model_id = "ba0"
>>> df0[model_id] = df0.sents.progress_apply(ba0.predict)
>>>
>>> df0["BATot"] = df0[model_id].apply(ba0.tot)
>>> df0["BAPos"] = df0[model_id].apply(ba0.pos)
>>> df0["BANeg"] = df0[model_id].apply(ba0.neg)
>>> df0["BAAbs"] = df0[model_id].apply(ba0.abs)
>>>
>>> cols0 = [
>>>     "sents",
>>>     "ba0",
>>>     "BATot",
>>>     "BAPos",
>>>     "BANeg",
>>>     "BAAbs",
>>> ]
>>>
>>> # Check example rows.
>>> df0[cols0].tail(n=8)
predict(sentences)[source]

Predict agency for a list of texts.

Parameters

sentences (List[str]) – a list of texts (e.g., sentences).

Return type

List[float]

Returns

  • List[float] – List of scores.

  • .. note:: – See doc for the BERTAgent class for usage examples.

classmethod tot(vals)[source]

Get the total score (mean) from a list of BERTAgent scores.

Parameters

vals (List[Union[int, float]]) – a list of scores.

Returns

Agency (total) score.

Return type

float

Note

See doc for the BERTAgent class for usage examples.

classmethod pos(vals)[source]

Get the agency-positive score from a list of BERTAgent scores.

This score is commuted as mean of all scores with negative values replaced by 0.

Parameters

vals (List[Union[int, float]]) – a list of scores.

Returns

Agency-positive score.

Return type

float

Note

See doc for the BERTAgent class for usage examples.

classmethod neg(vals)[source]

Get the agency-negative score from a list of BERTAgent scores.

This score is commuted as mean of all scores with positive values replaced by 0.

valsList[Union[int, float]]

a list of scores.

Returns

Agency-negative score.

Return type

float

Note

See doc for the BERTAgent class for usage examples.

classmethod abs(vals)[source]

Get the agency-absolute score from a list of BERTAgent scores.

This score is commuted as mean of absolute values of all scores.

valsList[Union[int, float]]

a list of scores.

Returns

Agency-absolute score.

Return type

float

Note

See doc for the BERTAgent class for usage examples.

Module contents

Top-level package for bertagent.

bertagent.get_module_version()[source]