initial commit

This commit is contained in:
Alexander Munch-Hansen 2017-12-14 23:47:33 +01:00
commit 3370e78c50
7 changed files with 215 additions and 0 deletions

33
cat_facts/bot.rb Normal file
View File

@ -0,0 +1,33 @@
require 'cinch'
file = File.open "cat_facts.html", "r"
a = file.read.split "<li"
l = a.collect do |s|
if s.include? "id"
s.slice s.index("id"), s.index("<sub>")
end
end
l.delete_at 0
done = l.collect do |s|
if s.include? "id"
s.slice( s.index(">") + 1 .. s.index("<") - 1 )
end
end
bot = Cinch::Bot.new do
configure do |c|
c.nick = 'CatFacts^'
c.server = 'server'
c.port = port
c.channels = ['#bot-test']
c.ssl.use = true
end
on :message, /(?i)(.*).*cat.*/ do |m|
m.reply done.sample
end
end
bot.start

2
cat_facts/cat_facts.txt Normal file
View File

@ -0,0 +1,2 @@
Unlike dogs, cats do not have a sweet tooth. Scientists believe this is due to a mutation in a key taste receptor#
When a cat chases its prey, it keeps its head level. Dogs and humans bob their heads up and down#

View File

@ -0,0 +1,33 @@
# coding: utf-8
require 'cinch'
bot = Cinch::Bot.new do
configure do |c|
c.nick = 'MrRollBot'
c.server = 'irc.guava.space'
c.port = 6697
c.channels = ['#bot-test', '#chat']
c.ssl.use = true
end
on :message, /^!roll/ do |m|
random_int = rand(0..100).to_s
m.params[1].match /^!roll ([0-9]*)-([0-9]*)/ do |numbers|
if not numbers[1].nil? then
random_int = rand(numbers[1].to_i..numbers[2].to_i).to_s
end
end
m.reply random_int
end
on :message, /!coin/ do |m|
probs = rand(1..10).to_i
if probs < 5 then
m.reply "Heads"
else
m.reply "Tails"
end
end
end
bot.start

View File

@ -0,0 +1,22 @@
# coding: utf-8
require 'cinch'
bot = Cinch::Bot.new do
configure do |c|
c.nick = 'GoodTable'
c.server = 'irc.guava.space'
c.port = 6697
c.channels = ['#bot-test']
c.ssl.use = true
end
on :message, '(╯°□°)╯︵ ┻━┻' do |m|
m.reply '┯━┯ノ(º₋ºノ) plz be kind to table'
end
on :message, 'ಠ_ಠ' do |m|
m.reply '( ͡° ͜ʖ ͡°)'
end
end
bot.start

View File

@ -0,0 +1,36 @@
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|
words = words.collect { |s| s.gsub(/["()]/, "") }
self.add_word_to_dict words[0..-2], words[-1]
end
end
end
end

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 = 30
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

39
taylor_bot/taylor_bot.rb Normal file
View File

@ -0,0 +1,39 @@
require 'cinch'
require_relative 'DictionaryGenerator'
require_relative 'SentenceGenerator'
require_relative 'config'
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 "-------------------------------------"
bot = Cinch::Bot.new do
configure do |c|
c.nick = 'TaylorSwift'
c.server = SERVER_ADDRESS
c.port = PORT
c.channels = CHANNELS
c.ssl.use = true
end
on :message, /.*(?i)(.).*taylor.*/ do |m|
to_reply = sentenceGen.generate_sentence
sec_to_sleep = to_reply.size * 0.01+1
sleep(sec_to_sleep)
m.reply to_reply
end
end
bot.start