pyabsa.tasks.AspectPolarityClassification.prediction.sentiment_classifier¶
Classes¶
High-level predictor for Aspect Polarity Classification (APC). |
|
High-level predictor for Aspect Polarity Classification (APC). |
Module Contents¶
- class pyabsa.tasks.AspectPolarityClassification.prediction.sentiment_classifier.SentimentClassifier(checkpoint=None, **kwargs)¶
Bases:
pyabsa.framework.prediction_class.predictor_template.InferenceModelHigh-level predictor for Aspect Polarity Classification (APC).
This class loads a trained APC checkpoint and provides convenient inference APIs for single-sentence and batch predictions. It supports checkpoints produced by different APC model families (e.g., LCF-based, BERT-baseline, GloVe-based) and automatically builds the corresponding inference dataset and tokenizer from the checkpoint configuration.
Typical usage:
Load from a checkpoint directory containing .config, .tokenizer, and either .state_dict or .model files
Call predict(text) for a string or list of strings with marked aspect terms, or batch_predict(file_path) for a dataset file
The returned results include predicted sentiment, confidence, and probabilities; optionally, when reference labels exist in the input, evaluation metrics are printed.
- task_code = 'APC'¶
- batch_infer(target_file=None, print_result=True, save_result=False, ignore_error=True, **kwargs)¶
Deprecated alias of batch_predict.
- Parameters:
target_file – Path to the input file or directory.
print_result – Whether to print formatted results to stdout.
save_result – Whether to save JSON results to disk.
ignore_error – Skip malformed lines instead of raising errors.
**kwargs – Additional inference options such as eval_batch_size.
- Returns:
Inference results; when labels exist, also prints summary metrics.
- Return type:
List[dict]
- infer(text: str = None, print_result=True, ignore_error=True, **kwargs)¶
Deprecated alias of predict for a single text input.
- Parameters:
text – The input sentence with marked aspect terms.
print_result – Whether to print formatted results to stdout.
ignore_error – Skip parsing errors instead of raising.
**kwargs – Additional inference options such as eval_batch_size.
- Returns:
The prediction result for the given text.
- Return type:
dict
- batch_predict(target_file=None, print_result=True, save_result=False, ignore_error=True, **kwargs)¶
Run APC inference on a dataset file or directory.
The function detects valid APC inference files under the given path and performs batched prediction. When gold labels are present in the input, overall metrics (accuracy, classification report, confusion matrix) are printed.
- Parameters:
target_file – Path to a file or directory containing APC data.
print_result – Print formatted results to stdout.
save_result – Save JSON results to the working directory.
ignore_error – Skip malformed lines instead of raising errors.
**kwargs – Additional options, e.g., eval_batch_size.
- Returns:
Inference results, one entry per input example.
- Return type:
List[dict]
- predict(text: str | list = None, print_result=True, ignore_error=True, **kwargs)¶
Predict aspect sentiments for a string or a list of strings.
Each input sentence should contain aspect terms annotated with the expected markers used by the selected APC dataset format.
- Parameters:
text – A single string or list of strings to infer.
print_result – Print formatted results to stdout.
ignore_error – Skip parsing errors instead of raising.
**kwargs – Additional options, e.g., eval_batch_size.
- Returns:
A single result when text is a string, otherwise a list of results.
- Return type:
dict or List[dict]
- merge_results(results)¶
Merge per-aspect predictions belonging to the same input text.
The inference loop emits one record per aspect term. This utility consolidates multiple aspect records that originate from the same sentence into a single entry with list fields.
- Parameters:
results – Flat list of per-aspect prediction records.
- Returns:
Merged results with per-text aggregation.
- Return type:
List[dict]
- _run_prediction(save_path=None, print_result=True, **kwargs)¶
Internal prediction loop shared by predict and batch_predict.
Executes the model in evaluation mode over self.infer_dataloader, collects raw logits, derives sentiments and confidences, and optionally prints and saves results. When reference labels are available, it also reports accuracy, classification report and confusion matrix.
- Parameters:
save_path – Optional file path to save JSON results.
print_result – Whether to print formatted results to stdout.
**kwargs – Additional control flags (e.g., merge_results).
- Returns:
Inference results for all samples.
- Return type:
List[dict]
- clear_input_samples()¶
Clear any previously prepared inference samples/dataset cache.
- class pyabsa.tasks.AspectPolarityClassification.prediction.sentiment_classifier.Predictor(checkpoint=None, **kwargs)¶
Bases:
SentimentClassifierHigh-level predictor for Aspect Polarity Classification (APC).
This class loads a trained APC checkpoint and provides convenient inference APIs for single-sentence and batch predictions. It supports checkpoints produced by different APC model families (e.g., LCF-based, BERT-baseline, GloVe-based) and automatically builds the corresponding inference dataset and tokenizer from the checkpoint configuration.
Typical usage:
Load from a checkpoint directory containing .config, .tokenizer, and either .state_dict or .model files
Call predict(text) for a string or list of strings with marked aspect terms, or batch_predict(file_path) for a dataset file
The returned results include predicted sentiment, confidence, and probabilities; optionally, when reference labels exist in the input, evaluation metrics are printed.