markov_rand_words 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/markov_chain.rb +70 -0
  2. metadata +45 -0
@@ -0,0 +1,70 @@
1
+ # This class implements a Markov Chain for the given text (could be from a book)
2
+ # It does so using a hash of hashes which is build from each pair of words in the
3
+ # text. In the first hash, the first word is the key and the second hash is the value.
4
+ # In the second hash, the second word is the key and the number of times it occurs after
5
+ # the first word is the value.
6
+ # An online search for "Rails Markov Chain" will yield similar implementations.
7
+
8
+ class MarkovChain
9
+ def initialize(text)
10
+ @words = Hash.new
11
+ @beginings =[]
12
+ wordlist = text.split
13
+ # add each pair of words to hash
14
+ wordlist.each_with_index do |word, index|
15
+ add(word, wordlist[index+1]) if index <= wordlist.size - 2
16
+ end
17
+ end
18
+
19
+ # this does the actual add to the hash
20
+ def add(word1, word2)
21
+ # strip quotes
22
+ word1 = word1.gsub(/['""]/,'')
23
+ word2 = word2.gsub(/['""]/,'')
24
+ # beginings contains sentence beginings. end_with? does not take regexes.
25
+ (@beginings << word2) if (word1.end_with?(".") || word1.end_with?("!") || word1.end_with?("?"))
26
+ @words[word1] = Hash.new(0) if !@words[word1]
27
+ @words[word1][word2] += 1
28
+ end
29
+
30
+ # Given a word, this retrieves a plausible next word
31
+ def getnext(word)
32
+ return "" if !@words[word]
33
+ @words[word].key(@words[word].values.sample)
34
+ end
35
+
36
+ # Get 3 words. Used for captions/links/title
37
+ def words(count = 3, start_word = nil)
38
+ result = ''
39
+ word = start_word || @beginings.sample.capitalize
40
+ count.times do
41
+ result << word << ' '
42
+ word = getnext(word).capitalize
43
+ end
44
+ result.strip
45
+ end
46
+
47
+ # get 5 sentences
48
+ def sentences(count = 5, start_word = nil)
49
+ word = start_word || @beginings.sample.capitalize
50
+ sentences = ""
51
+ sen_cnt = 0
52
+ word_cnt = 1
53
+ until sen_cnt == count
54
+ # keep going till we get a ending or cap at 25 words.
55
+ until( (sentences.count("/[.?!]/") == (sen_cnt + 1)) || word_cnt == 25)
56
+ word_cnt += 1
57
+ sentences << word << " "
58
+ word = getnext(word)
59
+ end
60
+ sen_cnt += 1
61
+ word = @beginings.sample.capitalize
62
+ word_cnt = 1
63
+ end
64
+ sentences.strip
65
+ end
66
+ end
67
+
68
+
69
+
70
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markov_rand_words
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vishwanath Mantha
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-05 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Random Words based on Markov Chain
15
+ email: vmantha@contextoptional.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/markov_chain.rb
21
+ homepage: http://rubygems.org/gems/markov_rand_words
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.10
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Random Words
45
+ test_files: []