Skip to content

Instantly share code, notes, and snippets.

@dphiffer
Last active October 11, 2015 21:43
Show Gist options
  • Save dphiffer/f5b25de97354b9807c74 to your computer and use it in GitHub Desktop.
Save dphiffer/f5b25de97354b9807c74 to your computer and use it in GitHub Desktop.
LipsumBlender / code test for Position Development (I did not get an offer)
# LipsumBlender
# Dan Phiffer <[email protected]>
# August 2015
# LipsumBlender watches the current directory for .txt files, and accumulates
# the file contents into an internal corpus. This corpus is used to randomly
# generate a paragraph of 10 sentences, each containing 10 words. The frequency
# of the word selection is based on how commonly words appear within the
# aggregate input texts.
# +---------------------------------------------------------------------------+
# | n.b.: this will delete the .txt file after it finishes, so don't put |
# | anything precious into the folder. |
# +---------------------------------------------------------------------------+
# Usage: ruby lipsum_blender.rb
require 'directory_watcher'
class LipsumBlender
def initialize()
@corpus = []
@dir_watcher_target = '.'
@dir_watcher_options = {
:glob => '*.txt',
:interval => 1
}
@downcase_exceptions = [
'I'
]
start_watching
end
def start_watching()
# Watch for incoming files
dw = DirectoryWatcher.new @dir_watcher_target, @dir_watcher_options
dw.add_observer do |*args|
args.each do |event|
# We found a file, let's do some stuff with it
found_text_file event
# All done, we can delete the file now
if File.exists? event.path then
File.delete event.path
end
end
end
dw.start
puts "I gobble up .txt files in the current directory and turn them into lipsum."
puts "Press enter key to exit.\n"
gets # Wait for an enter key press
dw.stop
end
def found_text_file(event)
# Only operate on newly added files
return unless event.added?
# Read the file, strip out punctuation, and grab all the words
content = File.read event.path
content.gsub! /\s/, ' ' # Normalize whitespace
content.gsub! /[^A-Za-z ]/, '' # Remove non-alpha chars
@corpus += content.scan /[A-Za-z]+/ # Find consecutive alpha chars
# Output a random paragraph
puts
puts wrap get_random_paragraph
end
def get_random_paragraph()
# Returns a paragraph of 10 sentences
sentences = []
10.times do
sentences.push get_random_sentence
end
sentences.join ' '
end
def get_random_sentence()
# Returns a capitalized sentence, with a trailing period.
words = []
10.times do
# Pick a random word from the word corpus
index = rand @corpus.length
word = @corpus[index]
if (words.empty?) then
word.capitalize! # Capitalize the first word
elsif @downcase_exceptions.find_index(word).nil?
word.downcase! # Lowercase everything else
end
words.push word
end
words.join(' ') + '.'
end
# From the Ruby Cookbook
# https://www.safaribooksonline.com/library/view/ruby-cookbook/0596523696/ch01s15.html
def wrap(s, width=80)
s.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
end
end
# Let's blend!
lb = LipsumBlender.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment