
    -iv                         d Z ddlZddlZddlZddlZddlZedk    rej        	                    d           dZ
ddlmZ  G d d          Zd Zedk    r e             dS dS )	zq
Arabic Semantic relations Dictionary Class from Arramooz Al Waseet.
Used in multiporpus morpholigical treatment
    N__main__z../zdata/semantic.sqlitec                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )SemanticDictionaryz
    Arabic Semntic relations dictionary Class
    Used to allow abstract acces to lexicon of arabic language, 
    can get indexed and hashed entries from the  basic lexicon
    add also, support to extract attributtes from entries
    c                 L   t          t          d          rt          j        }nAt          j                            t          j                            t                              }t          j                            |t                    }d| _
        t          j                            |          r	 t          j        |          | _
        t          j        | j
        _        | j
                                        | _        d	S # t"          $ r5 t%          d|           t&          dk    rt          j                     Y d	S Y d	S w xY wt%          d                    d|dt          j        g                              d                     t&          dk    rt          j                     d	S d	S )
zw
        initialisation of dictionary from a data dictionary, create indexes 
        to speed up the access.

        frozenFz(Fatal Error Can't find the database filer    zInexistant Filez current dir utf8N)hasattrsysprefixospathdirnamerealpath__file__joinFILE_DB
db_connectexistssqliteconnectRowrow_factorycursorIOErrorprint__name__exitcurdirencode)selfbase	file_paths      Q/var/www/html/speakWrite/venv/lib/python3.11/site-packages/asmai/semdictionary.py__init__zSemanticDictionary.__init__!   sd    3!! 	?:DD7??27#3#3H#=#=>>DGLLw//	 7>>)$$ 	 "(.";";.4j+"o4466      @)LLL++HJJJJJJ ,++ 
 $)).	?I  v( ( (Z''




 ('s   0AC? ?8D>=D>c                 J    | j         r| j                                          dS dS )zH
        Delete instance and close database connection
        
        N)r   close)r!   s    r$   __del__zSemanticDictionary.__del__>   s2    
 ? 	$O!!#####	$ 	$    c                     d| j         d|d}	 | j                            |           | j        r| j                                        S n# t          j        $ r Y dS w xY wdS )z Get dictionary entry by id from the dictionary
        @param id :word identifier
        @type id: integer
        @return: all attributes
        @rtype: dict
        select * FROM  WHERE id=''F)
table_namer   executefetchallr   OperationalError)r!   idfsqls      r$   get_entry_by_idz"SemanticDictionary.get_entry_by_idG   s      48???CCCH	K$$${ .{++---.' 	 	 	55	us   9A AAc                     d| j         d|d}	 | j                            |           | j        r| j        D ]}||         c S n# t          j        $ r Y dS w xY wdS )a   Get attribute value by id from the dictionary
        @param id :word identifier
        @type id: integer
        @param attribute :the attribute name
        @type attribute: unicode
        @return: The attribute
        value
        @rtype: mix.
        r+   r,   r-   F)r.   r   r/   r   r1   )r!   r2   	attributer3   rows        r$   get_attrib_by_idz#SemanticDictionary.get_attrib_by_idX   s      48???CCCH	K$$${ +; + +C	N***' 	 	 	55	us   3A AAc                     g }d|d|d}	 | j                             |           | j         r/| j         D ]}|                    |d                    |r|d         S n# t          j        $ r g cY S w xY wdS )a(  
        look up for the relation between first and secondword
        @param primate_word: the derived word.
        @type primate_word: unicode.
        @param second_word: the related word.
        @type second_word: unicode.
        @return: list of relations .
        @rtype: list.
        z(select rule FROM relations WHERE first='z' and second ='r-   ruler   F)r   r/   appendr   r1   )r!   primate_wordsecond_wordrulelistr3   r7   s         r$   lookup_rulezSemanticDictionary.lookup_rulem   s     Q]Q]Q]		K$$${ '; 1 1COOCK0000 '#A;&' 	 	 	III	us   AA A21A2c                 :   g }d|z  }	 | j                             |           | j         r7| j         D ]%}|                    |d         |d         f           &|r|d         S n9# t          j        $ r' t          d|                    d                     Y dS w xY wdS )z
        look up for the original verb fo the given word
        @param primate_word: the derived word.
        @type primate_word: unicode.
        @return:  (verb, derivation type) .
        @rtype: tuple of unicode.
        z7select verb, type FROM derivations WHERE derived = '%s'r      degatr	   ) rC   )r   r/   r;   r   r1   r   r    )r!   r<   idlistr3   r7   s        r$   get_originalzSemanticDictionary.get_original   s     HLY
	K$$${ %; 3 3CMM3q6#a&/2222 %!!9$' 	 	 	'L//7788877	 ws   AA" "2BBc                     d}|                      ||          }|sL|                     |          \  }}|r2|r0|                      ||          }|                     ||          r|S d}|S )ak  
        look up for the relation between first and secondword
        @param primate_word: the derived word.
        @type primate_word: unicode.
        @param second_word: the related word.
        @param original:  if the lookup is made with original word
        @type second_word: unicode.
        @return: list of relations .
        @rtype: list.
        F)r?   rE   is_compatible)r!   r<   r=   r:   original_primate_wordderivation_types         r$   lookupzSemanticDictionary.lookup   s     k::  
	! 594E4El4S4S1 /# ! !&&'<kJJt$$T?;; !K Dr)   c                     ||k    S )a  
        Test if the rule is compatible to derivation type
        @param rule: the given rule.
        @type rule: unicode.
        @param derivation_type: the given derivation_type.
        @type derivation_type: unicode.
        @return: Boolean .
        @rtype: Boolean.
         )r!   r:   rI   s      r$   rG   z SemanticDictionary.is_compatible   s     &&r)   N)r   
__module____qualname____doc__r%   r(   r4   r8   r?   rE   rJ   rG   rL   r)   r$   r   r      s           :$ $ $  "  *  0  ,  <' ' ' ' 'r)   r   c            
         ddddd} t                      }ddgdd	gd
d	gdd	gddgddgddgddgddgg	}|D ]}|                    |d         |d                   }|r8t          d                    |                                        d          |f ^t          dd                    |                              d                     dS )z
    main test
    r   rA         )idfirstsecondr:   rC   u
   استقلu   اِسْتَعْمَلَu   قَلَمٌu   مُسْتَعْمَلٌu   مُسْتَعْمِلٌu   شَرَّحَu   طَبِيبٌu   جَسَدٌu   نَشَّطَu   دَواءٌu   دَوَاءٌu   مُنَشَّطٌr   r	   z	No resultN)r   rJ   r   r   r    )VERB_DICTIONARY_INDEXmydictwordlistwordsresults        r$   mainlyr[      s    	   !!F- /2 /2 /2*+)()*-.
H  C Ca%(33 	C499U##$$++F33V;;;;5!1!1 9 9& A ABBBBC Cr)   )rO   rer   os.pathsqlite3r   r   r   r   r;   r   pyarabic.arabyarabyr   r[   rL   r)   r$   <module>ra      s     
			             



HOOE
!      m' m' m' m' m' m' m' m'^C C C> 
FHHHHH r)   