Skip to content

Instantly share code, notes, and snippets.

@ololobus
Created August 27, 2014 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ololobus/11f222d1fc48f2efef56 to your computer and use it in GitHub Desktop.
Save ololobus/11f222d1fc48f2efef56 to your computer and use it in GitHub Desktop.
XLS file dict (word, transcription, translation) to js object
require 'roo'
require 'optparse'
require 'json'
options = {}
dict = {}
OptionParser.new do |opts|
opts.on('-f File', '--file File', String, 'Input file') do |f|
options[:file] = f
end
opts.on('-o OutputFile', '--output OutputFile', String, 'Output file') do |o|
options[:output] = o
end
end.parse!
if !options[:file] || !options[:output] then
puts 'Please scpecify input file using -f key and output file using -o'
else
sheet = Roo::Excel.new(options[:file])
for l in sheet.first_row..sheet.last_row
word = sheet.cell('A', l)
transcript = sheet.cell('B', l)
trans = sheet.cell('C', l)
if word && word != '' && transcript && transcript != '' && word.scan(/\ /).count < 3 && word.scan(/[\-\.\,]+/).count == 0 && trans
trans = trans.strip.gsub(/\(.*\)/, '').gsub(';', '').split(',').first(2).map{ |w| w.strip }.join(', ')
if trans.scan(/\ /).count < 3 && trans.scan(/[\=\"\(\)]+/).count == 0 && trans != ''
dict[word] = [transcript, trans]
end
end
end
output_content = "{\n"
dict.each do |k,v|
output_content += " \"#{k}\": [\"#{v[0]}\", \"#{v[1]}\"],\n"
end
output_content += '}'
File.open(options[:output], 'w') { |file| file.write(output_content) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment