o
    qi                     @   s\   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ G dd	 d	eZdS )
z
Sentence data structures
    N)Tuple)CoNLL)	Conllable   )TokenWordc                   @   s   e Zd ZdZd#deeef fddZdedefdd	Z	dede
fd
dZd#dedefddZdd Zedd Zejdd ZedefddZejdd Zedd Zejdd Zedd Zejdd Zdd Zdefdd Zd!d" ZdS )$SentencezY A sentence class that stores attributes of a sentence and carries a list of tokens.
    Nsentencec                 C   s8   i | _ g | _g | _g | _d| _g | _|| _| | dS )z Construct a sentence given a dict object which contain sentence meta and
        tokens in the form of CoNLL-U dicts.
        N)_meta_tokens_words_dependencies_text_ents_doc_process)selfr	   doc r   O/home/ubuntu/.local/lib/python3.10/site-packages/urduhack/core/unit/sentence.py__init__   s   zSentence.__init__keyreturnc                 C   s
   | j | S )ap  
        Returns the value associated with the key in the metadata (comments).

        Args:
            key (str): The key whose value to look up.
        Returns:
            str: The value associated with the key as a string. If the key is a singleton then None is returned.
        Raises:
            KeyError: If the key is not present in the comments.
        r
   r   r   r   r   r   
meta_value    s   
zSentence.meta_valuec                 C   s
   || j v S )a  
        Check if the key is present as a singleton or as a pair.

        Args:
            key (str): The value to check for in the comments.
        Returns:
            bool: True if the key was provided as a singleton or as a key value pair. False otherwise.
        r   r   r   r   r   meta_present-   s   
	zSentence.meta_presentvaluec                 C   s   || j |< dS )a#  
        Set the metadata or comments associated with this Sentence.

        Args:
            key (str): The key for the comment.
            value (str): The value to associate with the key. If the comment is a
                singleton, this field can be ignored or set to None.
        Nr   )r   r   r   r   r   r   set_meta8   s   	zSentence.set_metac                 C   s   d}g g | _ | _|\}}| D ]
\}}| || qt|D ]C\}}tj|vr2t|d |tj< t|}	| j	|	 t
|tj}
|
|krR| j d j	|	 n| j 	t||	gd | j d |	_q d S )Nr   )words)tokensr    itemsr   	enumerater   IDstrr   appendintgetr   parent)r   r	   e_nsentence_metar!   r   r   ientrynew_wordidxr   r   r   r   C   s    
zSentence._processc                 C      | j S )z% Access the parent doc of this span. r   r   r   r   r   r   X      zSentence.docc                 C   
   || _ dS )z" Set the parent doc of this span. Nr1   r   r   r   r   r   r   ]      
c                 C   r0   )z( Access the raw text for this sentence. r   r2   r   r   r   textb   r3   zSentence.textc                 C   r4   )z% Set the raw text for this sentence. Nr7   r5   r   r   r   r8   g   r6   c                 C   r0   )z. Access the list of tokens for this sentence. r   r2   r   r   r   r!   l   r3   zSentence.tokensc                 C   r4   )z+ Set the list of tokens for this sentence. Nr9   r5   r   r   r   r!   q   r6   c                 C   r0   )z- Access the list of words for this sentence. r   r2   r   r   r   r    v   r3   zSentence.wordsc                 C   r4   )z* Set the list of words for this sentence. Nr:   r5   r   r   r   r    {   r6   c                 C   s    g }| j D ]}|| 7 }q|S )zV Dumps the sentence into a list of dictionary for each token in the sentence.
        )r!   to_dict)r   rettokenr   r   r   r;      s   
zSentence.to_dictc                 C   s   |  ds| jdur| d| j g }t| j tdd}|D ]'}|d dur9tj	 d|d  d|d  }n
tj	 d|d  }|
| q!| jD ]	}|
|  qLd|S )	z
        Convert the sentence to a CoNLL-U representation.

        Returns:
            str: A string representing the Sentence in CoNLL-U format.
        r8   Nr   )r   r    z = 
)r   r8   r   sortedr
   r"   operator
itemgetterr   COMMENT_MARKERr&   r    conlljoin)r   linessorted_metametalinewordr   r   r   rD      s    

zSentence.conllc                 C   s   t j|  dddS )N   F)indentensure_ascii)jsondumpsr;   r2   r   r   r   __repr__   s   zSentence.__repr__)N)__name__
__module____qualname____doc__r   dictlistr   r%   r   boolr   r   r   propertyr   setterr8   r!   r    r;   rD   rP   r   r   r   r   r      s4    






r   )rT   rN   rA   typingr   urduhack.conllr   urduhack.conll.conllabler   r=   r   r   r   r   r   r   r   <module>   s   