-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.rb
57 lines (41 loc) · 1.18 KB
/
generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'thread'
require 'socket'
require_relative 'thread-pool.rb'
require_relative 'markov-chains.rb'
# Usage statement and check for proper number of command line arguments
unless ARGV.length > 6
puts 'Usage: ruby generator.rb host_address host_ port number_threads order number_of_sentences input_file'
puts 'Can have multiple input_files'
exit
end
# initiate all variables
p = Pool.new(ARGV[2].to_i)
host = ARGV[0]
port = ARGV[1]
order = ARGV[3].to_i
number_sentences = ARGV[4].to_i
socket = TCPSocket.open(host,port)
# shift the command line arguments off the array to be able to use ARGF
# to read in the file for the dictionary
5.times {
ARGV.shift
}
# Create a Markov Chain
markov = MarkovChainer.new(order)
# Read in all input txt files and add them to the Markov Chain
markov.add_text(ARGF.read)
# Schedule threads from the thread pool to
# generate sentences and send them to the target
# listening host
number_sentences.times {
p.schedule do
socket.puts markov.generate_sentence
end
}
# Depracated random sentence code
#while 1 == 1 do
# p.schedule do
# socket.puts (1..40).map { (("a".."z").to_a + [" "] * 20)[rand(56)] }.join
# end
#end
at_exit { p.shutdown }