Initial implementation of script

This commit is contained in:
Danila Fedorin 2019-11-09 20:16:50 -08:00
commit e342ff703f
2 changed files with 115 additions and 0 deletions

57
3dw.cr Normal file
View File

@ -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

58
mutate.cr Normal file
View File

@ -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