<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import logging <EOL> import itertools <EOL> from gensim import utils , matutils <EOL> from six . moves import xrange <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class CorpusABC ( utils . SaveLoad ) : <EOL> """<STR_LIT>""" <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> super ( CorpusABC , self ) . save ( * args , ** kwargs ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def save_corpus ( fname , corpus , id2word = None , metadata = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> logger . info ( "<STR_LIT>" % fname ) <EOL> with utils . smart_open ( fname , '<STR_LIT:wb>' ) as fout : <EOL> for doc in corpus : <EOL> fmt = str ( doc ) <EOL> fout . write ( utils . to_utf8 ( "<STR_LIT>" % fmt ) ) <EOL> class TransformedCorpus ( CorpusABC ) : <EOL> def __init__ ( self , obj , corpus , chunksize = None ) : <EOL> self . obj , self . corpus , self . chunksize = obj , corpus , chunksize <EOL> self . metadata = False <EOL> def __len__ ( self ) : <EOL> return len ( self . corpus ) <EOL> def __iter__ ( self ) : <EOL> if self . chunksize : <EOL> for chunk in utils . grouper ( self . corpus , self . chunksize ) : <EOL> for transformed in self . obj . __getitem__ ( chunk , chunksize = None ) : <EOL> yield transformed <EOL> else : <EOL> for doc in self . corpus : <EOL> yield self . obj [ doc ] <EOL> class TransformationABC ( utils . SaveLoad ) : <EOL> """<STR_LIT>""" <EOL> def __getitem__ ( self , vec ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def _apply ( self , corpus , chunksize = None ) : <EOL> """<STR_LIT>""" <EOL> return TransformedCorpus ( self , corpus , chunksize ) <EOL> class SimilarityABC ( utils . SaveLoad ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , corpus ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def get_similarities ( self , doc ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def __getitem__ ( self , query ) : <EOL> """<STR_LIT>""" <EOL> is_corpus , query = utils . is_corpus ( query ) <EOL> if self . normalize : <EOL> if matutils . ismatrix ( query ) : <EOL> import warnings <EOL> else : <EOL> if is_corpus : <EOL> query = [ matutils . unitvec ( v ) for v in query ] <EOL> else : <EOL> query = matutils . unitvec ( query ) <EOL> result = self . get_similarities ( query ) <EOL> if self . num_best is None : <EOL> return result <EOL> if matutils . ismatrix ( result ) : <EOL> return [ matutils . full2sparse_clipped ( v , self . num_best ) for v in result ] <EOL> else : <EOL> return matutils . full2sparse_clipped ( result , self . num_best ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> norm = self . normalize <EOL> self . normalize = False <EOL> try : <EOL> chunking = self . chunksize > <NUM_LIT:1> <EOL> except AttributeError : <EOL> chunking = False <EOL> if chunking : <EOL> for chunk_start in xrange ( <NUM_LIT:0> , self . index . shape [ <NUM_LIT:0> ] , self . chunksize ) : <EOL> chunk_end = min ( self . index . shape [ <NUM_LIT:0> ] , chunk_start + self . chunksize ) <EOL> chunk = self . index [ chunk_start : chunk_end ] <EOL> if chunk . shape [ <NUM_LIT:0> ] > <NUM_LIT:1> : <EOL> for sim in self [ chunk ] : <EOL> yield sim <EOL> else : <EOL> yield self [ chunk ] <EOL> else : <EOL> for doc in self . index : <EOL> yield self [ doc ] <EOL> self . normalize = norm </s>
