
    c?                     0   d Z ddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZmZ ddlmZ ddlmZ  ej        e          ZdZdZdd
ZddZddZ G d dej                  Z G d dej        ej                  ZddZdddej         ddfdZ!dS )av	  Module for `Latent Semantic Analysis (aka Latent Semantic Indexing)
<https://en.wikipedia.org/wiki/Latent_semantic_analysis#Latent_semantic_indexing>`_.

Implements fast truncated SVD (Singular Value Decomposition). The SVD decomposition can be updated with new observations
at any time, for an online, incremental, memory-efficient training.

This module actually contains several algorithms for decomposition of large corpora, a
combination of which effectively and transparently allows building LSI models for:

* corpora much larger than RAM: only constant memory is needed, independent of
  the corpus size
* corpora that are streamed: documents are only accessed sequentially, no
  random access
* corpora that cannot be even temporarily stored: each document can only be
  seen once and must be processed immediately (one-pass algorithm)
* distributed computing for very large corpora, making use of a cluster of
  machines

Wall-clock `performance on the English Wikipedia <https://radimrehurek.com/gensim/wiki.html>`_
(2G corpus positions, 3.2M documents, 100K features, 0.5G non-zero entries in the final TF-IDF matrix),
requesting the top 400 LSI factors:

====================================================== ============ ==================
 algorithm                                             serial       distributed
====================================================== ============ ==================
 one-pass merge algorithm                              5h14m        1h41m
 multi-pass stochastic algo (with 2 power iterations)  5h39m        N/A [1]_
====================================================== ============ ==================


*serial* = Core 2 Duo MacBook Pro 2.53Ghz, 4GB RAM, libVec

*distributed* = cluster of four logical nodes on three physical machines, each
with dual core Xeon 2.0GHz, 4GB RAM, ATLAS


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

    >>> from gensim.test.utils import common_dictionary, common_corpus
    >>> from gensim.models import LsiModel
    >>>
    >>> model = LsiModel(common_corpus, id2word=common_dictionary)
    >>> vectorized_corpus = model[common_corpus]  # vectorize input copus in BoW format


.. [1] The stochastic algo could be distributed too, but most time is already spent
   reading/decompressing the input from disk in its 4 passes. The extra network
   traffic due to data distribution across cluster nodes would likely make it
   *slower*.

    N)sparsetools)
interfacesmatutilsutils)	basemodel)is_emptyd      MbP?c                 p   t          j        dt          j        | t          j        |           z            z
            }dt	          t          j        |t          |d|z            k              d                   z   }t          ||          }t                              d|d||dz
           z             |S )a  Find how many factors should be kept to avoid storing spurious (tiny, numerically unstable) values.

    Parameters
    ----------
    s : list of float
        Eigenvalues of the original matrix.
    k : int
        Maximum desired rank (number of factors)
    discard: float
        Percentage of the spectrum's energy to be discarded.

    Returns
    -------
    int
        Rank (number of factors) of the reduced matrix.


          ?   r   z9keeping %i factors (discarding %.3f%% of energy spectrum)r	   )	npabscumsumsumlenwhereminloggerinfo)skdiscardrel_spectrumsmalls        6lib/python3.11/site-packages/gensim/models/lsimodel.pyclip_spectrumr   R   s    ( 6#	!bfQii- 8 8899LBH\Cq,A,AABB1EFFFEAuA
KKKQPSVbcdghchViPijjjH     c                     | j         j        s5t                              d| j        |           t          j        |           } | S )a/  Get an array laid out in Fortran order in memory.

    Parameters
    ----------
    a : numpy.ndarray
        Input array.
    name : str, optional
        Array name, used only for logging purposes.

    Returns
    -------
    np.ndarray
        The input `a` in Fortran, or column-major order.

    z'converting %s array %s to FORTRAN order)flagsf_contiguousr   debugshaper   asfortranarrayanames     r   asfarrayr*   n   s?      7 !>NNNa  Hr   c                     | j         j        s5t                              d| j        |           t          j        |           } | S )a;  Return a contiguous array in memory (C order).

    Parameters
    ----------
    a : numpy.ndarray
        Input array.
    name : str, optional
        Array name, used for logging purposes.

    Returns
    -------
    np.ndarray
        Contiguous array (row-major order) of same shape and content as `a`.

    z!converting %s array %s to C order)r"   
contiguousr   r$   r%   r   ascontiguousarrayr'   s     r   ascarrayr.      s?      7 $8!'4HHH ##Hr   c                   >    e Zd ZdZddeeej        dfdZd Z	ddZ
dS )	
Projectiona  Low dimensional projection of a term-document matrix.

    This is the class taking care of the 'core math': interfacing with corpora, splitting large corpora into chunks
    and merging them etc. This done through the higher-level :class:`~gensim.models.lsimodel.LsiModel` class.

    Notes
    -----
    The projection can be later updated by merging it with another :class:`~gensim.models.lsimodel.Projection`
    via  :meth:`~gensim.models.lsimodel.Projection.merge`. This is how incremental training actually happens.

    NFc	           
         ||c| _         | _        || _        || _        || _        |?|s4t          ||t          j        || j        | j        || j                  \  }	}
n	 ddl}n# t          $ r t          d          w xY wt                              dt          |j                             t          j                            |          st#          j        |          }|                    ||dz             \  }}
}|j        }	~~t)          |
dz  | j                  }|	ddd|f                                         | _        |
d|                                         | _        dS d\  | _        | _        dS )	a/  Construct the (U, S) projection from a corpus.

        Parameters
        ----------
        m : int
            Number of features (terms) in the corpus.
        k : int
            Desired rank of the decomposed matrix.
        docs : {iterable of list of (int, float), scipy.sparse.csc}
            Corpus in BoW format or as sparse matrix.
        use_svdlibc : bool, optional
            If True - will use `sparsesvd library <https://pypi.python.org/pypi/sparsesvd/>`_,
            otherwise - our own version will be used.
        power_iters: int, optional
            Number of power iteration steps to be used. Tune to improve accuracy.
        extra_dims : int, optional
            Extra samples to be used besides the rank `k`. Tune to improve accuracy.
        dtype : numpy.dtype, optional
            Enforces a type for elements of the decomposed matrix.
        random_seed: {None, int}, optional
            Random seed used to initialize the pseudo-random number generator,
            a local instance of numpy.random.RandomState instance.

        N)	chunksize	num_termspower_iters
extra_dimsdtyperandom_seedr   zH`sparsesvd` module requested but not found; run `easy_install sparsesvd`z!computing sparse SVD of %s matrix   r
   NN)mr   r4   r5   r7   stochastic_svdsysmaxsize	sparsesvdImportErrorr   r   strr%   scipysparseissparser   
corpus2cscTr   copyur   )selfr:   r   docsuse_svdlibcr4   r5   r6   r7   rG   r   r>   utvts                 r   __init__zProjection.__init__   s   8 A&$& 	(  2%!s{T-=#eIY[ [ [11
r$$$$$" r r r%&pqqqr?TZQQQ|,,T22 5#.t44D%//a"f==	ArD!!q&$&11qqq"1"uX]]__DFrrUZZ\\DFFF'NDFDFFFs   A$ $A>c                 \    t          | j        | j        | j        | j        | j                  S )zGet an empty Projection with the same parameters as the current object.

        Returns
        -------
        :class:`~gensim.models.lsimodel.Projection`
            An empty copy (without corpus) of the current projection.

        )r4   r5   r7   )r0   r:   r   r4   r5   r7   rH   s    r   
empty_likezProjection.empty_like   s3     FDF(8D4D
 
 
 	
r   r   c           
      6	   |j         dS | j         >|j                                         | _         |j                                        | _        dS | j        |j        k    rt	          d|j        d| j                  t
                              dt          | j         j                  t          |j         j                             | j         j        d         | j         j        d         |j         j        d         }}}t
          	                    d           t          | j         d          | _         t          j        | j         j        |j                   }t          | j         d          | _         |xj         t          j        | j         |          z  c_         |j         g|_         t          j        |j                   \  }}|j         rJ t          j        t          j        || j        z            t          j        ||j                  gt          j        t          j        g                               dd          t/          ||          |          t          j        ||j                  gg          }	t
          	                    d	|	j                   	 t0          j                            |	d
          \  }
}}n# t0          j        j        $ rn t
                              d           t0          j                            t          j        |	|	j                  d
          \  }
}}t          j        |          }Y nw xY wt=          |dz  | j                  }	t          j        |
d|d|	f                   t          j        |
|dd|	f                   |d|	         }}}t
          	                    d           || _        t          | j         d          | _         t          j        | j         |          | _         t          |d          }t          j        ||          }| xj         |z  c_         | j         j        d         dk    rNtA          | j         j        d                   D ]0}| j         d|f         dk     r| j         dd|fxx         dz  cc<   /dS dS )a  Merge current :class:`~gensim.models.lsimodel.Projection` instance with another.

        Warnings
        --------
        The content of `other` is destroyed in the process, so pass this function a copy of `other`
        if you need it further. The `other` :class:`~gensim.models.lsimodel.Projection` is expected to contain
        the same number of features.

        Parameters
        ----------
        other : :class:`~gensim.models.lsimodel.Projection`
            The Projection object to be merged into the current one. It will be destroyed after merging.
        decay : float, optional
            Weight of existing observations relatively to new ones.
            Setting `decay` < 1.0 causes re-orientation towards new data trends in the input document stream,
            by giving less emphasis to old observations. This allows LSA to gradually "forget" old observations
            (documents) and give more preference to new ones.

        Nz'vector space mismatch: update is using z features, expected zmerging projections: %s + %sr   r   z!constructing orthogonal componentzself.uz computing SVD of %s dense matrixFfull_matricesz"SVD(A) failed; trying SVD(A * A^T)r
   zupdating orthonormal basis Uq              )!rG   rF   r   r:   
ValueErrorr   r   r@   r%   r$   r*   r   dotrE   r.   r   
qr_destroybmatdiagmultiplypadarrayreshaper   rA   linalgsvdLinAlgErrorerrorsqrtr   r   range)rH   otherdecayr:   n1n2crT   rr   u_ks_k_u1_ku2_kis                   r   mergezProjection.merge   s   ( 7 	F6 	W\\^^DFW\\^^DFF6UW 	*UZU\U\U\^b^d^de   	2C4E4Es57=GYGYZZZFLOTV\!_egmA6Fr2 	8999$&(++F468UW%%$&(++26$&!$$$7)"57++17 GWUTV^$$bk!UW&=&=>\"(2,,..q!44c!Rjj"EEr{STV[V]G^G^_
   	7AAA	  ,**1E*BBKCaa|' 	 	 	LL=>>>,**26!QS>>*OOKCa'#,,CCC		 #(DF++(3ssBQBw<00"(3rssBQBw<2H2H#bqb'Cd 	3444$&(++%%QF1dOO! 6<?Q 	)46<?++ ) )6!Q$<#% )F111a4LLLD(LLL	) 	)) )s   %J= =A?L?>L?r   )__name__
__module____qualname____doc__P2_EXTRA_ITERSP2_EXTRA_DIMSr   float64rM   rP   rr    r   r   r0   r0      sn        
 
 "u.$BJD8( 8( 8( 8(t
 
 
Z) Z) Z) Z) Z) Z)r   r0   c                        e Zd ZdZdddddddeeej        dfdZdd	Z	d
 Z
ddZd ZddZddZddZ fdZe fd            Z xZS )LsiModela[  Model for `Latent Semantic Indexing
    <https://en.wikipedia.org/wiki/Latent_semantic_analysis#Latent_semantic_indexing>`_.

    The decomposition algorithm is described in `"Fast and Faster: A Comparison of Two Streamed
    Matrix Decomposition Algorithms" <https://arxiv.org/pdf/1102.5597.pdf>`_.

    Notes
    -----
    * :attr:`gensim.models.lsimodel.LsiModel.projection.u` - left singular vectors,
    * :attr:`gensim.models.lsimodel.LsiModel.projection.s` - singular values,
    * ``model[training_corpus]`` - right singular vectors (can be reconstructed if needed).

    See Also
    --------
    `FAQ about LSI matrices
    <https://github.com/RaRe-Technologies/gensim/wiki/Recipes-&-FAQ#q4-how-do-you-output-the-u-s-vt-matrices-of-lsi>`_.

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

        >>> from gensim.test.utils import common_corpus, common_dictionary, get_tmpfile
        >>> from gensim.models import LsiModel
        >>>
        >>> model = LsiModel(common_corpus[:3], id2word=common_dictionary)  # train model
        >>> vector = model[common_corpus[4]]  # apply model to BoW document
        >>> model.add_documents(common_corpus[4:])  # update model with new documents
        >>> tmp_fname = get_tmpfile("lsi.model")
        >>> model.save(tmp_fname)  # save model
        >>> loaded_model = LsiModel.load(tmp_fname)  # load model

    N    N  r   FTc           
         || _         t          |          | _        t          |          | _        t	          |          | _        |r|st                              d           d}|| _        |	|c| _	        | _
        |
| _        || _        || j         t          d          | j         Mt                              d           t          j        |          | _         t!          | j                   | _        n7d| j         r&t%          | j                                                   ndz   | _        d| _        t+          | j        | j        | j
        | j	        |
| j        	          | _        d| _        |s#t                              d
           d| _        n|st5          d          	 ddl}|                    d          }t                              dt=          |j                             |                     | j         |||| j
        | j	        d|           || _        t!          |!                                          | _        t                              d| j                   n?# tD          $ r2}t          #                    d|           tI          d|z            d}~ww xY w|]tK          j%                    }| &                    |           | '                    dd|  dtK          j%                    |z
  dd           dS dS )a  Build an LSI model.

        Parameters
        ----------
        corpus : {iterable of list of (int, float), scipy.sparse.csc}, optional
            Stream of document vectors or a sparse matrix of shape (`num_documents`, `num_terms`).
        num_topics : int, optional
            Number of requested factors (latent dimensions)
        id2word : dict of {int: str}, optional
            ID to word mapping, optional.
        chunksize :  int, optional
            Number of documents to be used in each training chunk.
        decay : float, optional
            Weight of existing observations relatively to new ones.
        distributed : bool, optional
            If True - distributed mode (parallel execution on several machines) will be used.
        onepass : bool, optional
            Whether the one-pass algorithm should be used for training.
            Pass `False` to force a multi-pass stochastic algorithm.
        power_iters: int, optional
            Number of power iteration steps to be used.
            Increasing the number of power iterations improves accuracy, but lowers performance
        extra_samples : int, optional
            Extra samples to be used besides the rank `k`. Can improve accuracy.
        dtype : type, optional
            Enforces a type for elements of the decomposed matrix.
        random_seed: {None, int}, optional
            Random seed used to initialize the pseudo-random number generator,
            a local instance of numpy.random.RandomState instance.

        z2forcing the one-pass algorithm for distributed LSATNzYat least one of corpus/id2word must be specified, to establish input space dimensionalityzHno word id mapping provided; initializing from corpus, assuming identityr   r   )r4   r5   r6   r7   z%using serial LSI version on this nodezfdistributed stochastic LSA not implemented yet; run either distributed one-pass, or serial randomized.zPYRONAME:gensim.lsi_dispatcherzlooking for dispatcher at %sF)id2word
num_topicsr2   rg   r4   extra_samplesdistributedonepassz)using distributed version with %i workersz)failed to initialize distributed LSI (%s)createdztrained z in z.2fr   )msg)(r   intr   r2   floatrg   r   warningr   r   r4   r6   r7   rW   r   dict_from_corpusr   r3   maxkeysdocs_processedr0   
projection
numworkersr   
dispatcherNotImplementedErrorPyro4Proxyr$   r@   _pyroUri
initialize
getworkers	Exceptionrc   RuntimeErrortimeadd_documentsadd_lifecycle_event)rH   corpusr   r   r2   rg   r   r   r4   r   r6   r7   r   r   errstarts                   r   rM   zLsiModel.__init__n  s+   H j//Y5\\
 	 STTT/<k,D,
& 	dl 	k   < 	TNNefff 1&99DL ..DNNdl"R#dl&7&7&9&9":":":PRSDN$NDO9I)DDT
 
 

  	VKK?@@@"DOO )M  V"[[)IJJ
;SAT=U=UVVV%% LZ9\a $ 0@R`eov &    #-"%j&;&;&=&=">">GYYYY V V VH#NNN"#NQT#TUUUV
  	IKKEv&&&$$CtCCu)<CCCC %     	 	s   #B?I# #
J--JJc           
         t                               d           || j        }|| j        }t	          |          rt                               d           t          j                            |          s| j	        st          | j        | j        d| j        | j                  }t          || j        | j        || j        | j        | j        | j                  \  |_        |_        | j                            ||           | xj        t/          |d          rt1          |          ndz  c_        dS d}| j        r9t                               d	| j                   | j                                         t9          t;          j        ||                    D ]o\  }}t                               d
           t?          d |D                       }t                                d           tC          j"        |t1          |          | j        || j                  }	~||	j#        d         z  }| j        rRt                                d|           | j        $                    |	           ~	t                               d|           t          | j        | j        |	| j        | j        | j        | j                  }~	| j                            ||           ~t                               d|           | %                    d           q| j        r8t                               d           | j        &                                | _        | xj        |z  c_        dS | j        r
J d            t          | j        | j        |'                                | j        | j        | j                  }| j                            ||           t                               d|j#        d                    | xj        |j#        d         z  c_        dS )a_  Update model with new `corpus`.

        Parameters
        ----------
        corpus : {iterable of list of (int, float), scipy.sparse.csc}
            Stream of document vectors or sparse matrix of shape (`num_terms`, num_documents).
        chunksize : int, optional
            Number of documents to be used in each training chunk, will use `self.chunksize` if not specified.
        decay : float, optional
            Weight of existing observations relatively to new ones,  will use `self.decay` if not specified.

        Notes
        -----
        Training proceeds in chunks of `chunksize` documents at a time. The size of `chunksize` is a tradeoff
        between increased speed (bigger `chunksize`) vs. lower memory footprint (smaller `chunksize`).
        If the distributed mode is on, each chunk is sent to a different worker/computer.

        z!updating model with new documentsNzLLsiModel.add_documents() called but no documents provided, is this intended?)r6   r7   )r3   r2   r5   r4   r6   r7   )rg   __len__r   zinitializing %s workersz"preparing a new chunk of documentsc              3   4   K   | ]}t          |          V  d S Nr   .0docs     r   	<genexpr>z)LsiModel.add_documents.<locals>.<genexpr>  s(      883c#hh888888r   zconverting corpus to csc format)num_docsr3   num_nnzr6   r   zcreating job #%izdispatched documents up to #%s)r5   r4   r6   r7   zprocessed documents up to #%s   zFreached the end of input; now waiting for all remaining jobs to finishz&must be in serial mode to receive jobs)r5   r4   r6   z$processed sparse job of %i documents)(r   r   r2   rg   r   r   rA   rB   rC   r   r0   r3   r   r6   r7   r;   r   r4   rG   r   r   rr   r   hasattrr   r   r   reset	enumerater   grouperr   r$   r   rD   r%   putjobprint_topicsgetstatetocsc)
rH   r   r2   rg   updatedoc_nochunk_nochunknnzjobs
             r   r   zLsiModel.add_documents  s   & 	7888  	'I 	JEF 	kNNijjj|$$V,, A	3< 7.#NDOT*$2B   &4DO"n	#1t?OW[Wa $ 0	& & &"&( %%fE%:::##gfi6P6P'Ws6{{{VWW#### ? ,KK 94?KKKO))+++'0vy1Q1Q'R'R - -OHeKK DEEE88%88888C LL!BCCC"-E

dn #4:  C cil*F -%7BBB..s333$DfMMMM ", NDOSTM_(,(8
X\Xh" " "  --fE-BBB"$CVLLL))!,,,, ? AKK hiii&*o&>&>&@&@DO##v-####PP(PPPPDL^ ,DJ  F O!!&!666KK>QPPP6<?2r   c           
      `    | j         j        d| j        d| j        d| j        d| j        d
S )zGet a human readable representation of model.

        Returns
        -------
        str
            A human readable string of the current objects parameters.

        z<num_terms=z, num_topics=z, decay=z, chunksize=>)	__class__rt   r3   r   rg   r2   rO   s    r   __str__zLsiModel.__str__2  s@     N###T^^^T___djjjRVR`R`R`
 	
r      c                    | j         j        t          d          t          j        |          \  }}|r|r|                     ||          S |s|g}t          j        || j        | j         j        j	                  }|j
        | j         j        ddd| j        f         z  j
        }|s|                    d          }|rd| j         j        d| j                 z  |z  }|st          j        |          }nt          j        |          }|S )a  Get the latent representation for `bow`.

        Parameters
        ----------
        bow : {list of (int, int), iterable of list of (int, int)}
            Document or corpus in BoW representation.
        scaled : bool, optional
            If True - topics will be scaled by the inverse of singular values.
        chunksize :  int, optional
            Number of documents to be used in each applying chunk.

        Returns
        -------
        list of (int, float)
            Latent representation of topics in BoW format for document **OR**
        :class:`gensim.matutils.Dense2Corpus`
            Latent representation of corpus in BoW format if `bow` is corpus.

        Nz9No training data provided - LSI model not initialized yet)r2   r3   r6   r   r   )r   rG   rW   r   	is_corpus_applyr   rD   r3   r6   rE   r   r_   r   full2sparseDense2Corpus)rH   bowscaledr2   r   vec
topic_distresults           r   __getitem__zLsiModel.__getitem__?  s3   ( ? 	ZXYYY --	3 	9 	9
 ;;si;888 	%C !#tGXG^___edo/3CDO3C0CDDG
   	0#++B//J 	R 12B4?2B CCzQJ  	7)*55FF *:66Fr   c           	         | j         j        j        }t          |          }g }t	          |          D ]}t          j        ||ddf                                                   }t          j        t          j	        t          j
        ||                              }|                    d|z  |z             t          j        |          S )a  Get the topic vectors.

        Notes
        -----
        The number of topics can actually be smaller than `self.num_topics`, if there were not enough factors
        in the matrix (real rank of input matrix smaller than `self.num_topics`).

        Returns
        -------
        np.ndarray
            The term topic matrix with shape (`num_topics`, `vocabulary_size`)

        Nr   )r   rG   rE   r   re   r   asarrayflattenrd   r   rX   appendr^   )rH   projectionsr   topicsrq   rj   norms          r   
get_topicszLsiModel.get_topics  s     o')%%
z"" 	* 	*A
;q!!!t,--5577A726"&A,,//00DMM#'D.))))xr   
   c                     |t           j        j        j                  k    rdS t	          j         j        j        j        |ddf                                                   t	          j        t	          j        t	          j	                                      t          j        t	          j                  |d          } fd|D             S )aQ  Get the words that define a topic along with their contribution.

        This is actually the left singular vector of the specified topic.

        The most important words in defining the topic (greatest absolute value) are included
        in the output, along with their contribution to the topic.

        Parameters
        ----------
        topicno : int
            The topics id number.
        topn : int
            Number of words to be included to the result.

        Returns
        -------
        list of (str, float)
            Topic representation in BoW format.

        r    NT)reversec                 X    g | ]&}|j         v j         |         d |         z  z  f'S rs   )r   )r   valrj   r   rH   s     r   
<listcomp>z'LsiModel.show_topic.<locals>.<listcomp>  s@    ```SCSWS_L_`c"C!C&L4$78```r   )r   r   rG   rE   r   r   r   rd   r   rX   r   argsortr   )rH   topicnotopnmostrj   r   s   `   @@r   
show_topiczLsiModel.show_topic  s    0 c$/+-... 	2Jt(*7AAA:677??AAwrvbfQll++,,q		4>>> a`````D````r   r   c                    g }|dk     r| j         }t          t          || j                             D ]}|t          | j        j                  k     rw|r|                     ||          }n|                     ||          }|                    ||f           |r-t          
                    d|| j        j        |         |           |S )a=  Get the most significant topics.

        Parameters
        ----------
        num_topics : int, optional
            The number of topics to be selected, if -1 - all topics will be in result (ordered by significance).
        num_words : int, optional
            The number of words to be included per topics (ordered by significance).
        log : bool, optional
            If True - log topics with logger.
        formatted : bool, optional
            If True - each topic represented as string, otherwise - in BoW format.

        Returns
        -------
        list of (int, str)
            If `formatted=True`, return sequence with (topic_id, string representation of topics) **OR**
        list of (int, list of (str, float))
            Otherwise, return sequence with (topic_id, [(word, value), ... ]).

        r   )r   ztopic #%i(%.3f): %s)r   re   r   r   r   r   print_topicr   r   r   r   )rH   r   	num_wordslog	formattedshownrq   topics           r   show_topicszLsiModel.show_topics  s    , > 	)Js:t7788 	W 	WA3t())) W ? ,,QY,??EE OOAIO>>EaZ((( WKK 5q$/:KA:NPUVVVr   r   c                     t          | j        | j        j        | j        j        t          t          |t          | j        j        j                                      |           dS )a  Print (to log) the most salient words of the first `num_topics` topics.

        Unlike :meth:`~gensim.models.lsimodel.LsiModel.print_topics`, this looks for words that are significant for
        a particular topic *and* not for others. This *should* result in a
        more human-interpretable description of topics.

        Alias for :func:`~gensim.models.lsimodel.print_debug`.

        Parameters
        ----------
        num_topics : int, optional
            The number of topics to be selected (ordered by significance).
        num_words : int, optional
            The number of words to be included per topics (ordered by significance).

        )r   N)	print_debugr   r   rG   r   re   r   r   rE   )rH   r   r   s      r   r   zLsiModel.print_debug  s`    $ 	L$/+T_->#j#do&7&9":":;;<<	
 	
 	
 	
 	
 	
r   c                     | j         ) | j         j        t          j        |d          g|R i |  t	          t
          |           j        |g|R dddgi| dS )a  Save the model to a file.

        Notes
        -----
        Large internal arrays may be stored into separate files, with `fname` as prefix.

        Warnings
        --------
        Do not save as a compressed file if you intend to load the file back with `mmap`.

        Parameters
        ----------
        fname : str
            Path to output file.
        *args
            Variable length argument list, see :meth:`gensim.utils.SaveLoad.save`.
        **kwargs
            Arbitrary keyword arguments, see :meth:`gensim.utils.SaveLoad.save`.

        See Also
        --------
        :meth:`~gensim.models.lsimodel.LsiModel.load`

        N.projectionignorer   r   )r   saver   smart_extensionsuperr}   )rH   fnameargskwargsr   s       r   r   zLsiModel.save  s    4 ? 	_ DO !6um!L!L^t^^^W]^^^"h"5_4___|8T_X^_____r   c                 \   |                     dd          |d<    t          t          |           j        |g|R i |}t	          j        |d          }	  t          t          |           j        |g|R i ||_        n-# t          $ r }t          j	        d||           Y d}~nd}~ww xY w|S )ag  Load a previously saved object using :meth:`~gensim.models.lsimodel.LsiModel.save` from file.

        Notes
        -----
        Large arrays can be memmap'ed back as read-only (shared memory) by setting the `mmap='r'` parameter.

        Parameters
        ----------
        fname : str
            Path to file that contains LsiModel.
        *args
            Variable length argument list, see :meth:`gensim.utils.SaveLoad.load`.
        **kwargs
            Arbitrary keyword arguments, see :meth:`gensim.utils.SaveLoad.load`.

        See Also
        --------
        :meth:`~gensim.models.lsimodel.LsiModel.save`

        Returns
        -------
        :class:`~gensim.models.lsimodel.LsiModel`
            Loaded instance.

        Raises
        ------
        IOError
            When methods are called on instance (should be called from class).

        mmapNr   z%failed to load projection from %s: %s)
getr   r}   loadr   r   r   r   loggingr   )clsr   r   r   r   projection_fnameer   s          r   r   zLsiModel.load  s    @  FD11v*x%%*5B4BBB6BB 0FF	Z 9h 4 4 9:J \T \ \ \U[ \ \F 	Z 	Z 	ZOCEUWXYYYYYYYY	Zs   )A? ?
B)	B$$B)r9   )Fr   )r   )r   r   FT)r   r   )rt   ru   rv   rw   rx   ry   r   rz   rM   r   r   r   r   r   r   r   r   classmethodr   __classcell__)r   s   @r   r}   r}   L  s5        D #tu5$N'rztc c c cJ]3 ]3 ]3 ]3~
 
 
D D D DL     .a a a aB" " " "H
 
 
 
0` ` ` ` `< & & & & [& & & & &r   r}   r   c           
         ||dz  }t                               dt          |                     t          |          i }}t	          |          D ]\  }}t          j        t          j        |                                                    }|t          j	        t          j
        t          j        ||                              z  }	|D ]3}
|                    |
g                               |	|
         |f           4t                               d||           t          |                                          D ]3}
t          ||
         d           }|d         \  }}|||
f         dk     rd	}nd
}g g }}|D ]S\  }}||||
f         z  dk    r=|                    d| |         |||
f         fz             t          |          |k    r nT|D ]S\  }}||||
f         z  dk     r=|                    d| |         |||
f         fz             t          |          |k    r nTt                               d|
||
         d                    |          d                    |                     5dS )a[  Log the most salient words per topic.

    Parameters
    ----------
    id2token : :class:`~gensim.corpora.dictionary.Dictionary`
        Mapping from ID to word in the Dictionary.
    u : np.ndarray
        The 2D U decomposition matrix.
    s : np.ndarray
        The 1D reduced array of eigenvalues used for decomposition.
    topics : list of int
        Sequence of topic IDs to be printed
    num_words : int, optional
        Number of words to be included for each topic.
    num_neg : int, optional
        Number of words with a negative contribution to a topic that should be included.

    Nr
   z+computing word-topic salience for %i topicszprinting %i+%i salient wordsc                 .    t          | d                    S )Nr   )r   )xs    r   <lambda>zprint_debug.<locals>.<lambda>c  s    s1Q4yyj r   )keyr   rU   rV   r   g-C6?z%s(%.3f)g-C6ztopic #%s(%.3f): %s, ..., %sz, )r   r   r   setr   r   r   r   r   rd   r   rX   
setdefaultr   r$   sortedr   join)id2tokenrG   r   r   r   num_negr   uvecnouvecudiffr   weightsrn   r   	normalizeposnegweights                     r   r   r   A  s   &   a-
KK=s6{{KKK[["FF!! H Hvbj&&..0011rwrvbfT4&8&899::: 	H 	HEeR((//uv0FGGGG	H LL/GDDD&& e e,@,@AAA!*4T5[>C 	III rS% 	 	NFF1VU]++f4 

:&)91VU];K(LLMMMs88y( E% 	 	NFF1VU]++g5 

:&)91VU];K(LLMMMs88w& E2E1U8TYYs^^UYU^U^_bUcUcdddd-e er   r   gư>c	                    t          |          }|t          dd|z            }	n|t          |          z   }	t                              d|	|z
  |           t          |          }t	          j        |||	f          }
t                              dt          |
j                             t          j        	                    |          }t          j                            |           r^| j        \  }}||k    sJ d| d| d	            |                    d
d||	f                              |
j                  }t!          j        |||	| j        | j        | j        |                                |
                                           ~|
j        |k    r|
                    |          }
t                              dt          |
j                             |
g}
t-          j        |
          \  }}t                              d|           t3          |          D ])}| j        |z  }| |z  g}t-          j        |          \  }}*n"d}t7          t9          j        | |                    D ]	\  }}t                              d||z             t=          d |D                       }t-          j        |||          }|j        \  }}||k    sJ ||k    sJ ||z  }t                              d           |                    d
d||	f                              |          }t!          j        |||	|j        |j        |j        |                                |
                                           ~~|
g}
t-          j        |
          \  }}t3          |          D ]}t                              d|dz              |                                 }d
|dd<   t7          t9          j        | |                    D ]P\  }}t                              d||z  |           t-          j        |||          }|j        |z  }||z  }~||z  }Q~|g}t-          j        |          \  }}|ddd|	f         j                                         }~t          j                            |           r[|| z  }t                              dt          |j                             t          j!        "                    |d          \  }}}~~nFt	          j        |j        d         |j        d         f|          }t                              dt          |j                             t7          t9          j        | |                    D ]d\  }}t                              d||z  |           t-          j        |||j                  }||z  }~|t	          j#        ||j                  z  }~et                              dt          |j                             t          j!        "                    |          \  }}}t	          j$        |          }|j                                         }~t                              d           tK          |dz  ||          }|ddd|f                                          }|d|         }t	          j#        ||          }|                    |          |                    |          fS )a  Run truncated Singular Value Decomposition (SVD) on a sparse input.

    Parameters
    ----------
    corpus : {iterable of list of (int, float), scipy.sparse}
        Input corpus as a stream (does not have to fit in RAM)
        or a sparse matrix of shape (`num_terms`, num_documents).
    rank : int
        Desired number of factors to be retained after decomposition.
    num_terms : int
        The number of features (terms) in `corpus`.
    chunksize :  int, optional
        Number of documents to be used in each training chunk.
    extra_dims : int, optional
        Extra samples to be used besides the rank `k`. Can improve accuracy.
    power_iters: int, optional
        Number of power iteration steps to be used. Increasing the number of power iterations improves accuracy,
        but lowers performance.
    dtype : numpy.dtype, optional
        Enforces a type for elements of the decomposed matrix.
    eps: float, optional
        Percentage of the spectrum's energy to be discarded.
    random_seed: {None, int}, optional
        Random seed used to initialize the pseudo-random number generator,
         a local instance of numpy.random.RandomState instance.


    Notes
    -----
    The corpus may be larger than RAM (iterator of vectors), if `corpus` is a `scipy.sparse.csc` instead,
    it is assumed the whole corpus fits into core memory and a different (more efficient) code path is chosen.
    This may return less than the requested number of top `rank` factors, in case the input itself is of lower rank.
    The `extra_dims` (oversampling) and especially `power_iters` (power iterations) parameters affect accuracy of the
    decomposition.

    This algorithm uses `2 + power_iters` passes over the input data. In case you can only afford a single pass,
    set `onepass=True` in :class:`~gensim.models.lsimodel.LsiModel` and avoid using this function directly.

    The decomposition algorithm is based on `"Finding structure with randomness:
    Probabilistic algorithms for constructing approximate matrix decompositions" <https://arxiv.org/abs/0909.4061>`_.


    Returns
    -------
    (np.ndarray 2D, np.ndarray 1D)
        The left singular vectors and the singular values of the `corpus`.

    Nr   r
   z.using %i extra samples and %i power iterations)r6   r%   z(1st phase: constructing %s action matrixz mismatch in number of features: z in sparse matrix vs. z
 parameterrU   r   z!orthonormalizing %s action matrixzrunning %i power iterationsr   zPROGRESS: at document #%ic              3   4   K   | ]}t          |          V  d S r   r   r   s     r   r   z!stochastic_svd.<locals>.<genexpr>  s(      ..CHH......r   r   zmultiplying chunk * gausszrunning power iteration #%ir   zPROGRESS: at document #%i/%iz)2nd phase: running dense svd on %s matrixFrR   )r%   r6   z,2nd phase: constructing %s covariance matrixz3running dense decomposition on %s covariance matrixz!computing the final decomposition)r   )&r   r   r   r   r   zerosr@   r%   randomRandomStaterA   rB   rC   normalastyper6   r   csc_matvecsindptrindicesdataravelr   rY   r$   re   rE   r   r   r   r   rD   rF   r`   ra   rX   rd   r   )r   rankr3   r2   r5   r4   r6   epsr7   samplesyrandom_stater:   norT   rn   r   r   r   r   
power_iteryoldtmpqtbrG   rL   r   keeps                                 r   r;   r;   {  s   h t99D )b!d(##Z(
KK@'D.R]^^^II
 	uY$8999A
KK:CLLIII9((55L|V$$ <*|1A~ppp!pp[dppppppS1g,77>>qwGGq'6=&.KAGGII	
 	
 	
  7e 	 A7QWFFFC"1%%12K@@@{## 	* 	*A1A!A&q))DAqq	*
 (vy)I)IJJ 	 	OHeKK3h6JLLL .......A'%PPPE;DAq	>!!!	>!!!MHLL4555##Cq'l==DDUKKA#1gu|U]
AGGIIqwwyy   qqC"1%%1,, 	* 	*JKK5zA~FFF6688DAaaaD#,U]69-M-M#N#N  %:Hy<PRZ[[[ +EYeTTTgnckSA&q))DAqq	
111hwh;				 	 B	|V$$ K?QWNNN<##AU#;;1brr
 HBHQK!5UCCCBCLLQQQ(vy)I)IJJ 	 	OHeKK698LhWWW'"(SSSEU
A13A 	I3qw<<XXX<##A&&1bGAJJ
		A

KK3444as333D	!!!UdU(A	%4%A
q!A88E??AHHUOO++r   )r   )r    )r   N)"rw   r   r<   r   numpyr   scipy.linalgrA   scipy.sparser   gensimr   r   r   gensim.modelsr   gensim.utilsr   	getLoggerrt   r   ry   rx   r   r*   r.   SaveLoadr0   TransformationABCBaseTopicModelr}   r   rz   r;   r{   r   r   <module>r2     s  4 4l  



              $ $ $ $ $ $ . . . . . . . . . . # # # # # # ! ! ! ! ! !		8	$	$    8   ,   ,o) o) o) o) o) o) o) o)dr r r r rz+Y-E r r rj7e 7e 7e 7ev ,1TRZTtf, f, f, f, f, f,r   