"""
few functions to massage into lisp data
python functions to convert arff files to lisp
only works on csv file with no other junk
chet tobrey
feb 27 2008
"""
def addParens (s):
   lispStr = '(data ' + s + ')\n'
   lispStr = lispStr.replace('e+','')
   lisrStr = lispStr.replace('e-','')
   return lispStr

def lispify(f):
    lispData = []
    counterrows = 0
    for line in f:
        line = line.rstrip('\r\n')
        line = kill_columns(line,0)#must set to zero or otherwise this returns a list
        temp = ''
        counterrows +=1
        for elt in line:
           temp1 = str(elt)
           temp += (temp1 +' ')
       
        lispData += addParens(temp)
        print 'rows' , counterrows
    return lispData

def build_relation (num_cols):
   rel_name ='(defrelation ' + raw_input ("what will be the name of the relation?\n") +'\n'
  
   class_column_num = num_cols - 1

   att_list =[]   
   att_list += rel_name
   
   for i in range(1,num_cols+1):
      att_str = '(attribute x' + str(i) + ' real)\n'
      att_list += att_str

   return att_list

def kill_columns(data,how_many):
   """takes a string of cs values and removes specified
   number of columns from front"""
   temp = data.split(',')

   for i in range(how_many):
      temp.pop(0)
   return temp

def Main():
    """change filename here to use on other files"""
    inFile =raw_input('Enter the file name\n')
    f = open(inFile)
    data = f.readlines()
    f.close()

    num_cols=(len(data[0].split(','))) #-105 for all_data mystery
    lispData = lispify(data)
    g = open((inFile.split('.')[0])+'.lisp','w')
    data_list =[]
    
    for line in lispData:
       data_list +=line
   
    att_list = build_relation(num_cols)
 
    att_list += data_list
    att_list.append(('\n)'))
    for elt in att_list:
       g.write(elt)
    g.close()

    
    
