Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Created July 7, 2016 20:38
Show Gist options
  • Save DataKinds/95f7eba6d3fc87d1919a5bc7988fb96e to your computer and use it in GitHub Desktop.
Save DataKinds/95f7eba6d3fc87d1919a5bc7988fb96e to your computer and use it in GitHub Desktop.
random word generator (with multiple methods, based off of a previous markov generator)
require 'json'
TRUELOOKFORWARD = true
LOOKFORWARD = 2 #how many words forward it generates from a seed word
def prepareString(str)
#return str.downcase.split(/(\W)+/).map{|word| word.gsub(/\s+/, "")}.reject{|word| word == ""} to remind you what it was
return str.split("").push("").unshift("")
end
def addArrayToChain(strArray, chain)
strArray.each_with_index do |currentStr, index|
doneWithString = false
LOOKFORWARD.times do |t|
if strArray[index + t + 1].nil?
doneWithString = true
end
end #break if no more strings can be found up ahead
break if doneWithString
if TRUELOOKFORWARD
if index - (LOOKFORWARD - 1) <= 0
currentStr = ""
else
currentStrArray = []
LOOKFORWARD.times do |lookforwardIndex|
currentStrArray.unshift(strArray[index - lookforwardIndex])
end
currentStr = currentStrArray.join("")
end
end
nextStringsArray = [] #make an array of the next strings
LOOKFORWARD.times do |t| nextStringsArray << strArray[index + t + 1] end
nextStrings = nextStringsArray.join("")
if !chain.key?(currentStr)
chain[currentStr] = {}
end
if !chain[currentStr].key?(nextStrings)
chain[currentStr][nextStrings] = 0
end
chain[currentStr][nextStrings] += 1
end
#File.open("markov2lookaheadbackup.rson", "w") { |file| file.write(JSON.generate(chain)) }
return chain
end
def generateWords(chain)
#topicWords = topic.split(/\s+/).reject{|word| word.include? BOTNAME}.sort_by(&:length).select{|word| word.length > 2} #select suitable-looking words for the bot to talk about
topicWord = ""
#topicWords.each do |word| #try to make the bot talk about them
# if !chain[word].nil?
# topicWord = word
# break
# end
#end
outputString = []
outputString << topicWord #put the first seed word in
loop do
if !TRUELOOKFORWARD
lastWord = outputString.last.to_s.split("").last
lastWord = "" if lastWord.nil?
else
if outputString.length < LOOKFORWARD
lastWord = ""
else
lastWord = outputString[-1]
end
end
break if chain[lastWord].nil? #if there aren't words to complete the phrase
chanceTable = chain[lastWord]
cdf = 0 #calculating the weighted random, warning: gross
chanceTable.each do |weight|
cdf += weight[1]
end
randomInt = rand(cdf)
runningSum = 0
chanceTable.each_pair do |word, weight|
runningSum += weight
if runningSum > randomInt
outputString << word
break
end
end
break if outputString.last == ""
end
return outputString.join("")
end
begin
@permChain = JSON.parse(File.read("markov2truelookaheadbackup.rson"))
rescue
puts "Backup file not found"
@permChain = {}
File.open("/usr/share/dict/words").each_line do |word|
#`cat /usr/share/dict/words | head -n 50`.each_line do |word|
@permChain = addArrayToChain(prepareString(word), @permChain)
end
ensure
10000.times do
puts generateWords(@permChain)
end
File.open("markov2truelookaheadbackup.rson", "w") { |file| file.write(JSON.generate(@permChain)) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment