<s> """<STR_LIT>""" <EOL> import logging <EOL> import shelve <EOL> from gensim import interfaces , utils <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class IndexedCorpus ( interfaces . CorpusABC ) : <EOL> def __init__ ( self , fname , index_fname = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if index_fname is None : <EOL> index_fname = fname + '<STR_LIT>' <EOL> self . index = utils . unpickle ( index_fname ) <EOL> logger . info ( "<STR_LIT>" % index_fname ) <EOL> except : <EOL> self . index = None <EOL> self . length = None <EOL> @ classmethod <EOL> def serialize ( serializer , fname , corpus , id2word = None , index_fname = None , progress_cnt = None , labels = None , metadata = False ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( corpus , '<STR_LIT>' , None ) == fname : <EOL> raise ValueError ( "<STR_LIT>" % fname ) <EOL> if index_fname is None : <EOL> index_fname = fname + '<STR_LIT>' <EOL> if progress_cnt is not None : <EOL> if labels is not None : <EOL> offsets = serializer . save_corpus ( fname , corpus , id2word , labels = labels , progress_cnt = progress_cnt , metadata = metadata ) <EOL> else : <EOL> offsets = serializer . save_corpus ( fname , corpus , id2word , progress_cnt = progress_cnt , metadata = metadata ) <EOL> else : <EOL> if labels is not None : <EOL> offsets = serializer . save_corpus ( fname , corpus , id2word , labels = labels , metadata = metadata ) <EOL> else : <EOL> offsets = serializer . save_corpus ( fname , corpus , id2word , metadata = metadata ) <EOL> if offsets is None : <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> serializer . __name__ ) <EOL> logger . info ( "<STR_LIT>" % ( serializer . __name__ , index_fname ) ) <EOL> utils . pickle ( offsets , index_fname ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . index is not None : <EOL> return len ( self . index ) <EOL> if self . length is None : <EOL> logger . info ( "<STR_LIT>" ) <EOL> self . length = sum ( <NUM_LIT:1> for doc in self ) <EOL> return self . length <EOL> def __getitem__ ( self , docno ) : <EOL> if self . index is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return self . docbyoffset ( self . index [ docno ] ) </s>
