Bot which is somewhat capable of generating sentences

This commit is contained in:
Alexander Munch-Hansen 2017-11-01 01:02:35 +01:00
commit 852251c6d6
6 changed files with 229 additions and 0 deletions

34
DictionaryGenerator.rb Normal file
View File

@ -0,0 +1,34 @@
class DictionaryGenerator
attr_reader :dictionary, :depth
def initialize( depth )
@dictionary = {}
@capitalized_words = []
@depth = depth
@split_words = /(\.\s+)|(\.$)|([?!])|[\s]+/
@split_sentence = /(?<=[.!?])\s+/
end
def add_word_to_dict( keyword, followedby )
@dictionary[keyword] ||= []
@dictionary[keyword] << followedby
end
def insert_text( text )
input_sentences = text.split @split_sentence
if input_sentences.empty? then raise "insert_text called without input" end
unless input_sentences[-1].strip[-1].match /[.!?]/ then
input_sentences[-1] = input_sentences[-1].strip + '.'
end
input_sentences.each do |sentence|
sentence.split(@split_words).each_cons(@depth + 1) do |words|
self.add_word_to_dict words[0..-2], words[-1]
end
end
end
end

50
SentenceGenerator.rb Normal file
View File

@ -0,0 +1,50 @@
class NullObject
def method_missing (*args, &block)
self
end
def nil?; true; end
def to_str; end
def to_ary; []; end
end
NULL_OBJECT = NullObject.new
class SentenceGenerator
def initialize(dictionary)
@dictionary = dictionary
@depth = @dictionary.depth
end
def random_word(last_word)
@dictionary.dictionary.fetch(last_word, NULL_OBJECT).sample
end
def is_terminator?(symbol)
(symbol =~ /[!?]/ || symbol == '.')
end
# Ugly way of making sure the key starts with a capital letter
def random_capitalized_word
random_choice = ["temp"]
until random_choice[0] =~ /[A-Z]/
words = @dictionary.dictionary.keys
random_choice = words[rand(words.length)]
end
random_choice
end
def generate_sentence()
sentence = []
maximum_length = 20
wordcount = 0
sentence.concat(random_capitalized_word)
until (is_terminator?(sentence.last[-1])) || wordcount > maximum_length
wordcount += 1
word = random_word(sentence.last(@depth))
sentence << word
end
sentence.join(' ')
end
end

47
love_story.txt Normal file
View File

@ -0,0 +1,47 @@
We were both young when I first saw you
I close my eyes and the flashback starts
I'm standing there on a balcony in summer air
See the lights, see the party, the ball gowns
See you make your way through the crowd
And say hello
Little did I know
That you were Romeo, you were throwing pebbles
And my daddy said, "Stay away from Juliet"
And I was crying on the staircase
Begging you, please, don't go
And I said,
"Romeo, take me somewhere we can be alone
I'll be waiting, all that's left to do is run
You'll be the prince and I'll be the princess
It's a love story, baby just say yes
So I sneak out to the garden to see you
We keep quiet 'cause we're dead if they knew
So close your eyes, escape this town for a little while
'Cause you were Romeo, I was a scarlet letter
And my daddy said "Stay away from Juliet"
But you were everything to me, I was begging you, please, don't go
And I said Romeo take me somewhere we can be alone
I'll be waiting, all there's left to do is run
You'll be the prince and I'll be the princess
It's a love story baby just say yes
Romeo save me, they're trying to tell me how to feel
This love is difficult, but it's real
Don't be afraid, we'll make it out of this mess
It's a love story, baby just say "Yes"
Oh, oh
I got tired of waiting
Wondering if you were ever coming around
My faith in you was fading
When I met you on the outskirts of town
And I said
"Romeo save me, I've been feeling so alone
I keep waiting for you but you never come
Is this in my head? I don't know what to think"
He knelt to the ground and pulled out a ring and said
"Marry me, Juliet, you'll never have to be alone
I love you and that's all I really know
I talked to your dad, go pick out a white dress
It's a love story, baby just say yes"
Oh, oh,
Oh, oh
'Cause we were both young when I first saw you.

41
our_song.txt Normal file
View File

@ -0,0 +1,41 @@
I was riding shotgun with my hair undone in the front seat of his car,
He's got a one-hand feel on the steering wheel,
The other on my heart,
I look around, turn the radio down, he says, "baby is something wrong?"
I say "nothing, I was just thinking how we don't have a song, "
And he says,
Our song is a slamming screen door,
Sneaking out late tapping on your window,
When we're on the phone and you talk real slow,
'Cause it's late and your mama don't know,
Our song is the way you laugh,
The first date man I didn't kiss her when I should have,
And when I got home, before I said amen,
Asking God if He could play it again.
I was walking up the front porch steps after everything that day,
Had gone all wrong, had been trampled on and lost and thrown away,
Got to the hallway well on my way to my lovin' bed,
I almost didn't notice all the roses,
And the note that said,
Our song is a slamming screen door,
Sneaking out late tapping on your window,
When we're on the phone and you talk real slow,
'Cause it's late and your mama don't know,
Our song is the way you laugh,
The first date man I didn't kiss her when I should have,
And when I got home, before I said amen,
Asking God if He could play it again.
I've heard every album, listened to the radio,
Waiting for something to come along,
That was as good as our song,
'Cause our song is a slamming screen door,
Sneaking out late tapping on your window,
When we're on the phone, and he talks real slow,
'Cause it's late and his mama don't know,
Our song is the way he laughs,
The first date man I didn't kiss him when I should have,
And when I got home, before I said amen,
Asking God if He could play it again.
I was riding shotgun with my hair undone in the front seat of his car,
I grabbed a pen and an old napkin
And I wrote down our song.

40
shouldve_said_no.txt Normal file
View File

@ -0,0 +1,40 @@
It's strange to think the songs we used to sing
The smiles, the flowers, everything: is gone
Yesterday I found out about you
Even now just looking at you: feels wrong
You say that you'd take it all back, given one chance
It was a moment of weakness and you said yes
You should've said no, you should've gone home
You should've thought twice before you let it all go
You should've know that word, bout what you did with her
Would get back to me
And I should've been there, in the back of your mind
I shouldn't be asking myself why
You shouldn't be begging for forgiveness at my feet
You should've said no, baby and you might still have me
You can see that I've been crying
And baby you know all the right things: to say
But do you honestly expect me to believe
We could ever be the same
You say that the past is the past, you need one chance
It was a moment of weakness and you said yes
You should've said no, you should've gone home
You should've thought twice before you let it all go
You should've know that word, bout what you did with her
Would get back to me
And I should've been there, in the back of your mind
I shouldn't be asking myself why
You shouldn't be begging for forgiveness at my feet
You should've said no, baby and you might still have me
I can't resist, before you go, tell me this
Was it worth it
Was she worth this
No, no no no
You should've said no, you should've gone home
You should've thought twice before you let it all go
You should've know that word, bout what you did with her
Would get back to me
And I should've been there, in the back of your mind
I shouldn't be asking myself why
You shouldn't be begging for forgiveness at my feet
You should've said no, baby and you might still have me

17
taylor_bot.rb Normal file
View File

@ -0,0 +1,17 @@
require_relative 'DictionaryGenerator'
require_relative 'SentenceGenerator'
love_file = File.open("love_story.txt","r").read
our_file = File.open("our_song.txt","r").read
shouldve_file = File.open('shouldve_said_no.txt','r').read
text = love_file + our_file + shouldve_file
@dictionary = DictionaryGenerator.new(2)
@dictionary.insert_text text
@sentenceGen = SentenceGenerator.new(@dictionary)
puts "-------------------------------------"
puts @sentenceGen.generate_sentence()
puts "-------------------------------------"