[文档]@TAGGING_OPS.register_module(OP_NAME)@OPERATORS.register_module(OP_NAME)classQuerySentimentDetectionMapper(Mapper):""" Mapper to predict user's sentiment label ('negative', 'neutral' and 'positive') in query. Input from query_key. Output label and corresponding score for the query, which is store in 'query_sentiment_label' and 'query_sentiment_label_score' in Data-Juicer meta field. """_accelerator='cuda'_batched_op=True
[文档]def__init__(self,hf_model:str='mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis',# noqa: E501 E131zh_to_en_hf_model:Optional[str]='Helsinki-NLP/opus-mt-zh-en',model_params:Dict={},zh_to_en_model_params:Dict={},*,label_key:str=MetaKeys.query_sentiment_label,score_key:str=MetaKeys.query_sentiment_score,**kwargs):""" Initialization method. :param hf_model: Huggingface model ID to predict sentiment label. :param zh_to_en_hf_model: Translation model from Chinese to English. If not None, translate the query from Chinese to English. :param model_params: model param for hf_model. :param zh_to_en_model_params: model param for zh_to_hf_model. :param label_key: The key name in the meta field to store the output label. It is 'query_sentiment_label' in default. :param score_key: The key name in the meta field to store the corresponding label score. It is 'query_sentiment_label_score' in default. :param kwargs: Extra keyword arguments. """super().__init__(**kwargs)self.label_key=label_keyself.score_key=score_keyself.model_key=prepare_model(model_type='huggingface',pretrained_model_name_or_path=hf_model,return_pipe=True,pipe_task='text-classification',**model_params)ifzh_to_en_hf_modelisnotNone:self.zh_to_en_model_key=prepare_model(model_type='huggingface',pretrained_model_name_or_path=zh_to_en_hf_model,return_pipe=True,pipe_task='translation',**zh_to_en_model_params)else:self.zh_to_en_model_key=None