def write( self ): CheckAndMakeDirs( self.path ) filename = self.path + TokensAPI.TOKENS with open( filename, 'w' ) as f: writer = UnicodeWriter( f ) for ( docID, docTokens ) in self.data.iteritems(): writer.writerow( [ docID, ' '.join(docTokens) ] )
def WriteAsSparseMatrix( matrix, filename ): """ Expect a sparse matrix (two-level dict) of values. Generate a tab-delimited file, with 3 columns. Write two keys as the 1st and 2nd columns; write cell value as the 3rd column. """ sortedKeys = sorted( matrix.keys(), key = lambda key : -matrix[key] ) with open( filename, 'w' ) as f: writer = UnicodeWriter( f ) for ( aKey, bKey ) in sortedKeys: writer.writerow( [ aKey, bKey, str( matrix[ (aKey, bKey) ] ) ] )
def WriteAsSparseVector( vector, filename ): """ Expect a sparse vector (dict) of values. Generate a tab-delimited file, with 2 columns. Write key as the 1st column; write cell value as the 2nd column. """ sortedKeys = sorted( vector.keys(), key = lambda key : -vector[key] ) with open( filename, 'w' ) as f: writer = UnicodeWriter( f ) for key in sortedKeys: writer.writerow( [ key, str( vector[key] ) ] )
def WriteAsTabDelimited( data, filename, fields ): """ Expect a list of dict values. Take in a list of output fields. Write specified fields to disk, as a tab-delimited file (with header row). """ with open( filename, 'w' ) as f: writer = UnicodeWriter( f ) writer.writerow( fields ) for element in data: values = [] for field in fields: values.append( str( element[field] ) ) writer.writerow( values )
def WriteAsMatrix( matrix, filename ): with open( filename, 'w' ) as f: writer = UnicodeWriter( f ) for row in matrix: writer.writerow( map( str, row ) )