<s> import codecs , collections , os , os . path <EOL> class CorpusWriter ( object ) : <EOL> def __init__ ( self , fileids , path = '<STR_LIT>' , mode = '<STR_LIT:a>' , encoding = '<STR_LIT:utf-8>' , trace = <NUM_LIT:1> ) : <EOL> assert fileids and path and mode <EOL> self . mode = mode <EOL> self . encoding = encoding <EOL> self . trace = trace or <NUM_LIT:0> <EOL> self . full_path = os . path . expanduser ( path ) <EOL> for dirname in set ( [ os . path . dirname ( fileid ) for fileid in fileids ] ) : <EOL> dirpath = os . path . join ( self . full_path , dirname ) <EOL> if not os . path . exists ( dirpath ) : <EOL> if trace : <EOL> print ( '<STR_LIT>' % dirpath ) <EOL> os . makedirs ( dirpath ) <EOL> self . fileids = [ os . path . join ( self . full_path , fileid ) for fileid in fileids ] <EOL> self . files = { } <EOL> def get_file ( self , fileid ) : <EOL> if not fileid . startswith ( self . full_path ) : <EOL> fileid = os . path . join ( self . full_path , fileid ) <EOL> if fileid not in self . files : <EOL> self . files [ fileid ] = codecs . open ( fileid , self . mode , self . encoding ) <EOL> return self . files [ fileid ] <EOL> def open ( self ) : <EOL> for fileid in self . fileids : <EOL> if self . trace : <EOL> print ( '<STR_LIT>' % fileid ) <EOL> self . get_file ( fileid ) <EOL> return self <EOL> def close ( self , * args , ** kwargs ) : <EOL> for fileid , f in self . files . items ( ) : <EOL> if self . trace : <EOL> print ( '<STR_LIT>' % fileid ) <EOL> f . close ( ) <EOL> del self . files [ fileid ] <EOL> __enter__ = open <EOL> __exit__ = close <EOL> __del__ = close <EOL> def write ( self , s , fileid = None ) : <EOL> if not fileid : <EOL> fileid = self . fileids [ <NUM_LIT:0> ] <EOL> self . get_file ( fileid ) . write ( s ) </s>
