Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1577 views


�t�W%�@sdZGdd�d�ZdS)zt
GSC Software: Tree class
Author: Nick Becker
Department of Cognitive Science, Johns Hopkins University
Summer 2015
c@sveZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�ZdS)�TreezN
    The last tree class you will ever need. Currently under development.
    cCsK|j|�|_g|_|j|jd�g|_|j|j�dS)a�
        Create an internal representation of a tree by passing in a text version of it.
        Formatting rules apply: trees must be passed to this method in the form
            A (B A (D E))

        In other words, parentheses enclose a node's children, and sister nodes are
        separated by whitespace. Parentheses were chosen over brackets to avoid any
        confusion when entering HNF trees, which contain many bracketed symbols.

        Any symbols can be used in node labels EXCEPT parentheses, commas, and
        percent signs (%).

        Calling this method automatically sets the recursive-style filler-role bindings
        for each node, as well as the span-style filler-role bindings for each node.
        �rN)�treeStringToList�treeList�recursiveFRbindings�setRecursiveRoles�spanFRbindings�setSpanRoles)�selfZ
treeString�r
�6/projects/18c77389-a5c3-49de-946a-7593b53d3fb2/tree.py�__init__s
		z
Tree.__init__cCs�|jdd�j�}d}x�|t|�kr||}t|�dkr|ddkso|ddkr�|dd�||<|j||d�|d7}||}|ddks�|d	dkr|dd
�||<|j|d|d�|d8}|d7}q!Wd}x�|t|�dkr�||}||d}|dkrv|dkrv|dkrv|dks�|dkr�|dkr�|dkr�|j|dd�|d7}|d7}qW|S)az
        Check formatting of the input string; if it is valid, prepare it for internal
        operations.

        TO DO: check input string, most likely using regular expressions
        - number of '(' should match number of ')'
        - - len(findall(...))
        - first character should be something other than ( or )
        - no stray characters after final )
        �,����(�)N�����rrr)�replace�split�len�insert)r	�inputStringZniceList�iZ
currentSymbolZ
nextSymbolr
r
rr-s2
 

 

0$
zTree.treeStringToListcCst|�dkr�|ddkr5td�|}n~|ddkrvtt|d�d�|dd�}|}n=|ddkr�|dd�}n|jj|d|f�|j|dd�|�n�d}x�|rzd}x�tt|j�d�D]t}t|j|d�t|j|dd�kr�|j|}|j|d|j|<||j|d<d}q�Wq�WdS)	zE
        Recursive function to populate recursiveFRbindings.
        rrr
rNrTF)r�str�intr�appendr�range)r	Z	treeNodes�levelZnewLevel�needSwappedr�tempr
r
rrZs&(		 2
zTree.setRecursiveRolescCs�|j|�}d}i}xdtt|��D]P}t||d�dkr.t|�t|d�|||d<|d7}q.Wd}x�|sx�tt|��D]�}t||d�dkr�d}d}xK||dD];}	|	|kr||	}
||
d|
d	}q�d}q�W|s�djtt|�d|j��}||||d<q�Wt|�t|�kr�d}q�Wt|�dg}xE|j�D]7}|j	d�}
|
d||f|t
|
d�<q�W||_dS)
a#
        A dumb, iterative function to populate spanFRbindings. Once again, probably
        bad programming practice to populate a variable like this instead of returning
        a value.

        A nice recursive algorithm to do this task is certainly possible and would
        be far more elegant, but it turned out not to be as straighforward as I initially
        thought... hence the inefficient iterative algorithm here. Luckily, this
        shouldn't be a problem because most users will be looking at very small
        trees.
        rrFrT�keyN�%r)�findSubTreesrrr�join�sorted�set�index�keysrrr)r	r�subTreesZspanBaseZspanDictr�done�spanZinvalidSpan�childZ	childSpanZtempBindingsr!ZsplitKeyr
r
rr}s8&	

$
&zTree.setSpanRolesc	Cs�d}xptt|�d�D]X}||dkr||dkr||dkr||dt|�7<|d7}qWg}x1tt|�d�D]}||dkr�||dkr�||dkr�g}|d}||dk}|r�d}|d7}x�|t|�kr�|dkr�||dkr@|d7}nJ||dkr]|d8}n-||dkr�|dkr�|j||�|d7}qW|j|||f�q�W|S)	zV
        Given a tree in list form, find the immediate children of each node.
        rrrr
rr"rr)rrrr)	r	rZidNorr)ZcurrentChildren�jZhasChildren�embeddedr
r
rr#�s000

!

zTree.findSubTreescCs|jS)N)r)r	r
r
r�
getFRbindings�szTree.getFRbindingscCs�d}x~tt|j��D]g}|dkr8|d7}||j|dd|j|d7}|t|j�dkr|d7}qW|d7}|S)zT
        Gets a pretty string for the recursive-style filler-role bindings.
        �{r� �/rz; 
z}
)rrr)r	�returnStringrr
r
r�recursiveFRtoString�s
(
zTree.recursiveFRtoStringcCs�d}x~tt|j��D]g}|dkr8|d7}||j|dd|j|d7}|t|j�dkr|d7}qW|d7}|S)zO
        Gets a pretty string for the span-style filler-role bindings.
        r0rr1r2rz; 
z}
)rrr)r	r3rr
r
r�spanFRtoString�s
(
zTree.spanFRtoStringN)�__name__�
__module__�__qualname__�__doc__rrrrr#r/r4r5r
r
r
rr
s-#2$rN)r9rr
r
r
r�<module>s