ó
Û=6Vc           @   s>   d  d l  Z  d  d l Z d  d l Z d e f d „  ƒ  YZ d S(   iÿÿÿÿNt   CorticalClientc           B   s­   e  Z d  Z d d d „ Z d „  Z d d e d „ Z d d d d e d	 „ Z	 d
 „  Z
 d „  Z d d e d „ Z d d „ Z d d d d d e d „ Z d „  Z d „  Z RS(   s  
    This class implements some basic access to Cortical.io's retina.
    Please have a look at api.cortical.io to see the full array of REST endpoints, and input and output types.
    The naming conventions of the methods are heavily related to the names of the rest endpoints.
    s   http://s_api.cortical.io:80t   en_synonymousc         C   sJ   d } | d k r+ t d t | ƒ ƒ ‚ n  | |  _ | |  _ | |  _ d S(   sG   
        retinaName: one of: 'en_synonymous', 'en_associative'
        s-   http://www.cortical.io/developers_apikey.htmlsJ   You must pass an apiKey when instantiating the APIClient. Get one here: %sN(   t   Nonet	   Exceptiont   strt   apiKeyt	   apiServert
   retinaName(   t   selfR   R   R   t   apiWeb(    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   __init__   s    		c         C   sh   d d j  g  | D] } t | ƒ ^ q ƒ } |  j | d d d | d t ƒ} g  | D] } | d ^ qT S(   s/   sdr is a numpy.ndarray. Returns a list of termss   { "positions" : [%s] }t   ,t   start_indexi    t   max_resultst   get_fingerprintt   term(   t   joinR   t   expressions_similar_termst   True(   R   t   sdrt   howManyt   valt
   expressiont   fpListt   d(    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   getSimilarTermsForSDR   s    ,!i    i
   c         C   sp   d |  j  t j | ƒ | | | f } d } y |  j | d d ƒ } Wn% t k
 rk } d | d GH| GHn X| S(   s$   Get a Term object for the input termsS   /rest/terms?retina_name=%s&term=%s&start_index=%i&max_results=%i&get_fingerprint=%st   GETs   Failed calling with term s   
N(   R   t   urllibt   quoteR   t   _callAPIR   (   R   R   R   R   R   t   resourcePatht   rest   e(    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   terms%   s    "	t    c   
      C   s   d |  j  t j | ƒ | | | f } | r< | d | 7} n  t | ƒ t k r_ | d | 7} n  d } y |  j | d d ƒ } Wn t k
 r˜ }	 |	 GHn X| S(   s   Get similar terms for a termsa   /rest/terms/similar_terms?retina_name=%s&term=%s&start_index=%i&max_results=%i&get_fingerprint=%ss   &pos_type=%ss   &context_id=%iR   N(   R   R   R   t   typet   intR   R   R   (
   R   R   t
   context_idR   R   t   pos_typeR   R   R   R    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   terms_similar_terms1   s    "	c         C   sK   d |  j  } d } y |  j | d | ƒ } Wn t k
 rF } | GHn X| S(   s   Get SDR for a texts   /rest/text?retina_name=%st   POSTN(   R   R   R   R   (   R   t	   inputTextR   R   R    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   text@   s    	c         C   sK   d |  j  } d } y |  j | d | ƒ } Wn t k
 rF } | GHn X| S(   s<    Calls the getKeyords for text endpoint of the Cortical API.s"   /rest/text/keywords?retina_name=%sR(   N(   R   R   R   R   (   R   R)   R   R   R    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   text_keywordsJ   s    	c         C   sW   d |  j  | | | f } d } y |  j | d | ƒ } Wn t k
 rR } | GHn X| S(   s<    Calls the get slices for text endpoint of the Cortical API.sQ   /rest/text/slices?retina_name=%s&start_index=%i&max_results=%i&get_fingerprint=%sR(   N(   R   R   R   R   (   R   R)   t
   startIndext
   maxResultst   getFingerprintR   t   slicesR    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   text_slicesT   s    	c         C   sk   d |  j  } | r- | d t j | ƒ 7} n  d } y |  j | d | ƒ } Wn t k
 rf } | GHn X| S(   s#   Get sentences and tokens for a texts"   /rest/text/tokenize?retina_name=%ss   &POStags=%sR(   N(   R   R   R   R   R   R   (   R   R)   t   posTagsR   R   R    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   text_tokenize`   s    	g      ð?c         C   s”   d |  j  | | | | f } | r3 | d | 7} n  t | ƒ t k rV | d | 7} n  d }	 y |  j | d | ƒ }	 Wn t k
 r }
 |
 GHn X|	 S(   s#   Get similar terms for an expressionsk   /rest/expressions/similar_terms?retina_name=%s&start_index=%i&max_results=%i&sparsity=%f&get_fingerprint=%ss   &pos_type=%ss   &context_id=%iR(   N(   R   R#   R$   R   R   R   (   R   R   R%   R   R   R&   t   sparsityR   R   R   R    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyR   m   s    	c         C   s‚   d |  j  } d } | rD | rD d t | ƒ d t | ƒ d } n  d  } y |  j | d | ƒ } Wn t k
 r} } | GHn X| S(   Ns   /rest/compare?retina_name=%sR"   s   [ { "positions":s   } , { "positions":s    } ]R(   (   R   R   R   R   R   (   R   t   fingerprint1t   fingerprint2R   R   R   R    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   compare|   s    %	c   
      C   sè   |  j  | } | } i  } |  j | d <d | d <| d k r? n6 | d k ra | ru d | d <qu n t d	 | d
 ƒ ‚ t j d | j d ƒ d | d | ƒ } t j | ƒ } | j ƒ  }	 y t j	 |	 ƒ } Wn t
 k
 rã d  } n X| S(   Ns   api-keys$   58baa020-97b4-11e3-82ec-614a46604ad2R   R(   t   PUTt   DELETEs   application/jsons   Content-types   Method s    is not recognized.t   urls   utf-8t   headerst   data(   s   GET(   s   POSTR7   R8   (   R   R   R   t   urllib2t   Requestt   encodet   urlopent   readt   jsont   loadst
   ValueErrorR   (
   R   R   t   methodt   postDataR9   R;   R:   t   requestt   responset   string(    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyR   Š   s&    
'
N(   t   __name__t
   __module__t   __doc__R
   R   R   R!   R   t   FalseR'   R*   R+   R0   R2   R   R6   R   (    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyR       s   	
	
	
	(   RA   R   R<   t   objectR    (    (    (    sT   /Users/avi/Dropbox (Personal)/programming/banker.ai/corticaltest/CortiYelp/client.pyt   <module>   s   