Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yiming-wange
GitHub Repository: yiming-wange/cs224n-2023-solution
Path: blob/main/a3/__pycache__/parser_transitions.cpython-310.pyc
995 views
o

id�.�@s�dZddlZGdd�de�Zdd�Zdd�Zd	d
�Zdd�ZGd
d�de�Zdd�Z	dd�Z
edkr\ejZ
ee
�dkr?ed��e
ddkrMe�e�dSe
ddkrXe
�dSed��dS)z�
CS224N 2021-2022: Homework 3
parser_transitions.py: Algorithms for completing partial parsess.
Sahil Chopra <[email protected]>
Haoshen Hong <[email protected]>
�Nc@s$eZdZdd�Zdd�Zdd�ZdS)�PartialParsecCs"||_dg|_|��|_g|_dS)z�Initializes this partial parse.

        @param sentence (list of str): The sentence to be parsed as a list of words.
                                        Your code should not modify the sentence.
        �ROOTN)�sentence�stack�copy�buffer�dependencies)�selfr�r
�D/Users/yimingwang/Desktop/cs224n/assignment/a3/parser_transitions.py�__init__
s

zPartialParse.__init__cCs�|dkr|j�|jd�|jd=dS|dkr+|j�|jd|jdf�|jd=dS|dkrC|j�|jd|jdf�|jd=dSdS)aaPerforms a single parse step by applying the given transition to this partial parse

        @param transition (str): A string that equals "S", "LA", or "RA" representing the shift,
                                left-arc, and right-arc transitions. You can assume the provided
                                transition is a legal transition.
        �Sr�LA�����������RAN)r�appendrr)r	�
transitionr
r
r�
parse_step)s�zPartialParse.parse_stepcCs|D]}|�|�q|jS)a�Applies the provided transitions to this PartialParse

        @param transitions (list of str): The list of transitions in the order they should be applied

        @return dependencies (list of string tuples): The list of dependencies produced when
                                                        parsing the sentence. Represented as a list of
                                                        tuples where each tuple is of the form (head, dependent).
        )rr)r	�transitionsrr
r
r�parseBs	zPartialParse.parseN)�__name__�
__module__�__qualname__rrrr
r
r
rrsrcCs�g}dd�|D�}|dd�}t|�dkrD|d|�}|�|�}t||�D]\}}	|�|	�}
|js=t|j�dkr=|�|�q%t|�dksdd�|D�}|S)a�Parses a list of sentences in minibatches using a model.

    @param sentences (list of list of str): A list of sentences to be parsed
                                            (each sentence is a list of words and each word is of type string)
    @param model (ParserModel): The model that makes parsing decisions. It is assumed to have a function
                                model.predict(partial_parses) that takes in a list of PartialParses as input and
                                returns a list of transitions predicted for each parse. That is, after calling
                                    transitions = model.predict(partial_parses)
                                transitions[i] will be the next transition to apply to partial_parses[i].
    @param batch_size (int): The number of PartialParses to include in each minibatch


    @return dependencies (list of dependency lists): A list where each element is the dependencies
                                                    list for a parsed sentence. Ordering should be the
                                                    same as in sentences (i.e., dependencies[i] should
                                                    contain the parse for sentences[i]).
    cSsg|]}t|��qSr
)r)�.0rr
r
r�
<listcomp>qsz#minibatch_parse.<locals>.<listcomp>Nr�cSsg|]}|j�qSr
)r)rrr
r
rr|s)�len�predict�ziprrr�remove)�	sentences�model�
batch_sizer�partial_parsesZunfinished_parses�batchrZ
partial_parser�transr
r
r�minibatch_parsePs


��	r'c	Cs�tg�}||||_|_|_|�|�t|j�t|j�tt|j��}}}||ks4Jd�|||���||ksAJd�|||���||ksNJd�|||���td�|��dS)z:Tests that a single parse step returns the expected outputz,{:} test resulted in stack {:}, expected {:}z-{:} test resulted in buffer {:}, expected {:}�6{:} test resulted in dependency list {:}, expected {:}z{:} test passed!N)	rrrrr�tuple�sorted�format�print)	�namerr�buf�depsZex_stackZex_buf�ex_deps�ppr
r
r�	test_step�s
&
�
�
�r2c	CsXtddddgddggddd	�td
dgd�dggddd
�tddgd�ggdd	d�dS)z`Simple tests for the PartialParse.parse_step function
    Warning: these are not exhaustive
    �SHIFTr
r�the�cat�sat)rr4r5)r6r
zLEFT-ARCr)rr5))r5r4z	RIGHT-ARCr)r�run�fast)rr7))r7r8N)r2r
r
r
r�test_parse_step�s���r9cCsbgd�}t|��gd��}tt|��}d}||ks!Jd�||���t|�dks+Jd��td�dS)z[Simple tests for the PartialParse.parse function
    Warning: these are not exhaustive
    )r�thisr)r
r
r
rrr))rr)rr)rr:z5parse test resulted in dependencies {:}, expected {:}z<parse test failed: the input sentence should not be modifiedzparse test passed!N)rrr)r*r+r,)rr�expectedr
r
r�
test_parse�s

��r<c@s2eZdZdZddd�Zdd�Zdd�Zd	d
�ZdS)
�
DummyModelz9Dummy model for testing the minibatch_parse function
    �unidirectionalcCs
||_dS)N��mode)r	r@r
r
rr�s
zDummyModel.__init__cCs.|jdkr
|�|�S|jdkr|�|�St��)Nr>�
interleave)r@�unidirectional_predict�interleave_predict�NotImplementedError�r	r$r
r
rr�s




zDummyModel.predictcC�dd�|D�S)z�First shifts everything onto the stack and then does exclusively right arcs if the first word of
        the sentence is "right", "left" if otherwise.
        cSs4g|]}t|j�dkr|jddurdndnd�qS)rr�rightrrr
�rrr�rr1r
r
rr�s,�z5DummyModel.unidirectional_predict.<locals>.<listcomp>r
rEr
r
rrB�s�z!DummyModel.unidirectional_predictcCrF)zXFirst shifts everything onto the stack and then interleaves "right" and "left".
        cSs8g|]}t|j�dkrt|j�ddkrdndnd�qS)r�rrr
rHrIr
r
rr�s0�z1DummyModel.interleave_predict.<locals>.<listcomp>r
rEr
r
rrC�s�zDummyModel.interleave_predictN)r>)rrr�__doc__rrrBrCr
r
r
rr=�s
r=cCs*tt|��}||ksJd�|||���dS)z?Tests the provided dependencies match the expected dependenciesr(N)r)r*r+)r-r/r0r
r
r�test_dependencies�s
�rLcCs�gd�gd�gd�gd�g}t|t�d�}td|dd�td|d	d
�td|dd�td|dd
�dgg}t|t�d�}td|dd�gd�g}t|tdd�d	�}td|dd�td�dS)zXSimple tests for the minibatch_parse function
    Warning: these are not exhaustive
    )rG�arcs�only)rGrMrN�again)�leftrMrN)rPrMrNrOrJr'r)�rrG�rMrN�rGrMr)rQrR)rNrOrS))rNr)rNrM)rNrP�))rOr)rOrM)rOrP)rOrNrG)rQ)r:�is�interleaving�
dependency�testrAr?))rrU)rWrV)rWrX)rUrW)rUr:zminibatch_parse test passed!N)r'r=rLr,)r!r/r
r
r�test_minibatch_parse�s6�
�
�
�
�

�rY�__main__rJzdYou did not provide a valid keyword. Either provide 'part_c' or 'part_d', when executing this scriptrZpart_cZpart_d)rK�sys�objectrr'r2r9r<r=rLrYr�argv�argsr�	Exceptionr
r
r
r�<module>s*D2"

�