pyabsa.utils.pyabsa_utils

Module Contents

Functions

save_args(config, save_path)

Save arguments to a file.

print_args(config[, logger])

Print the arguments to the console.

validate_absa_example(text, aspect, polarity, config)

Validate input text, aspect, and polarity to ensure they meet certain criteria.

check_and_fix_labels(label_set, label_name, all_data, ...)

Check and fix the labels of the dataset.

check_and_fix_IOB_labels(label_map, config)

Check and fix IOB labels.

set_device(config, auto_device)

Sets the device to be used for the PyTorch model.

fprint(*objects[, sep, end, file, flush])

Custom print function that adds a timestamp and the pyabsa version before the printed message.

rprint(*objects[, sep, end, file, flush])

Custom print function that adds a timestamp, the pyabsa version, and a newline character before and after the printed message.

init_optimizer(optimizer)

Initialize the optimizer for the PyTorch model.

pyabsa.utils.pyabsa_utils.save_args(config, save_path)[source]

Save arguments to a file.

Args: - config: A Namespace object containing the arguments. - save_path: A string representing the path of the file to be saved.

Returns: None

pyabsa.utils.pyabsa_utils.print_args(config, logger=None)[source]

Print the arguments to the console.

Args: - config: A Namespace object containing the arguments. - logger: A logger object.

Returns: None

pyabsa.utils.pyabsa_utils.validate_absa_example(text: str, aspect: str, polarity: str, config)[source]

Validate input text, aspect, and polarity to ensure they meet certain criteria.

Parameters:
  • text (-) – The input text to validate.

  • aspect (-) – The input aspect to validate.

  • polarity (-) – The input polarity to validate.

  • config (-) – Configuration options.

Returns: - warning (bool): Flag indicating whether there are any warnings.

pyabsa.utils.pyabsa_utils.check_and_fix_labels(label_set: set, label_name, all_data, config)[source]

Check and fix the labels of the dataset.

Parameters:
  • label_set (set) – A set of unique labels in the dataset.

  • label_name (str) – Name of the label column in the dataset.

  • all_data (list) – List of dictionaries containing the dataset.

  • config (Config) – The config object.

Returns:

None.

pyabsa.utils.pyabsa_utils.check_and_fix_IOB_labels(label_map, config)[source]

Check and fix IOB labels.

Parameters:
  • label_map (dict) – A dictionary that maps IOB labels to their corresponding indices.

  • config (Config) – A configuration object.

Returns:

None

pyabsa.utils.pyabsa_utils.set_device(config, auto_device)[source]

Sets the device to be used for the PyTorch model.

Parameters:
  • config – An instance of ConfigManager class that holds the configuration for the model.

  • auto_device – Specifies the device to be used for the model. It can be either a string, a boolean, or None. If it is a string, it can be either “cuda”, “cuda:0”, “cuda:1”, or “cpu”. If it is a boolean and True, it automatically selects the available CUDA device. If it is None, it uses the autocuda.

Returns:

device: The device to be used for the PyTorch model. device_name: The name of the device.

pyabsa.utils.pyabsa_utils.fprint(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)[source]

Custom print function that adds a timestamp and the pyabsa version before the printed message.

Args:

*objects: Any number of objects to be printed sep (str, optional): Separator between objects. Defaults to “ “. end (str, optional): Ending character after all objects are printed. Defaults to “

“.

file (io.TextIOWrapper, optional): Text file to write printed output to. Defaults to sys.stdout. flush (bool, optional): Whether to flush output buffer after printing. Defaults to False.

pyabsa.utils.pyabsa_utils.rprint(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)[source]

Custom print function that adds a timestamp, the pyabsa version, and a newline character before and after the printed message.

Args:

*objects: Any number of objects to be printed sep (str, optional): Separator between objects. Defaults to “ “. end (str, optional): Ending character after all objects are printed. Defaults to “

“.

file (io.TextIOWrapper, optional): Text file to write printed output to. Defaults to sys.stdout. flush (bool, optional): Whether to flush output buffer after printing. Defaults to False.

pyabsa.utils.pyabsa_utils.init_optimizer(optimizer)[source]

Initialize the optimizer for the PyTorch model.

Parameters:

optimizer – str or PyTorch optimizer object.

Returns:

PyTorch optimizer object.

Raises:

KeyError – If the optimizer is unsupported.