Class: Momblish
- Inherits:
-
Object
show all
- Defined in:
- lib/momblish.rb,
lib/momblish/corpus.rb,
lib/momblish/version.rb,
lib/momblish/corpus_analyzer.rb
Defined Under Namespace
Modules: WeightedSample
Classes: Corpus, CorpusAnalyzer, EmptyCorpusError, Error
Constant Summary
collapse
- DICT =
{
'english' => ['/usr/share/dict/words', '/usr/dict/words', '/usr/share/dict/web2'],
'simple' => ["#{__dir__}/corpuses/simple.txt"],
'names' => ['/usr/share/dict/propernames', '/usr/dict/propernames'],
'spanish' => ["#{__dir__}/corpuses/spanish.txt"]
}
- VERSION =
"0.2.2"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(corpus = nil) ⇒ Momblish
Returns a new instance of Momblish.
51
52
53
54
55
56
57
|
# File 'lib/momblish.rb', line 51
def initialize(corpus = nil)
@corpus = corpus || Corpus.new({}, {})
if @corpus.weighted_bigrams.empty? || @corpus.occurrences.empty?
raise EmptyCorpusError.new('Your corpus has no words')
end
end
|
Instance Attribute Details
#corpus ⇒ Object
Returns the value of attribute corpus.
49
50
51
|
# File 'lib/momblish.rb', line 49
def corpus
@corpus
end
|
Class Method Details
.lookup_dict(lang) ⇒ Object
34
35
36
|
# File 'lib/momblish.rb', line 34
def lookup_dict(lang)
DICT[lang].find { |location| File.exist?(location) }
end
|
.method_missing(lang) ⇒ Object
38
39
40
41
42
43
44
45
46
|
# File 'lib/momblish.rb', line 38
def method_missing(lang)
if(DICT.has_key?(lang.to_s))
dict_file = lookup_dict(lang.to_s)
corpus = Momblish::CorpusAnalyzer.new(File.readlines(dict_file)).corpus
new(corpus)
else
super
end
end
|
Instance Method Details
#sentence(count = nil, word_length: nil) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/momblish.rb', line 77
def sentence(count = nil, word_length: nil)
return enum_for(:sentence, count, word_length: word_length) unless block_given?
if count.nil?
loop { yield word(word_length) }
else
count.times { yield word(word_length) }
end
end
|
#word(length = nil) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/momblish.rb', line 59
def word(length = nil)
length ||= rand(4..12)
word = @corpus.weighted_bigrams.weighted_sample
(length - 2).times do
last_bigram = word[-2..-1]
next_letter = @corpus.occurrences[last_bigram].weighted_sample
return word.downcase if next_letter.nil?
word += next_letter
end
word.downcase
end
|