o
    h:                     @   s   d dl Z d dlZd dlZd dlm  mZ d dlm  m  mZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZ ejjejjgZG dd	 d	ZdS )
    N)Trainer)TEST_MODELS_DIR)fake_embeddings)train_file_with_treesdev_file_with_trees)utils)build_trainerTREEBANKc                   @   sl   e Zd Zejdddd ZdddZddd	Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )TestConstituencyClassifierclass)scopec                 C   sF   g d}t t|g|R dti}t|dd }|j|dd |S )N)z--pattn_num_layers0z--lattn_d_projr   z--hidden_size20z--delta_embedding_dim10treebankconstituencyzconstituency.ptF)save_optimizer)r   strr	   mktempsave)selfr   tmp_path_factoryargstrainer
trainer_pt r   p/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/tests/classifiers/test_constituency_classifier.pyconstituency_model   s
   z-TestConstituencyClassifier.constituency_modelNc                 C   s   t |d }d}d|d|ddd|dt |d	d
dt |dt |ddddg}	|dur,|	| }	t|	}	t|	j|	j|	j}
t	|	|
}||
|	fS )z\
        Build a Constituency Classifier model to be used by one of the later tests
        
classifierzmodel.ptz
--save_dirz--save_namez--model_typer   z--constituency_modelz--wordvec_pretrain_filez--fc_shapesz20,10z--train_filez
--dev_filez--max_epochs2z--batch_size60N)
r   r   
parse_argsdataread_dataset
train_filewordvec_typemin_train_lenr   build_new_model)r   tmp_pathr   r   r   r   
extra_argssave_dir	save_namer   	train_setr   r   r   r   build_model   s&   


z&TestConstituencyClassifier.build_modelc              	   C   s|   |  ||||||\}}}	t|	j|	j|	j}
t|}tj	|	j
|	j}t|	j
||	j}t||||	||
| |||	fS )z5
        Iterate a couple times over a model
        )r-   r"   r#   dev_filer%   r&   dataset_labelsospathjoinr*   r+   r   checkpoint_namecheckpoint_save_namer   train_model)r   r(   r   r   r   r   r)   r   r,   r   dev_setlabelssave_filenamecheckpoint_filer   r   r   run_training2   s   

z'TestConstituencyClassifier.run_trainingc                 C   s   |  ||||| dS )zK
        Test that building a basic constituency-based model works
        N)r-   r   r(   r   r   r   r   r   r   r   test_build_model?   s   z+TestConstituencyClassifier.test_build_modelc           
      C   sN   |  |||||\}}}tj|j|j}	||	 |j|_t	|j|}dS )z@
        Test that a constituency model can save & load
        N)
r-   r0   r1   r2   r*   r+   r   	load_namer   load)
r   r(   r   r   r   r   r   _r   r8   r   r   r   test_save_loadE   s
   
z)TestConstituencyClassifier.test_save_loadc                 C   s   |  ||||| d S Nr:   r;   r   r   r   test_train_basicQ   s   z+TestConstituencyClassifier.test_train_basicc                 C   s   |  |||||\}}}tj|j|j}	tj|	sJ tj|js&J ddtdd|j|j	dd|	|j	ddd}
t
jdi |
}|d}|jd jdusOJ dS )	zk
        Test that writing out a temp model, then loading it in the pipeline is a thing that works
        enNz#tokenize,pos,constituency,sentimentT)langdownload_method	model_dir
processorstokenize_pretokenizedconstituency_model_pathconstituency_pretrain_path!constituency_backward_charlm_path constituency_forward_charlm_pathsentiment_model_pathsentiment_pretrain_pathsentiment_backward_charlm_pathsentiment_forward_charlm_pathzThis is a testr   r   )r:   r0   r1   r2   r*   r+   existsr   r   wordvec_pretrain_filestanzaPipeline	sentences	sentiment)r   r(   r   r   r   r   r   r?   r   r8   pipeline_argspipelinedocr   r   r   test_train_pipelineT   s*   z.TestConstituencyClassifier.test_train_pipelinec                 C   0   |  |||||dg |  |||||dg d S )N--constituency_all_words--no_constituency_all_wordsrB   r;   r   r   r   test_train_all_wordsp      z/TestConstituencyClassifier.test_train_all_wordsc                 C   r\   )Nz--constituency_top_layerz--no_constituency_top_layerrB   r;   r   r   r   test_train_top_layeru   r`   z/TestConstituencyClassifier.test_train_top_layerc              	   C   sJ   |  |||||ddg |  |||||ddg |  |||||dg d S )Nz--constituency_node_attnr^   r]   z--no_constituency_node_attnrB   r;   r   r   r   test_train_attnz   s   z*TestConstituencyClassifier.test_train_attnrA   )__name__
__module____qualname__pytestfixturer   r-   r:   r<   r@   rC   r[   r_   ra   rb   r   r   r   r   r
      s    



r
   )r0   rf   rT   stanza.models.classifiermodelsr   stanza.models.classifiers.dataclassifiersr"   !stanza.models.classifiers.trainerr   stanza.testsr   (stanza.tests.classifiers.test_classifierr   "stanza.tests.classifiers.test_datar   r   stanza.models.commonr   &stanza.tests.constituency.test_trainerr   r	   markrY   travis
pytestmarkr
   r   r   r   r   <module>   s    