
    MgG"                         S SK J r JrJr  S SKJr  S SKJrJrJr  S SK	r	S SK
r
S SKr\" SS9\" S5      \ " S5      S	 5       5       5       rS
 rS rS rS rg)    )pluginrequirealias)
Translator)	LANGCODES	LANGUAGESSPECIAL_CASESNT)networktrans	translatec                    US:w  a  [         R                  " UR                  5       5      nSnSnSnSn[        [	        U5      5       H  nX'   nUnU[
        ;   d  M  Un  O   [        US-   [	        U5      5       H  nX'   nUnU[
        ;   d  M  Un  O   U(       aH  U(       aA  US:  a  SR                  X$S-   S 5      n	OSR                  USUS-
   5      n	[        XXi5        gU R                  S5        [        U 5        g[        U 5        g)a   
Translates from one language to another and allows input to be somewhat natural.

Usage:

'Jarvis, please translate, from English to French, Hello, how are you?'

OR

'Jarvis, could you translate Hello, how are you? from English to French for me please?'
 r   N       zZ
Sorry, I couldn't understand your translation request. Please enter the request in steps.)
nltkword_tokenizelowerrangelenr   joinperformTranslationsaydefault)
jarvisswords
currentPosfinalPossrcsdesiwordtexs
             ?/Users/admin/workspace/ai/Jarvis/jarviscli/plugins/translate.pyr   r   	   s   " 	Bw""1779-
 s5z"A8DJ 	! # zA~s5z2A8DH	! 3 DQhhu\]34hhu_j1n56vS6 JJtuFO    c                 ~    UnX2S-   :  a!  X   S:X  d  X   S:X  a  U$ US-  nX2S-   :  a  M!  U R                  S5        U$ )a  
    Checks for new line characters '
 or 
' that would mean the end of sentence.

    This work better that trying to find dots (.) because it can break semantic continuity
    with cases like " J. Doe, e.g. ". Returns the index where end of sentence is found.
    i  
z
r   z_
Could not find a new line character in over 4000 characters. Not able to break file correctly.)r   )r   tstartr"   s       r%   sentenceEndr+   G   sR     	A
dl
44<146>H	Q	 dl

 JJqrHr&   c                     / n[        U5      S:  aA  [        XS5      nUR                  USUS-    5        X[        U5       n[        U5      S:  a  MA  UR                  US[        U5       5        U$ )zj
Splits the file text (larger than 10k characters) in smaller parts
returns a list with the smaller parts
'  ip  r   r   )r   r+   append)r   r)   r   idxs       r%   textSplitterr0   X   sm    
 	A
a&5.&T*	1S1W#a&M a&5. HHQqQ[Hr&   c                    U R                  S5        U R                  5       R                  5       R                  5       nU[        ;  a  U[
        ;  a  U[        ;  a  US:X  d}  U[
        ;   a
  [
        U   nOCU[        ;   a
  [        U   nO/U R                  S5        U R                  5       R                  5       nU[        ;  a  U[
        ;  a  U[        ;  a  US:X  d  M}  U R                  S5        U R                  5       R                  5       R                  5       nU[        ;  a  U[
        ;  a  U[        ;  aw  U[
        ;   a
  [
        U   nOCU[        ;   a
  [        U   nO/U R                  S5        U R                  5       R                  5       nU[        ;  a  U[
        ;  a  U[        ;  a  Mw  U R                  S5        U R                  5       n[        R                  " [        XX#5      5        g)z
Default function that is called when translate is entered alone or
when input is not understood when translate is entered with additional parameters
z
Enter source language autoz$
Invalid source language
Enter againz
Enter destination language z)
Invalid destination language
Enter againz
Enter text or path for fileN)
r   inputr   stripr   r	   r   asynciorunr   )r   r    r!   r$   s       r%   r   r   f   s    JJ)*<<>!'')D 	!%	!46>=  &DYT?DJJ?@<<>'')D 	!%	!46> JJ./
,,.


 
&
&
(C y }$y -$CIC.CJJDE,,.&&(C y }$y  JJ./
,,.CKK"6:;r&   c           	      j  #    SnSn[        5        ISh  vN n[        R                  R                  U5      (       Ga|  U[	        U5      S-
  [	        U5       S:X  d  U R                  S5        SnGO[        US5       nUR                  5       nSSS5        [	        U5      S:  a  [        X5      nSn[        S	S
SS9n	[        [	        U5      5       H8  n
UR                  X   XS9I Sh  vN nU	R                  UR                  5        M:     U	R                  5         U R                  S[        R                  " 5       -   S-   S-   5        OUR                  X1US9I Sh  vN nSR                  5       R!                  UR"                  UR$                  UR&                  UR                  S9n[)        SU-   5        OyUR                  X1US9I Sh  vN nSR                  5       R!                  UR"                  UR$                  UR&                  UR                  UR*                  S9n[)        SU-   5        U(       d  U(       d  U R                  S5        U R-                  5       R/                  5       R                  5       nUS:X  aR  [        S	S
SS9 nUR                  W5        SSS5        U R                  S[        R                  " 5       -   S-   S-   5        SSS5      ISh  vN   g GN! , (       d  f       GNc= f GN GN GN5! , (       d  f       Nj= f N4! , ISh  vN  (       d  f       g= f7f)a  
Function that performs the actual translation give the user input
There are 3 cases :
1. input is text.

2. input is a small txt file ( less than 10k characters )
able to be translated with one call to the library.

3. Large txt files that need to be broken up to smaller chunks.

In cases 1 and 2 the output is printed in the screen
with the option to save the output to a file at the current working directory.
while in case 3 the output is too large and would clutter the terminal
window, so it is only saved to the file by default.
FN   txtzL
This is a valid path but not for a .txt file. Only txt files are supported.Trr-   z./translated.txtwzutf-8)encoding)srcdestz
The output was saved at : \ztranslated.txtzz
                    [{src}] {original}
                       ->
                    [{dest}] {text}
                    )r=   r>   originaltextr(   zj
        [{src}] {original}
           ->
        [{dest}] {text}
        [pron.] {pronunciation}
        )r=   r>   r@   rA   pronunciationz%
 Do you want to save result to file?yes)r   ospathisfiler   r   openreadr0   r   r   writerA   closegetcwdr4   formatr=   r>   originprintrB   r3   r   )r   r    r!   r$   saveerror
translatorfilesubTextfr"   resultanss                r%   r   r      s      DE||z77>>#s3x!|SX.%7

jk#s^t))+C $
 s8e#*67GD/wGA"3w<0'1';';GJD';'[![, 1 GGIJJ=		KdRUeef $.#7#7C#7#PPF 6::FKKRXR_R_,2KK !' !9	  $-( &//C/HHF
 EGFFvzzfmm &6;O;O  Q  $-  EJJ?@,,.&&(..0Ce|,cGDJJv& E

9BIIKG$NQaabg || $^ "\ Q I EDc |||s   L3K(L3A%L>K+AL*K=+A4LL  A.LLCLL"5LL3"L#L3+
K:	5	L LL
L	LL3L0L" L0,L3)r   r   r   googletransr   googletrans.constantsr   r   r	   r   r5   rD   r   r+   r0   r   r    r&   r%   <module>rZ      sg    ) ) " E E   	 	w8   8v"'<TEcr&   