#!/usr/bin/env ruby # -*- coding: utf-8 -*- Encoding.default_external = "UTF-8" $:.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'rubyfca' ################ parse options ########## opts = Trollop::options do version RubyFCA::VERSION banner <<-EOS RubuFCA converts Conexp CXT data to Graphviz dot format. Usage: rubyfca [options] where: ".cxt", ".csv" ."dot", ".png", ".jpg", or ".eps" [options]: EOS opt :full, "Do not contract concept labels", :default=> false opt :coloring, "Color concept nodes [0 = none (default), 1 = lightblue/pink, 2 = monochrome]", :default => 0 opt :straight, "Straighten edges (available when output format is either png, jpg, svg, pdf, or eps)", :default => false opt :nodesep, "Size of separation between sister nodes (from 0.1 to 5.0)", :default => 0.4 opt :ranksep, "Size of separation between ranks (from 0.1 to 5.0)", :default => 0.2 opt :legend, "Print the legend of concept nodes (available only when using circle node shape)", :default => false opt :circle, "Use circle shaped concept nodes", :default=> false end Trollop::die :coloring, "must be 0, 1, or 2" if (opts[:coloring] > 2 || opts[:coloring] < 0) Trollop::die :ranksep, "must be within 0.1 - 5.0" if (opts[:ranksep] < 0.1 || opts[:ranksep] > 5.0) Trollop::die :nodesep, "must be within 0.1 - 5.0" if (opts[:nodesep] < 0.1 || opts[:nodesep] > 5.0) ############### main program ############### if ARGV.size != 2 showerror("Input and output files are not set properly", 1) end filename1 = ARGV[0] #input filename filename2 = ARGV[1] #output filename # # extract input and output file types # input_type = filename1.slice(/\.[^\.]+\z/).split(//)[1..-1].join("") output_type = filename2.slice(/\.[^\.]+\z/).split(//)[1..-1].join("") if (input_type !~ /\A(cxt|csv)\z/ || output_type !~ /\A(dot|png|jpg|svg|pdf|eps)\z/) showerror("These file extensions are not (yet) supported.", 1) end # # input data is kept as plain text # f = File.open(filename1, "r:UTF-8:UTF-8") inputdata = f.read inputdata.gsub!(/\r\n?/){"\n"} f.close # # ask for confirmation of overwriting an exisiting file # if (File.exist?(filename2) && !opts[:sil]) print "#{filename2} exists and will be overwritten, OK? [y/n]" var1 = STDIN.gets; if /y/i !~ var1 exit; end end # # context data is converted to a hash table # begin ctxt = FormalContext.new(inputdata, input_type, !opts[:full]) ctxt.calcurate # rescue => e # puts e # showerror("Source data may have problems. Process aborted.", 1) end # # create the output file # case output_type when "dot" File.open(filename2, "w") do |f| f.write(ctxt.generate_dot(opts)) end when "png" ctxt.generate_img(filename2, "png", opts) when "jpg" ctxt.generate_img(filename2, "jpg", opts) when "svg" ctxt.generate_img(filename2, "svg", opts) when "pdf" ctxt.generate_img(filename2, "pdf", opts) when "eps" ctxt.generate_img(filename2, "eps", opts) end