Sha256: 8a5eaa940883c70cd540feb57898ec5eb8d078ed43924815803eb38794107d26

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

#!/usr/bin/env ruby
require 'arb/dict'
require 'csv'
require 'slop'
require 'json'


define_method :fmt_json do |main_hash,*args|
  puts main_hash.to_json
end

define_method :fmt_csv do |main_hash,separator|
  puts main_hash.values.map{|tmp| next tmp.join(separator) if tmp.is_a? Array;tmp}.to_csv
end

define_method :fmt_raw do |main_hash,*args|
  puts main_hash[:entity], ''
  if main_hash[:entity].ascii_only?
    puts "US[#{main_hash[:phonetic_us]}]"
    puts "UK[#{main_hash[:phonetic_uk]}]"
  else
    puts "Phonetic[#{main_hash[:phonetic]}]"
  end
  puts '' 
  puts main_hash[:translation]
end

begin
  opts = Slop.parse do |o|
    o.string *%w{-f --format}, 'available choices: json,csv,raw(default)', default: 'raw'
    o.string *%w{-s --separator},'separator used in array#join in csv format(default: NEWLINE)',default: "\n"
    o.string *%w{-i --inputfile},'sepecify data input file in which words or phrases should be separated by line'
  end
rescue Slop::MissingArgument
  puts 'Missing arguments!'
rescue Slop::UnknownOption
  puts 'Unknown options!'
end

if opts
  unless opts.args.size>0 || opts[:inputfile]
    puts 'Missing target, please specify a word or phrase!'
    exit
  end
  if opts[:inputfile]
    if File.exists?(opts[:inputfile])
    opts.args<<File.readlines(opts[:inputfile]).map(&:chomp)
    opts.args.flatten!
    else
      puts "Can not find inputfile #{opts[:inputfile]}"
    end
  end
  opts.args.each do |entity|
    main_hash=Arb::Dict.query(entity)
    main_hash=main_hash.map{|tmp| [tmp[0],tmp[1] || 'NOT FOUND']}.to_h
    send "fmt_#{opts[:format]}", main_hash,opts[:separator]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arb-dict-0.2.4 bin/arb-dict