B
    em#                 @   s  d dl Z d dlZd dlmZ d dlZd dlmZmZmZmZm	Z	m
Z
 d dlmZ d dlZd dlmZ d dlmZ edZeedd Zeed	d Zered
dkrd dlmZ nd dlmZ e ZeededdZeddd Zeddd Zeddd Zdd Zeddd Zeddd Zed d!d" Z ed#d$d% Z!d&d' Z"d(d) Z#ej$ed*ed+ed,ed-d.Z%e&d/kre'd0(e& e) Z*e*+e% e*+ed1 ee*d2d3d4d4d5 n.e'd0(e& e) Z*e*+e% e*+ed1 dS )6    N)dotenv_values)runpostgetrequestresponsetemplate)WordsApi)cors_plugin)OpenAiTranslatorz.envENABLE_PREDICTIONSDISABLE_LOGGINGZAPP_ENVZlocal)SmartDictionaryZWORDS_API_PATHZWORDS_EXTRA_PATH)pathZ
path_extraz
/translatec               C   s   t dS )N	translate)r    r   r   =/home/dictionary/smart-dictionary-consec/app/gogyup/server.pyr      s    r   z/api/predictc              C   sD   t j} | d }| d }| d }t| d}t||||}t|S )NcontextZtargetPositionZcandidateDefinitionssort)r   jsonboolr   smartpredictjson_response)datar   target_positioncandidate_definitionsZ	sort_defsZpredictionsr   r   r   predict_definitions#   s    r   z/api/definec          
      s  t jj}|dr|dd }t jj}t jj}tt jjp:d}d}tt jjpNd}t	odtt jj
pbd tpxtt jjpvd}ytt jj}W n   d}Y nX |dkrt jd}	t| |||||| |		 |s|std	S  r|std
S |dkr|stdS |s|stdS |rt	rxttd|D ]\}
}|d}|dkrj| | kr.| }P q.| |krtdS | |kr.|d}|s|}n| | krtdS P q.W tdS n|stdS t|||} rt|dkrdd |D }dd |D }d}g }xVttd|D ]B\}
}|d}|| | | kr6| |kr6|
}q6W t
|||||} fdd|D }d|t|t||d}t|S )a~  
    Either a word or a context (or both) must be provided.
    If no context is given, then an unsorted list of definitions will be returned.
    If no word is provided, then a context and offset must be provided.
    If a starting position is provided, then a context must also be provided.
    If a part of speech is provided then results will be filtered by part of speech.
    z'sNr      
   ZRefererz6Either a word or a context (or both) must be provided.z*A context is required to make predictions.zIIf a starting position is provided, then a context must also be provided.zNIf no word is provided, then a context and starting position must be provided.z\w+['\w]*\bzWord not found in context.zWord not found in contextzCIf no word is provided, then a context and offset must be provided.c             S   s   g | ]}|d  |d fqS )lemma
definitionr   ).0rr   r   r   
<listcomp>   s    zdefine.<locals>.<listcomp>c             S   s   g | ]}|d  qS )partOfSpeechr   )r$   r%   r   r   r   r&      s    c          	      s>   g | ]6}d g|d |d |d  r,| dnddgdqS )ZwordsApir"   r'   r#   probabilityN)r#   r(   )ZdatasetsZheadwordpart_of_speechZsenses)r   )r$   r%   )r   r   r   r&      s      )statusoffsetcountZtotalresults)r   querywordendswithr   posintstartr   r   r   r   Znologlimitheadersr   log_definition_request
json_error	enumeratereZfinditergrouplower	words_apilookuplenappendr   r   )pymydbtarget_wordr   r)   r4   r,   sort_Zno_logr5   
ip_addressimatchr0   r.   r   Zparts_of_speechr   Zcontext_listoutputr   )r   r   define.   s    	







 

rH   c	       
      C   s$   d}	|  |	||||||||f d S )NzINSERT INTO `DefinitionRequests` (`target_word`, `context`, `part_of_speech`, `start`, `offset`, `sort`, `predict`, `ip_address`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s))execute)
rA   rB   r   r)   r4   r,   rC   r   rD   Zsqlr   r   r   r7      s    r7   z/api/translatec              C   s   t jj} t jj}t jj}| r|r|rt }|| || |j}|rd|krt|d t	rt
|d dkr|jd }x2|D ]*}d|krxt|d trx|d  |d< qxW t|jd S tdS ntdS d S )Nchoicesr   textzTranslation failedz,Required parameters: word, context, language)r   r/   r0   r   languager   r   r   
isinstancelistr?   strstripr   r8   )r0   r   rL   Z
translatorZresrJ   Zchoicer   r   r   r      s    *


z/api/extra/define/<lemma>c             C   s   t dS )NzNot implemented)r8   )r"   rA   r   r   r   define_extra   s    rQ   z(/api/extra/define_extra_by_id/<lemma_id>c             C   s   t dS )NzNot implemented)r8   )Zlemma_idrA   r   r   r   define_extra_by_id   s    rR   z/api/pokemon/<pokemon_id>c             C   s.   t | } |d| f | }t| t|S )Nz#SELECT * from pokemon where id="%s")r3   rI   Zfetchallprintr   )Z
pokemon_idrA   Zrowsr   r   r   pokemon   s
    rT   c             C   s   dt jd< dt _t| S )Nzapplication/jsonzContent-Typer*   )r   r6   r+   r   dumps)r   r   r   r   r      s    
r   c             C   s,   t | trd| i} dtjd< dt_t| S )Nerrorzapplication/jsonzContent-Typei  )rM   rO   r   r6   r+   r   rU   )r   r   r   r   r8      s
    

r8   ZDB_HOSTZDB_USERZDB_PASSZDB_NAME)ZdbhostZdbuserZdbpassZdbname__main__z	Name = {}*Z	localhosti  T)ZhostZportdebugZreloader),r   r:   Zdotenvr   Zbottler   r   r   r   r   r   Zgogyup.words_apir	   Zbottle_pymysqlZbottle_cors_pluginr
   Zgogyup.openai_toolsr   Zconfigr3   r   r   Zgogyup.mocksr   Zgogyup.smart_dictionaryr   r=   r   r   rH   r7   rQ   rR   rT   r   r8   ZPluginZplugin__name__rS   formatZdefault_appapplicationZinstallr   r   r   r   <module>   sT    u	


