-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateTweet.rb
110 lines (95 loc) · 3.25 KB
/
CreateTweet.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module CreateTweet
require_relative 'current_state.rb'
require 'json'
dictionary = Hash.new
def create_dictionary(dict, text)
#Reading from the tweetText.txt
File.open(text).each do |line|
string_left_to_parse = line[0..line.length - 2] #making the line the right length
while !(string_left_to_parse.eql? nil) do #looping through every word
word = find_next_word(string_left_to_parse)
dict.store(word, Hash.new) #add word to the hashtable dictionary mapping to empty word_sets
string_left_to_parse = string_left_to_parse[(word.length + 1)..-1]
end
end
dict.store(".", Hash.new) #adding "." for start of sentences
#loop through dictionary to update word_sets
File.open(text).each do |line|
string_left_to_parse = line[0..line.length - 2] #making the line the right length
first_word = find_next_word(string_left_to_parse)
string_left_to_parse = string_left_to_parse[(first_word.length + 1)..-1]
last_word = "" #store the last word to put that info into the word_map
period_word_map = dict["."] #mapping the start of sentences "." to the first words
if(period_word_map[first_word].eql? nil)
period_word_map.store(first_word, 1)
else
period_num_appearances = period_word_map[first_word] + 1
period_word_map.store(first_word, period_num_appearances)
end
while !(string_left_to_parse.eql? nil) do #looping through every word
next_word = find_next_word(string_left_to_parse)
word_map = dict[first_word]
#puts word_map[next_word]
if(word_map[next_word].eql? nil)
word_map.store(next_word, 1)
else
if(next_word.eql? ".")
last_word = first_word
end
num_appearances = word_map[next_word] + 1
word_map.store(next_word, num_appearances)
end
#dict.store(first_word, Hash.new) #add word to the hashtable dictionary mapping to empty word_sets
first_word = next_word
string_left_to_parse = string_left_to_parse[(next_word.length + 1)..-1]
end
end
end
#Identifying different words in string
def find_next_word(text)
char = text[0]
if(char.eql? " ")
""
elsif(text.length == 1)
char
else
char + find_next_word(text[1..-1])
end
end
#output a word based on the probability of word appearing in a phrase
def choose_word(ptr_string, dictionary)
set = dictionary[ptr_string]
val_array = set.values
sum = val_array.inject(0){|sum,x| sum + x }
rand_num = rand(1..sum)
prev_sum = 0
range_array = val_array.map {|x| prev_sum += x}
index = 0
i = 0
while (rand_num > range_array[i]) do
i += 1
index += 1
end
key_array = set.keys
return key_array[index]
end
def write_to_json(dictionary, file_name)
create_dictionary(dictionary, "tweetText.txt")
File.open(file_name, 'w'){|f| f.write(dictionary.to_json)} #write this to the json file
end
#write_to_json(dictionary, "tweet_text.json")
def make_tweet()
tweet_file = File.read("tweet_text.json")
dictionary = JSON.parse(tweet_file)
tweet = ""
cur_state = new_state(".", ".")
new_word = choose_word(cur_state[1], dictionary)
while(tweet.length < 141) #while(!(new_word.eql? "."))
next_state = shift_in(cur_state, new_word)
tweet = tweet + next_state[1] + " "
cur_state = next_state
new_word = choose_word(cur_state[1], dictionary)
end
puts tweet
end
end