o
    h                     @   s`   d Z ddlZddlZddlmZ ddlmZ ddlT ddlm	Z	m
Z
 e
eG dd de	ZdS )	a~  Processor that attaches a sentiment score to a sentence

The model used is a generally a model trained on the Stanford
Sentiment Treebank or some similar dataset.  When run, this processor
attaches a score in the form of a string to each sentence in the
document.

TODO: a possible way to generalize this would be to make it a
ClassifierProcessor and have "sentiment" be an option.
    N)SimpleNamespace)Trainer)*)UDProcessorregister_processorc                   @   s<   e Zd ZeegZeegZdZdd Z	dd Z
dd ZdS )	SentimentProcessori  c           
   	   C   s   | dd }| dd }| dd }t||||d dd d}|d }|d u r,td|tj|||jd}	|	| _|	j| _	| j	j
j| _| d	tj| _d S )
Npretrain_pathforward_charlm_pathbackward_charlm_pathF)devicecharlm_forward_filecharlm_backward_filewordvec_pretrain_file
elmo_modeluse_elmosave_dir
model_pathzbNo model specified for the sentiment processor.  Perhaps it is not supported for the language.  {})filenameargsfoundation_cache
batch_size)getr   FileNotFoundErrorformatr   loadr   _trainermodel_modelconfig
model_type_model_typer   DEFAULT_BATCH_SIZE_batch_size)
selfr   pipeliner   r   r	   r
   r   r   trainer r&   ^/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/pipeline/sentiment_processor.py_set_up_model#   s,   z SentimentProcessor._set_up_modelc                 C   s4   t | jj}dd | D }|| || _d S )Nc                 S   s    i | ]\}}t |s||qS r&   )r   filter_out_option).0kvr&   r&   r'   
<dictcomp>C   s     z;SentimentProcessor._set_up_final_config.<locals>.<dictcomp>)dataclassesasdictr   r   itemsupdate_config)r#   r   loaded_argsr&   r&   r'   _set_up_final_configA   s   

z'SentimentProcessor._set_up_final_configc                 C   sZ   | j |}t  | j j|| jd}W d    n1 sw   Y  |jt|dd |S )N)r   T)to_sentence)r   extract_sentencestorchno_gradlabel_sentencesr"   set	SENTIMENT)r#   document	sentenceslabelsr&   r&   r'   processH   s   
zSentimentProcessor.processN)__name__
__module____qualname__r:   r;   PROVIDES_DEFAULTTOKENIZEREQUIRES_DEFAULTr!   r(   r4   r?   r&   r&   r&   r'   r      s    

r   )__doc__r.   r7   typesr   !stanza.models.classifiers.trainerr   stanza.pipeline._constantsstanza.pipeline.processorr   r   r;   r   r&   r&   r&   r'   <module>   s    