markov_chains 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/markov_chains/dictionary.rb +28 -26
- data/lib/markov_chains/version.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a5502df6fbd5d786c29c3c73eeccc2aadf024a8
|
4
|
+
data.tar.gz: ad36913b85e82c09e51e2d4737554c3b05a05f6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9a1b39ef2e706d7228f610c05100cd98faf8d60fe03bc26193fe7b0700952a09b791da004986488e327644582b9386518ce6fd9c0964e354756ac42e0896387
|
7
|
+
data.tar.gz: 9de8d90926e2bb63ff65f34bed0d826c4081829946c1aa013c614c5d696b290c4cb4ab758e411c89fbcce1098a74bcf1e183ac0cd18dd5a4265fa964642d9fa0
|
@@ -28,32 +28,6 @@ module MarkovChains
|
|
28
28
|
sentences = text.split seps
|
29
29
|
sentences.each_slice(2) { |s,t| process_sentence(s.strip,t) }
|
30
30
|
end
|
31
|
-
|
32
|
-
# Processes a single sentence with terminator
|
33
|
-
#
|
34
|
-
# @example Process a sentence
|
35
|
-
# process_sentence("It is sunny today", "!")
|
36
|
-
#
|
37
|
-
# @param [String] sentence to process
|
38
|
-
# @param [Character] sentence terminator
|
39
|
-
#
|
40
|
-
private def process_sentence(sentence, terminator)
|
41
|
-
# Consider phrases/words/clauses separators when splitting
|
42
|
-
seps = "([,;:])"
|
43
|
-
|
44
|
-
# Split <sentence> into words
|
45
|
-
words = sentence.gsub(/[^#{seps}\w'\s]/, "").gsub(/(#{seps})\s+/, '\1').split(/\s+|#{seps}/)
|
46
|
-
words << terminator
|
47
|
-
|
48
|
-
# Add <@order> start words to the list
|
49
|
-
@start_words << words[0, @order]
|
50
|
-
|
51
|
-
# Add the words to the frequency hash <words_for>
|
52
|
-
until words.size < @order + 1 do
|
53
|
-
(@words_for[words[0, @order]] ||= []) << words[@order]
|
54
|
-
words.shift
|
55
|
-
end
|
56
|
-
end
|
57
31
|
|
58
32
|
# Returns a word based on the likelihood of it appearing after the input array of words
|
59
33
|
#
|
@@ -79,5 +53,33 @@ module MarkovChains
|
|
79
53
|
def get_start_words
|
80
54
|
@start_words.sample
|
81
55
|
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
# Processes a single sentence with terminator
|
60
|
+
#
|
61
|
+
# @example Process a sentence
|
62
|
+
# process_sentence("It is sunny today", "!")
|
63
|
+
#
|
64
|
+
# @param [String] sentence to process
|
65
|
+
# @param [Character] sentence terminator
|
66
|
+
#
|
67
|
+
def process_sentence(sentence, terminator)
|
68
|
+
# Consider phrases/words/clauses separators when splitting
|
69
|
+
seps = "([,;:])"
|
70
|
+
|
71
|
+
# Split <sentence> into words
|
72
|
+
words = sentence.gsub(/[^#{seps}\w'\s]/, "").gsub(/(#{seps})\s+/, '\1').split(/\s+|#{seps}/)
|
73
|
+
words << terminator
|
74
|
+
|
75
|
+
# Add <@order> start words to the list
|
76
|
+
@start_words << words[0, @order]
|
77
|
+
|
78
|
+
# Add the words to the frequency hash <words_for>
|
79
|
+
until words.size < @order + 1 do
|
80
|
+
(@words_for[words[0, @order]] ||= []) << words[@order]
|
81
|
+
words.shift
|
82
|
+
end
|
83
|
+
end
|
82
84
|
end
|
83
85
|
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module MarkovChains
|
2
|
-
VERSION = '0.1.
|
3
|
-
end
|
2
|
+
VERSION = '0.1.1'
|
3
|
+
end
|