Skip to content

Commit

Permalink
Added convert command
Browse files Browse the repository at this point in the history
  • Loading branch information
cboulanger committed Mar 6, 2024
1 parent de51953 commit b928106
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions run-workflow
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ HELP = \
--verbose Output additional information
--debug Output debug information
--clean Clean output directories before running command
convert [options] <path/to/fie>
--to-csl <path/to/csl-file> Convert input file to csl-json file at the given path
TEXT

puts HELP if ARGV.include?('--help') || ARGV.empty?
Expand Down Expand Up @@ -433,3 +436,42 @@ if ARGV.include? 'export-dataset'
dataset.export(exporter, limit:, preprocess:, postprocess:)

end

# ############################################################################################################
# convert
# ############################################################################################################

if ARGV.include? 'convert'
ds = Wapiti::Dataset.new
as = Datamining::AnyStyle.new(use_default_models:true)

# input file tpyes
arg_name = '--from-xml'
if ARGV.include? arg_name
input_path = ARGV[(ARGV.index(arg_name) + 1)]
if File.file? input_path
files = [input_path]
elsif File.directory? input_path
files = Dir.children(input_path)
else
raise 'Invalid input path'
end
files.each do |file|
xml = File.read(File.join(input_path, file), encoding:'utf-8')
ds += as.xml_to_wapiti(xml)
end
end

raise "No input given" if ds.nil?

# output file types
arg_name = '--to-csl'
if ARGV.include? arg_name
output_path = ARGV[(ARGV.index(arg_name) + 1)]
items = as.wapiti_to_csl(ds)
csl_json = JSON.pretty_generate(items)
File.write(output_path, csl_json, encoding:'utf-8')
else
raise "Invalid export format."
end
end

0 comments on commit b928106

Please sign in to comment.