pyabsa.tasks.AspectTermExtraction.prediction.aspect_extractor

Classes

AspectExtractor

Predictor for Aspect Term Extraction and (optional) Polarity Classification.

Predictor

Predictor for Aspect Term Extraction and (optional) Polarity Classification.

Module Contents

class pyabsa.tasks.AspectTermExtraction.prediction.aspect_extractor.AspectExtractor(checkpoint=None, **kwargs)

Bases: pyabsa.framework.prediction_class.predictor_template.InferenceModel

Predictor for Aspect Term Extraction and (optional) Polarity Classification.

Loads an ATEPC checkpoint and provides utilities to extract aspect terms from text, with optional sentiment classification for extracted aspects depending on the configured model/task. Supports single-text and batch inference and can auto-detect dataset files for inference.

task_code = 'ATEPC'
processor
num_labels
eval_dataloader = None
merge_result(sentence_res, results)

merge ate sentence result and apc results, and restore to original sentence order :param sentence_res: list of ate sentence results, which has (tokens, iobs) :type sentence_res: [tuple] :param results: list of apc results :type results: [dict]

Returns:

merged extraction/polarity results for each input example

Return type:

[dict]

extract_aspect(inference_source: List[pathlib.Path] | list | str, save_result=True, print_result=True, pred_sentiment=True, **kwargs)

Extract aspects and their corresponding polarities from a list of input files.

Parameters:
  • self – An instance of the model class.

  • inference_source – A list of file paths, or a directory containing files to be processed.

  • save_result (bool) – Whether to save the output to a file. Default is True.

  • print_result (bool) – Whether to print the output to the console. Default is True.

  • pred_sentiment (bool) – Whether to predict the sentiment of each aspect. Default is True.

  • **kwargs – Additional keyword arguments to be passed to the batch_predict method.

Returns:

The predicted aspects and their corresponding polarities.

predict(text: str | List[str], save_result=True, print_result=True, pred_sentiment=True, **kwargs)
Parameters:
  • text (str) – input example

  • save_result (bool) – whether to save the result to file

  • print_result (bool) – whether to print the result to console

  • pred_sentiment (bool) – whether to predict sentiment

batch_predict(target_file: List[pathlib.Path] | list | str, save_result=True, print_result=True, pred_sentiment=True, **kwargs)
Parameters:
  • target_file (list) – list of input examples or a list of files to be predicted

  • save_result (bool, optional) – save result to file. Defaults to True.

  • print_result (bool, optional) – print result to console. Defaults to True.

  • pred_sentiment (bool, optional) – predict sentiment. Defaults to True.

Returns:

_extract(examples)
_run_prediction(examples)

This method should be implemented in the subclass for running predictions using the trained model.

Parameters:

kwargs – additional keyword arguments

Returns:

predicted labels or other prediction outputs

class pyabsa.tasks.AspectTermExtraction.prediction.aspect_extractor.Predictor(checkpoint=None, **kwargs)

Bases: AspectExtractor

Predictor for Aspect Term Extraction and (optional) Polarity Classification.

Loads an ATEPC checkpoint and provides utilities to extract aspect terms from text, with optional sentiment classification for extracted aspects depending on the configured model/task. Supports single-text and batch inference and can auto-detect dataset files for inference.