commit e342ff703fa9531b4ea4d2f09e423f07f6755c08 Author: Danila Fedorin Date: Sat Nov 9 20:16:50 2019 -0800 Initial implementation of script diff --git a/3dw.cr b/3dw.cr new file mode 100644 index 0000000..d659c99 --- /dev/null +++ b/3dw.cr @@ -0,0 +1,57 @@ +require "./mutate.cr" +require "random" + +class Generator + getter words : Array(String) + + def initialize(@words) + end + + def sentence(f = 3, t = 9) + words = [] of String + nwords = f + Random.rand(t - f + 1) + nwords.times do |time| + word = @words[time % @words.size] + word = word.capitalize if time == 0 + words << word.mutate + end + return words.join(" ") + ". " + end + + def paragraph(f = 4, t = 20) + nsentences = f + Random.rand(t - f + 1) + sentences = Array(String).new(nsentences) { sentence } + return sentences.join("") + end + + def subsection(f = 4, t = 20) + title = "\\subsection{#{sentence(@words.size, @words.size)}}\n" + nparas = f + Random.rand(t - f + 1) + paras = Array(String).new(nparas) { paragraph } + return title + paras.join("\n\n") + end + + def section(f = 4, t = 20) + title = "\\section{#{sentence(@words.size, @words.size)}}\n" + nsections = f + Random.rand(t - f + 1) + subsections = Array(String).new(nsections) { subsection } + return title + paragraph + "\n\n" + subsections.join("\n\n") + end + + def document(f = 2, t = 4) + title = sentence(@words.size, @words.size) + abstrct = paragraph(10, 10) + nsections = f + Random.rand(t - f + 1) + sections = Array(String).new(nsections) { section } + prefix = "\\documentclass[10pt, draftclsnofoot,onecolumn]{IEEEtran}" + + "\\def\\changemargin#1#2{\list{}{\\rightmargin#2\\leftmargin#1}\\item[]}" + + "\\let\\endchangemargin=\\endlist" + + "\\begin{document}" + postfix = "\\end{document}" + return prefix + "\\title{#{title}}\n\n\\maketitle\n\n\\begin{abstract}#{abstrct}\\end{abstract}\n\n\\pagebreak\n\n\\tableofcontents\\pagebreak" + sections.join("\n\n") + postfix + end +end + +WORDS = ["three", "day", "weekend"] +generator = Generator.new WORDS +puts generator.document diff --git a/mutate.cr b/mutate.cr new file mode 100644 index 0000000..bffa311 --- /dev/null +++ b/mutate.cr @@ -0,0 +1,58 @@ +require "random" + +class String + @@nearby_chars = { + 'q' => ['w', 'a'], + 'w' => ['q', 'e', 's'], + 'e' => ['w', 'r', 'd'], + 'r' => ['e', 't', 'f'], + 't' => ['r', 'y', 'g'], + 'y' => ['t', 'h', 'u'], + 'u' => ['y', 'j', 'i'], + 'i' => ['u', 'k', 'o'], + 'o' => ['i', 'l', 'p'], + 'p' => ['o', 'l'], + 'a' => ['q', 's', 'z'], + 's' => ['w', 'r', 'a', 'x'], + 'd' => ['s', 'e', 'f', 'x', 'c'], + 'f' => ['d', 'r', 'x', 'c', 'v'], + 'g' => ['f', 't', 'h', 'v', 'b'], + 'h' => ['g', 'y', 'j', 'b', 'n'], + 'j' => ['h', 'u', 'k', 'n', 'm'], + 'k' => ['j', 'i', 'l', 'm'], + 'l' => ['k', 'o', 'p'], + 'z' => ['a', 's', 'x'], + 'x' => ['z', 's', 'd', 'c'], + 'c' => ['x', 'd', 'f', 'v'], + 'v' => ['c', 'f', 'g', 'b'], + 'b' => ['v', 'g', 'h', 'n'], + 'n' => ['b', 'h', 'j', 'm'], + 'm' => ['n', 'j', 'k', 'l'], + ' ' => [' '] + } + + def mutate + return self if Random.rand > 0.1 + mutations = [ + ->mutate_modify, + ->mutate_insert + ] + return mutations.sample(1)[0].call.mutate + end + + def mutate_modify + index = Random.rand(size) + was_upcase = self[index].uppercase? + char = self[index].downcase + char = @@nearby_chars[char].sample(1)[0] + char = char.upcase if was_upcase + new_chars = chars + new_chars[index] = char + return new_chars.join("") + end + + def mutate_insert + index = Random.rand(size) + return insert(index, ' ') + end +end