=begin rdoc

Author:: Nicola Kaiser (kaiser@cl.uni-heidelberg.de)
Project:: iCookWare
Copyright:: iCookWare Team 2005 (Nicola Kaiser, Ana Kovatcheva, Olga Mordvinova, Stephanie Schuldes)
Embedded Documentation Tool:: rdoc
=end

require 'icw_row'
require 'dist'
require 'clustering'


#adds distance method to Row class
module ICW
  class Row < Array
    include ICW::Dice
  end
end


######## this is the module actually in use ##########

# marshals cluster in a file given by fn
# +cl+:: cluster, array of arrays, containing ingredient names
# +fn+:: filenneme, string, place to store marshaled data
def saveCluster cl, fn
  fp=open(fn, 'w')
  Marshal.dump(cl, fp)
  fp.close   
end

# loads cluster data structure from a mmarshaled file
# +fn+:: filename, string, place to get marshaled data
def getSavedCluster fn
  fp=open(fn, 'r')
  cl=Marshal.load(fp)
  fp.close
  return cl
end


# method for testing: prints cluster returned by cluster*  to stdout
def display cl
  cl.each_with_index do |cluster, i|
    puts "############### Cluster: "+ i.to_s+ " ########################"
    cluster[1].each do |v|
      puts v.name
    end
  end
end

def doClustering
  m = ICW::IngredientMatrix.new
  m.buildMatrix("kodierung_num.txt")
  c=ICW::Clustering.new(m, 30) #Second Argument: number of clusters
  anotherCluster=c.cluster_withExistingCentroids
  #display anotherCluster
  name_cl=c.reduceCluster
  #puts name_cl.inspect
  saveCluster name_cl, "test_cluster_marshalled"
  back=getSavedCluster "test_cluster_marshalled"
  puts "datan aus gemarshalltem file", back.inspect
end

doClustering
