module MadLibber
Constants
- OPTIONS
- TAGS
- TAGS_HTML
Public Instance Methods
create_tag_word_pairs(word)
click to toggle source
# File lib/madlibber.rb, line 66 def create_tag_word_pairs word index = ( word =~ /\/\S*$/ ) tag = word[(index + 1)..-1] word = word[0..(index - 1)] { word: word, tag: tag } end
find_fillable_indices(tag_word_pairs)
click to toggle source
# File lib/madlibber.rb, line 73 def find_fillable_indices tag_word_pairs fillable_indices = tag_word_pairs.map.with_index do |tw_pair, index| fillable_tag = TAGS[ tw_pair[:tag] ] fillable_tag ? index : nil end.compact! fillable_indices ? fillable_indices : [] end
generate_output(tag_word_pairs, fillable_indices, html_form_flag)
click to toggle source
# File lib/madlibber.rb, line 48 def generate_output tag_word_pairs, fillable_indices, html_form_flag output_array = tag_word_pairs.map.with_index do |tw_pair, index| if fillable_indices.include? index html_form_flag ? TAGS_HTML[ tw_pair[:tag] ] : TAGS[ tw_pair[:tag] ] else tw_pair[:word] end end if html_form_flag output_array.unshift "<form id='madlib-form'>" output_array << "<input type='submit'>" output_array << "</form>" end output_array.join(" ").gsub " '", "'" end
libberfy(text, options = OPTIONS)
click to toggle source
# File lib/madlibber.rb, line 36 def libberfy text, options = OPTIONS defaults = OPTIONS.merge options tgr = EngTagger.new tagged_text = tgr.get_readable(text) return unless tagged_text tag_word_pairs = tagged_text.split.map { |tagged_word| create_tag_word_pairs tagged_word } fillable_indices = find_fillable_indices(tag_word_pairs).shuffle.take defaults[:num_of_blanks] generate_output tag_word_pairs, fillable_indices, defaults[:html_form] end