Class: Ebooks::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ebooks/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator

Returns a new instance of Generator.



6
7
8
9
10
11
12
# File 'lib/ebooks/generator.rb', line 6

def initialize(config)
  @tweets_csv_path = config[:tweets_csv_path]
  @corpus_path     = config[:corpus_path]
  build_corpus
  @dictionary_name = config[:dictionary_name]
  @dictionary      = build_dictionary
end

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



4
5
6
# File 'lib/ebooks/generator.rb', line 4

def dictionary
  @dictionary
end

Instance Method Details

#generate_sentenceObject



30
31
32
33
# File 'lib/ebooks/generator.rb', line 30

def generate_sentence
  # Run when you want to generate a new Markov tweet
  dictionary.generate_n_sentences(2).split(/\#\</).first.chomp.chop
end

#generate_twitter_corpusObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ebooks/generator.rb', line 14

def generate_twitter_corpus
  # Go to Twitter.com -> Settings -> Download Archive.
  # This tweets.csv file is in the top directory. Put it in the same directory as this script.
  csv_text = CSV.parse(File.read(@tweets_csv_path))

  # Create a new clean file of text that acts as the seed for your Markov chains
  File.open(@corpus_path, 'w') do |file|
    csv_text.reverse.each do |row|
      # Strip links and new lines
      tweet_text = row[5].gsub(/(?:f|ht)tps?:\/[^\s]+/, '').gsub(/\n/,' ')
      # Save the text
      file.write("#{tweet_text}\n")
    end
  end
end