
    -i0]                     R   d Z ddlZddlZddlZddlZej                            d           ej                            d           ddlmZ ddlm	Z	 ddl
ZddlZddlmZ ddlZddlZdZej        ej        ej        ej        gZ G d	 d
          Zd Zedk    r e             dS dS )z
    Arabic Semantic analyzer
    Nz../z../lib   )	sem_const)semdictionaryFc                   j    e Zd ZdZddZd Zd Z	 	 ddZd Zd	 Z	d
 Z
d Zd Zd ZddZd Zd ZdS )SemanticAnalyzerz&
        Arabic Semantic analyzer
    Fc                     t          j                    | _        t          j                            |          | _        t          j        t          j                   t          j	        t                    | _        d S )N)level)r   SemanticDictionarysemdictsylajonecachesyncacheloggingbasicConfigNOTSET	getLogger__name__logger)self
cache_paths     J/var/www/html/speakWrite/venv/lib/python3.11/site-packages/asmai/anasem.py__init__zSemanticAnalyzer.__init__/   sW    $799 !,,Z88'.1111'11    c                 ,    |                      |          S )a#  
        Semantic analysis of stemming and syntaxic results.

        morphological and analysis Result is a list of list of 
        StemmedSynWord class.
        The semantic result have the same structure, but we add a 
        field named 'semantic' to every word steming dictioionary
        @param detailed_stemming_dict: detailed stemming stemmedsynword.
        @type detailed_stemming_dict:list of list of stemmedsynword
        @return: detailed semantic result with semantic tags.
        @rtype: list of list of stemmedsynword
        )context_analyze)r   detailed_stemming_dicts     r   analyzezSemanticAnalyzer.analyze8   s     ##$:;;;r   c           	      P   d}d}t          t          |                    D ]}d}||         D ]p}|dk    r|                     d|          d         }n-d}||         D ]"}|                     ||||          \  }}|dz  }#|                                rd}nd}|dz  }q|s|}d}|S )aG  
        Semantic analysis of stemming results.
        @param stemmed_synwordlistlist: detailed stemming and syntaxic details.
        @type stemmed_synwordlistlist: list of list of stemmedsynword class
        @return: detailed semantic result with semantic tags.
        @rtype: list of list of stemmedsynword class
        Fr   Nr   T)rangelenbigram_analyzeis_transparent)	r   stemmed_synwordlistlistignoreprevious_indexcurrent_indexstmword_case_positionstmwordprevious_case_positionpreviouss	            r   r   z SemanticAnalyzer.context_analyzeH   s     #C(?$@$@AA #	 #	M$%!2=A , ,!Q&&  $224AA!DGG /0*$;N$K 5 5-1-@-@<C<R<Q.S .S)' /14.. ))++ %!FF$f%!+%%  #0  '&r   r   c           
      D	  	 d}d}|r|s||fS |                      ||          r0|                    |                                          }| j                            |                                |                                          	| j                            |                                |                                          }t          rt          d           t          rwt          d                    |                                |                                t          |          t          	          g                                        d           	rY|	v r|}d}nP|t          j        k    rt          j        |v r|}d}n-|t          v r$t!          	fdt          D                       r|}d}|s|                                r|                                rd}|                     ||          }|r|t(          j        k    rX|                                r|                                rd	}n|                                s|                                rd
}n|t(          j        k    r*|                                s|                                rd}n|                                r|                                r|                     ||          }d}|rf|t(          j        k    r0|                                rd	}nI|                                sd
}n1|t(          j        k    r|                                sd}n	|                                r|                                r|                     ||          }d}|r|t(          j        k    r|                                rd	}n|t(          j        k    r|                                rd}ny|t(          j        k    r-|                                rd|                                v rd}n<|t(          j        k    r,|                                rd|                                v rd}|rG|rEt          rt          d|d|           |                    ||           |                     ||           ||fS )a  
        Syntaxic analysis of stemming results, two words.
        the positions are use to join related cases.
        @param previous    : the first item in bigram.
        @type previous    : stemmedSynWord
        @param current    : the second item in bigram.
        @type current    : stemmedSynWord
        @param previous_position    : the first item position 
        in the word case list.
        @type previous_position    : integer
        @param current_position    : the second item position 
        in the word case list.
        @type current_position    : integer
        @return: the updated previous and current stemmedSynWord.
        @rtype: (previous, current)
         anasem utf8ok0zok0-verb-subjectc              3       K   | ]}|v V  	d S )N ).0rsyn_relat_dicts     r   	<genexpr>z2SemanticAnalyzer.bigram_analyze.<locals>.<genexpr>   s)      EyEy^_a>FYEyEyEyEyEyEyr   zok0-primateok1ok2ok3ok4u   اسم فاعلok5u   اسم مفعولok6zanasem:r relation	confirmed)!is_syn_relatedget_next_relation	get_orderr   
is_relatedget_originaldebugprintjoinstrreprencodesycVerbSubjectRelationSubjectVerbRelationPRIMATE_RELATION_LISTanyis_verbis_nounare_sem_relatedr   	Predicate
is_passive
is_marfou3
is_mansoubSubjectAdded
is_majrourAdjis_adjget_typeadd_sem_nextadd_sem_previous)
r   r*   currentprevious_positioncurrent_positionrelationr=   syn_relationsyn_relat_dict_invertdr5   s
            @r   r!   zSemanticAnalyzer.bigram_analyze   s   & 	 	%w 	%W$$ x11 q	2 $55g6G6G6I6IJJL "]55h6K6K6M6M6=6J6J6L6LN NN &*]%=%=g>R>R>T>T6>6K6K6M6M&O &O"%eHooo  ReDIIx'<'<'>'>@T@T@V@VX[\hXiXikop~kk  'A  B  B  C  C  J  J  KQ  R  R  R .  >11+H %II!c&===#BY]sBsBs+H 2II "&;;;#EyEyEyEycxEyEyEyByBy;+H -I  :2##%% 92'//*;*; 92 "I $ 4 4Xw G GH 2#y':::'2244 29K9K9M9M 2,1		%-%8%8%:%: 2w?Q?Q?S?S 2,1	%):::#+#6#6#8#8 2W=O=O=Q=Q 2,1	%%'' (2GOO,=,= (2 $ 4 4Wh G GH "I 2#y':::&1133 2,1		%,%7%7%9%9 2,1	%):::#*#5#5#7#7 2,1	%%'' 2GOO,=,= 2 $ 4 4Wh G GH "I 2#y66&1133 2,1	%9=88&~~// 2,1	%9+<<<&~~// 24F'JZJZJ\J\4\4\,1	%9+>>>  '~~// 24HGL\L\L^L^4^4^,1	  	B	 	B Re/+yRRR!!"2H=== $$%6AAA  r   c                 :   |                                 }d}|                                rd}|                                 }|                                rd}t          j                            |g           r(t          j        |                             |d          }net          dd                    ||g                              d                     t          j        D ]}t          j        |         d         |k    r;t          j        |                             |d          }|t          j	        k    r nd}Zt          j        |         d         rWt          j        |         d         |k    r;t          j        |                             |d          }|t          j
        k    r n[d}t          j        |         d	         |k    r:t          j        |                             |d          }|t          j        k    r nd}!t          |           |dk    rd
S |S )  
        verify the semantic relation between the previous 
        to current stemmed word.
        If the current word is related with the previous word, return True.
        The previous word can contain a pointer to the next word. 
        the current can have a pointer to the previous if they ara realated
        @param previous: the previous stemmed word, 
        choosen by the tashkeel process.
        @type previous:stemmedSynWord class 
        @param current: the current stemmed word.
        @type current:stemmedSynWord class 
        @return: return the relation between two words, else False
        @rtype: Unicode or False
        r,      فلانrP   z + r/   subjtransobjaddF)rB   is_proper_nounr   SEM_RELATION_TABLEgetrD   rE   rH   SEM_DERIVATION_TABLErU   rQ   rV   )r   r*   r]   	preoriginr`   	curoriginkeys          r   are_sem_related3z!SemanticAnalyzer.are_sem_related3!  s    ++--	""$$ 	&%I**,,	!!## 	&%I'++Ir:: 	& 3I>BB9bQQHH#fkk9i2H&I&I%Q%QRX%Y%YZZZ 5 & &1#6v>)KK(;C@DDYPRSSH9#444#% 5c:7C &.s3E:iGG(;C@DDYPRSSH9#666#%4S9%@IMM(;C@DDYPRSSH9?22#%hr>>5Or   c                     |                                 }d}|                                rd}|                                 }|                                rd}| j                            ||          S )rd   r,   re   )rB   rj   r   lookup)r   r*   r]   
origin_prer`   
origin_curs         r   rP   z SemanticAnalyzer.are_sem_relatedW  su       ,,..
""$$ 	'&J++--
!!## 	'&J|"":z:::r   c                     |oQ|oO|                                 |                                v o'|                                 |                                v S )a{  
        verify the syntaxic path from the previous 
        to current stemmed word.
        If the current word is related with the previous word, return True.
        The previous word can contain a pointer to the next word. 
        the current can have a pointer to the previous if they ara realated
        @param previous: the previous stemmed word, 
        choosen by the tashkeel process.
        @type previous:stemmedSynWord class 
        @param current: the current stemmed word.
        @type current:stemmedSynWord class 
        @return: return if the two words are related syntaxicly.
        @rtype: boolean
        )r@   get_previousget_nextr   r*   r]   s      r   r>   zSemanticAnalyzer.is_syn_relatedp  s[     ' 7 4 4 6 6678#4#4#6#66	
r   c                    |rM| j                             d|                                t          |                                                     |rM| j                             d|                                t          |                                                     |oQ|oO|                                |                                v o'|                                |                                v S )ar  
        verify the syntaxic path from the previous to current stemmed word.
        If the current word is related with the previous word, return True.
        The previous word can contain a pointer to the next word. 
        the current can have a pointer to the previous if they ara realated
        @param previous: the previous stemmed word, 
        choosen by the tashkeel process.
        @type previous:stemmedSynWord class 
        @param current: the current stemmed word.
        @type current:stemmedSynWord class 
        @return: return if the two words are related syntaxicly.
        @rtype: boolean
        zpr-ordr: %d, pr-next %szcr-ordr: %d, cr-prvs %s)r   rC   r@   rG   get_sem_nextget_sem_previousry   s      r   rA   zSemanticAnalyzer.is_related  s      	nK79K9K9M9MtT\TiTiTkTkOlOlmmm 	pK79J9J9L9LdSZSkSkSmSmNnNnooo' ; 8 8 : ::;8#8#8#:#::	
r   c                 \    t          j        t          j        |          }d |D             }|S )a/  
        Decode objects result from analysis. helps to display result.
        @param stemmed_synwordlistlist: list of  list of StemmedSynWord.
        @type word_result: list of  list of StemmedSynWord
        @return: the list of list of dict to display.
        @rtype: list of  list of dict
        c                     g | ]	}|j         
S r2   )__dict__)r3   xs     r   
<listcomp>z+SemanticAnalyzer.decode.<locals>.<listcomp>  s    444AQZ444r   )	functoolsreduceoperatorconcatr   r#   	flat_lists      r   decodezSemanticAnalyzer.decode  s2     $X_6MNN	44444	r   c           	          d}|D ]X}|dz  }|D ]I}|dz  }|                                 }|                                D ]}|d|d||         dz  }|dz  }J|dz  }Y|d	z  }|S )
z
        display objects result from analysis
        @param stemmed_synwordlistlist: list of  list of StemmedSynWord.
        @type word_result: list of  list of StemmedSynWord
        [z
	[z
		{z
		u'z'  =  u'z', z
		}z
	]z
])get_dictkeys)r   r#   textrlistitemr(   rp   s          r   displayzSemanticAnalyzer.display  s     , 	 	EXD $ $# MMOO"<<>> N NCD333MMDD#XDDr   c                 p   g }g }g }d}|D ]e}|                     |d                                                    |D ]-}|                                r|                     d            n:.|r"|d         dk    r|                     d           n|                     d           g }	|D ]}|                                r|j        }
|D ]}|                                }||
v rv|	                     |                                |                                |                                |                                |
                    |d          g           |                     |	           |}gt          t          |||                    }g }|s"|D ]\  }}}|r|                     |           |}|S )a  
        display objects result from analysis
        @param stemmed_synwordlistlist: list of  list of StemmedSynWord.
        @type word_result: list of  list of StemmedSynWord
        @param all: display all words with tags
        @type all: boolean (default False)
        Nr   BIOi'  )appendget_wordhas_sem_nexthas_sem_previoussem_previousr@   get_vocalizedrB   rl   listzip)r   r#   allwordsemlistwordlisttag_listprevious_rlistr   r   
forms_listsem_prevwordstemorderresulttmp_listwordtagsems                     r   display_semzSemanticAnalyzer.display_sem  s     , 	# 	#E OOE!H--//000 	) 	)$$&& OOC(((E
  ) 3 3OOC((((OOC(((J ; ;((** ;#0H$2 ; ; ( 2 2 4 4 H,,&--t/A/A/C/CXE[E[E]E]!..00(2G2G2I2I&ll5%88/: ; ; ; z***"NNc(Hk::;; 	"( ) )c3 )OOC(((Fr   c                 X    |                      |          }t          j        |           dS )z
        print objects result from analysis
        @param stemmed_synwordlistlist: list of  list of StemmedSynWord.
        @type word_result: list of  list of StemmedSynWord
        N)r   pprintr   s      r   r   zSemanticAnalyzer.pprint  s,     KK 788	i     r   c                    t           j                                        }t          j                                        }|                    |          }|                    |          \  }}|                     |          }|S )z*
        Text Analysis semanticly
        )qalsadianalexAnalexr   anasynSyntaxAnalyzer
check_textr   )r   r   analyzeranasyntr   
synodelists         r   analyze_textzSemanticAnalyzer.analyze_text  si     ^**,,_3355((..&v66
LL((r   N)F)r   r   )r   
__module____qualname____doc__r   r   r   r!   rq   rP   r>   rA   r   r   r   r   r   r2   r   r   r   r   *   s        2 2 2 2< < < 5' 5' 5'p EF^! ^! ^! ^!B5 5 5l; ; ;2
 
 
(
 
 
0
 
 
  $1 1 1 1d	! 	! 	!
 
 
 
 
r   r   c                     d} ddl }g }|j                                        }t          j                                        }t                      }|                    |           }|                    |          \  }}|                    |          }|	                    |          }t          |                    d                     dS )z
    main test
    u1   يعبد الله منذ أن تطلع الشمسr   Nr/   )qalsadi.analexr   r   r   r   r   r   r   r   r   rD   rH   )r   r   r   r   r   r-   r   text2displays           r   mainlyr     s     BDF&&((H..00G!!F""4((F"??622FJ~~f%%F OOF++L	,

f
%
%&&&&&r   __main__)r   r   r   r   syspathr   r,   r   r   r   r   sylajone.anasynr   sylajone.syn_const	syn_constrI   sylajone.cacher   rC   PrimatePredicateMansoubRelationPrimateMansoubPredicateRelationDescribedAdjectiveRelationPrimatePredicateRelationrL   r   r   r   r2   r   r   <module>r      sN          



         % % % % % %                               
 33.,	 X X X X X X X Xt' ' '( 
FHHHHH r   