
    ?h                     L    d Z ddlmZ ddlmZ ddlmZ ddlm	Z	  G d d      Z
y)	z!
Contextionary class definition.
    )ConnectionError)
Connection)UnexpectedStatusCodeException)_decode_json_response_dictc            	       F    e Zd ZdZdefdZddedededdfd	Zdede	fd
Z
y)Contextionaryz
    Contextionary class used to add extend the Weaviate contextionary module
    or to get vector/s of a specific concept.
    
connectionc                     || _         y)z
        Initialize a Contextionary class instance.

        Parameters
        ----------
        connection : weaviate.connect.Connection
            Connection object to an active and running Weaviate instance.
        N)_connection)selfr	   s     g/home/chris/cleankitchens-env/lib/python3.12/site-packages/weaviate/contextionary/crud_contextionary.py__init__zContextionary.__init__   s     &    concept
definitionweightreturnNc                    t        |t              st        d      t        |t              st        d      t        |t              st        d      |dkD  s|dk  rt	        d      |||d}	 | j
                  j                  d|	      }|j                  dk(  ryt        d|      # t        $ r}t        d
      |d}~ww xY w)a^  
        Extend the text2vec-contextionary with new concepts

        Parameters
        ----------
        concept : str
            The new concept that should be added that is not in the Weaviate
            or needs to be updated, e.g. an abbreviation.
        definition : str
            The definition of the new concept.
        weight : float, optional
            The weight of the new definition compared to the old one,
            must be in-between the interval [0.0; 1.0], by default 1.0

        Examples
        --------
        >>> client.contextionary.extend(
        ...     concept = 'palantir',
        ...     definition = 'spherical stone objects used for communication in Middle-earth'
        ... )


        Raises
        ------
        TypeError
            If an argument is not of an appropriate type.
        ValueError
            If 'weight' is outside the interval [0.0; 1.0].
        requests.ConnectionError
            If text2vec-contextionary could not be extended.
        weaviate.UnexpectedStatusCodeException
            If the network connection to weaviate fails.
        zConcept must be stringzDefinition must be stringzWeight must be float      ?g        z)Weight out of limits 0.0 <= weight <= 1.0)r   r   r   z*/modules/text2vec-contextionary/extensions)pathweaviate_objectz-text2vec-contextionary could not be extended.N   zExtend text2vec-contextionary)

isinstancestr	TypeErrorfloat
ValueErrorr   postRequestsConnectionErrorstatus_coder   )r   r   r   r   	extensionresponseconn_errs          r   extendzContextionary.extend   s    F '3'455*c*788&%(233C<6C<HII 'zVT		'',,A ) - H 3&+,KXVV ' 	)?	s   .B' '	C0B<<Cc                     d|z   }	 | j                   j                  |      }t        |d      }|J |S # t        $ r}t        d      |d}~ww xY w)a  
        Retrieves the vector representation of the given concept.

        Parameters
        ----------
        concept : str
            Concept for which the vector should be retrieved.
            May be camelCase for word combinations.

        Examples
        --------
        >>> client.contextionary.get_concept_vector('king')
        {
            "individualWords": [
                {
                "info": {
                    "nearestNeighbors": [
                    {
                        "word": "king"
                    },
                    {
                        "distance": 5.7498446,
                        "word": "kings"
                    },
                    ...,
                    {
                        "distance": 6.1396513,
                        "word": "queen"
                    }
                    ],
                    "vector": [
                    -0.68988,
                    ...,
                    -0.561865
                    ]
                },
                "present": true,
                "word": "king"
                }
            ]
        }

        Returns
        -------
        dict
            A dictionary containing info and the vector/s of the concept.
            The vector might be empty if the text2vec-contextionary does not contain it.

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        z)/modules/text2vec-contextionary/concepts/)r   ztext2vec-contextionary vectorNz0text2vec-contextionary vector was not retrieved.)r   getr   r   )r   r   r   r"   resr#   s         r   get_concept_vectorz Contextionary.get_concept_vectorZ   sp    r ;WD		''+++6H -X7VWC?"?J ' 	)B	s   5 	AA

A)r   )__name__
__module____qualname____doc__r   r   r   r   r$   dictr(    r   r   r   r      sP    

&: 
&;Wc ;Ws ;WE ;WD ;WzC# C$ Cr   r   N)r,   requests.exceptionsr   r   weaviate.connectr   weaviate.exceptionsr   weaviate.utilr   r   r.   r   r   <module>r3      s%    K ' = 4R Rr   