Skip to content

Instantly share code, notes, and snippets.

@drichert
Created July 22, 2013 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drichert/6057330 to your computer and use it in GitHub Desktop.
Save drichert/6057330 to your computer and use it in GitHub Desktop.
Shuffle nouns
class Shuffler
attr_reader :words, :pos, :nouns, :noun_indexes
def initialize(words)
@words = words
@pos = Moby::PartsOfSpeech.new
end
def shuffle_nouns
words.each_with_index.map do |word, ndx|
if noun?(strip_punc(word))
nouns.shuffle!.shift
else
word
end
end
end
private
def noun?(word)
pos.noun?(word)
end
def strip_punc(word)
word.match(/\A[^\w]*(?<word>\w+)[^\w]*\z/i)[:word]
end
def noun_indexes
@noun_indexes ||= words.each_with_index.map do |word, ndx|
ndx if noun?(strip_punc(word))
end.compact
end
def nouns
@nouns ||= words.select {|word| noun?(strip_punc(word)) }
end
end
if __FILE__ == $0
str = "Since Gettier, 'knowledge' is no longer widely accepted as meaning 'justified true belief'. However, many epistemologists still consider knowledge to have a justification condition. Traditional theories of justification (foundationalism and coherentism) and indeed many philosophers consider an infinite regress not to be a valid justification. In their view, if A is justified by B, B by C, and so forth, then either (a) the chain must end with a link that requires no independent justification (a foundation), or (b) the chain must come around in a circle in some finite number of steps (the belief may be justified by its coherence) or (c) our beliefs must not be justified after all (as skeptics believe)."
shuf = Shuffler.new(str.split(/\s+/))
puts shuf.shuffle_nouns.join(" ")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment