
    co                     t    d Z ddlZddlZddlmZmZmZ  ej        e	          Z
 G d dej                  ZdS )a   Random Projections (also known as Random Indexing).

For theoretical background on Random Projections, see [1]_.


Examples
--------
.. sourcecode:: pycon

    >>> from gensim.models import RpModel
    >>> from gensim.corpora import Dictionary
    >>> from gensim.test.utils import common_texts, temporary_file
    >>>
    >>> dictionary = Dictionary(common_texts)  # fit dictionary
    >>> corpus = [dictionary.doc2bow(text) for text in common_texts]  # convert texts to BoW format
    >>>
    >>> model = RpModel(corpus, id2word=dictionary)  # fit model
    >>> result = model[corpus[3]]  # apply model to document, result is vector in BoW format
    >>>
    >>> with temporary_file("model_file") as fname:
    ...     model.save(fname)  # save model to file
    ...     loaded_model = RpModel.load(fname)  # load model


References
----------
.. [1] Kanerva et al., 2000, Random indexing of text samples for Latent Semantic Analysis,
       https://cloudfront.escholarship.org/dist/prd/content/qt5644k0w6/qt5644k0w6.pdf

    N)
interfacesmatutilsutilsc                   .    e Zd ZddZd Zd Zd Zd ZdS )	RpModelN,  c                     || _         || _        |1|                     |           |                     dd|             dS dS )a  

        Parameters
        ----------
        corpus : iterable of iterable of (int, int)
            Input corpus.

        id2word : {dict of (int, str), :class:`~gensim.corpora.dictionary.Dictionary`}, optional
            Mapping `token_id` -> `token`, will be determine from corpus if `id2word == None`.

        num_topics : int, optional
            Number of topics.

        Ncreatedzcreated )msg)id2word
num_topics
initializeadd_lifecycle_event)selfcorpusr   r   s       5lib/python3.11/site-packages/gensim/models/rpmodel.py__init__zRpModel.__init__3   sa     $ 	GOOF###$$Y4Et4E4E$FFFFF	G 	G    c                 @    | j         j        d| j        d| j        dS )Nz<num_terms=z, num_topics=>)	__class____name__	num_termsr   )r   s    r   __str__zRpModel.__str__H   s*    48N4K4K4KT^^^]a]l]l]lmmr   c                    | j         Mt                              d           t          j        |          | _         t          | j                   | _        n+| j         rdt          | j                   z   | _        nd| _        | j        | j        f}t                              dt          |                     ddt          j                            dd|          z  z
  }t          j        |t          j                  | _        dS )	zInitialize the random projection matrix.

        Parameters
        ----------
        corpus : iterable of iterable of (int, int)
          Input corpus.

        NzHno word id mapping provided; initializing from corpus, assuming identity   r   zconstructing %s random matrix   g      ?dtype)r   loggerinfor   dict_from_corpuslenr   maxr   strnprandombinomialasfortranarrayfloat32
projection)r   r   shaperandmats       r   r   zRpModel.initializeK   s     < 	KKbccc 1&99DL ..DNN\ 	T\!2!22DNNDN/3SZZ@@@ a"),,QU;;;;+G2:FFFr   c                    t          j        |          \  }}|r|                     |          S t          | dd          r&d| _        | j                            d          | _        t          j        || j	                  
                    | j	        d          t          j        | j                  z  }t          j        |t          j                  }t          j        | j        |          }d t#          |j                  D             S )a4  Get random-projection representation of the input vector or corpus.

        Parameters
        ----------
        bow : {list of (int, int), iterable of list of (int, int)}
            Input document or corpus.

        Returns
        -------
        list of (int, float)
            if `bow` is document OR
        :class:`~gensim.interfaces.TransformedCorpus`
            if `bow` is corpus.

        Examples
        ----------
        .. sourcecode:: pycon

            >>> from gensim.models import RpModel
            >>> from gensim.corpora import Dictionary
            >>> from gensim.test.utils import common_texts
            >>>
            >>> dictionary = Dictionary(common_texts)  # fit dictionary
            >>> corpus = [dictionary.doc2bow(text) for text in common_texts]  # convert texts to BoW format
            >>>
            >>> model = RpModel(corpus, id2word=dictionary)  # fit model
            >>>
            >>> # apply model to document, result is vector in BoW format, i.e. [(1, 0.3), ... ]
            >>> result = model[corpus[0]]

        freshly_loadedFFr   r   c                     g | ]?\  }}t          j        |          t          j        |d           .|t          |          f@S )g        )r&   isfiniteallclosefloat).0topicid
topicvalues      r   
<listcomp>z'RpModel.__getitem__.<locals>.<listcomp>   s]     
 
 
-@Wj{:&&
/1{:s/K/K
eJ''(
 
 
r   )r   	is_corpus_applygetattrr/   r+   copyr   sparse2fullr   reshaper&   sqrtr   r)   r*   dot	enumerateflat)r   bowr9   vec
topic_dists        r   __getitem__zRpModel.__getitem__i   s    B --	3 	$;;s###4)511 	8 #(D"o22377DO"377??PQRRUWU\]a]lUmUmm2:666VDOS11

 
DMjoD^D^
 
 
 	
r   c                 "    || _         d| _        dS )zSets the internal state and updates freshly_loaded to True, called when unpicked.

        Parameters
        ----------
        state : dict
           State of the class.

        TN)__dict__r/   )r   states     r   __setstate__zRpModel.__setstate__   s     "r   )Nr   )r   
__module____qualname__r   r   r   rF   rJ    r   r   r   r   1   so        G G G G*n n nG G G<1
 1
 1
f
# 
# 
# 
# 
#r   r   )__doc__loggingnumpyr&   gensimr   r   r   	getLoggerr   r    TransformationABCr   rM   r   r   <module>rT      s    >      . . . . . . . . . . 
	8	$	$u# u# u# u# u#j* u# u# u# u# u#r   