You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB
Ruby

#! /usr/bin/env ruby
require 'csv'
if ARGV.size != 2
$stderr.puts "Usage: #{$0} language out_file"
exit 1
end
case ARGV[0]
when 'c++', 'cxx'
prefix = ''
suffix = ''
array_prefix = 'constexpr const char* %s[] = { '
array_suffix = ' };'
when 'ruby'
prefix = '# frozen_string_literal: true'
suffix = <<'EOS'
require 'securerandom'
def udcrr
adv = ADVERBS[SecureRandom.random_number ADVERBS.size]
adj = ADJECTIVES[SecureRandom.random_number ADJECTIVES.size]
noun = NOUNS[SecureRandom.random_number NOUNS.size]
verb = VERBS[SecureRandom.random_number VERBS.size]
num = SecureRandom.random_number 10000
"%s-%s-%s-%s-%04d" % [adv, adj, noun, verb, num]
end
EOS
array_prefix = '%s = [ '
array_suffix = ' ].freeze'
when 'lua'
prefix = ''
suffix = <<'EOS'
local adv_size = #ADVERBS+1
local adj_size = #ADJECTIVES+1
local noun_size = #NOUNS+1
local verb_size = #VERBS+1
local floor, format = math.floor, string.format
local ffi = require("ffi")
ffi.cdef("int RAND_bytes(unsigned char *buf, int num);")
local ssl = ffi.load("crypto")
local num = ffi.new("uint32_t[1]")
local arg = ffi.cast("unsigned char*", num)
local function rand(max)
local trunc = floor(0xffffffff / max) * max
repeat
assert(ssl.RAND_bytes(arg, 4) == 1)
until num[0] < trunc
return num[0] % max
end
return function ()
local adv = ADVERBS[rand(adv_size)]
local adj = ADJECTIVES[rand(adj_size)]
local noun = NOUNS[rand(noun_size)]
local verb = VERBS[rand(verb_size)]
local num = rand(10000)
return format("%s-%s-%s-%s-%04d", adv, adj, noun, verb, num)
end
EOS
array_prefix = 'local %s = {[0]= '
array_suffix = '}'
else
fail "Unknown language"
end
define_method :gen_list do |fname, name, out|
words = File.foreach(fname).lazy.
map {|l| CSV.parse_line(l.gsub '\"', '""')[1] }.
drop(1).select {|x| x =~ /^[0-9a-zA-Z_'-]*$/ }.
map {|x| x.gsub(/[_']/, '-').downcase }.force
out.write array_prefix % name
out.write words.map(&:inspect).join ", "
out.puts array_suffix
puts "#{words.size} #{name}"
end
f = File.open ARGV[1], 'w'
f.puts prefix
# wordlist from wiktionary
# https://petscan.wmflabs.org/?psid=3899124&format=csv
gen_list 'adverb_list.csv', 'ADVERBS', f
# https://petscan.wmflabs.org/?psid=3864045&format=csv
gen_list 'adjective_list.csv', 'ADJECTIVES', f
# https://petscan.wmflabs.org/?psid=3864050&format=csv
gen_list 'noun_list.csv', 'NOUNS', f
# base verb forms, not used https://petscan.wmflabs.org/?psid=3899120&format=csv
# https://petscan.wmflabs.org/?psid=3900357&format=csv
gen_list 'verb_list.csv', 'VERBS', f
f.puts suffix