love_is 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c47bfbb06c55765459c1f9080d35bf098b855233
4
+ data.tar.gz: 3aa2c252c09af30c32125841ca39fb528e3c6c84
5
+ SHA512:
6
+ metadata.gz: 12271194b7454d53fa39ea62e7ad3eefeecce2c49fb84d3cf341c52c45176336c857f03b395796f6d5a643c53cc5a4a599191b2417b70ffd1043eb051ca6e67b
7
+ data.tar.gz: 3d2adc3c9478b84b5409b56ecd3992e3ba2d19882583c0297d32f00d1b5d02b0ba4415381441d27099202fa94d1a9f5e8473e233d4b31016b2e95061ecf5053c
@@ -0,0 +1,32 @@
1
+ class LoveIs::Generator
2
+ include LoveIs::Parser
3
+ attr_reader :ngram, :chain, :tree
4
+
5
+ def initialize(ngram = 3)
6
+ @tree = LoveIs::Node.new(nil, 1, [])
7
+ @ngram = ngram
8
+ @chain = []
9
+ end
10
+
11
+ def generate(chain_length = 1000)
12
+ @chain = []
13
+ initial_values
14
+ (chain_length - ngram).times do
15
+ break unless word = next_value
16
+ @chain << word
17
+ end
18
+ chain[1..-1].join(' ')
19
+ end
20
+
21
+ def initial_values
22
+ ngram.times.inject(@tree.child('*')) do |node|
23
+ @chain << node.value
24
+ node.next
25
+ end
26
+ end
27
+
28
+ def next_value
29
+ node = @tree[*chain[(-ngram + 1)..-1]]
30
+ node ? node.next.value : nil
31
+ end
32
+ end
@@ -0,0 +1,62 @@
1
+ class LoveIs::Node
2
+ attr_reader :value
3
+ attr_accessor :probability, :childs, :weight
4
+
5
+ def initialize(value = nil, weight = 1, childs = [])
6
+ @value = value
7
+ @weight = weight
8
+ set_childs(childs)
9
+ end
10
+
11
+ def set_childs(childs)
12
+ @childs = childs
13
+ unless @childs.all? { |x| x.probability }
14
+ probabilities
15
+ end
16
+ end
17
+
18
+ def probabilities
19
+ normalize_weights
20
+ t = 0.0
21
+ @childs.map! { |x| t += x.weight; x.probability = t; x }
22
+ @childs.each(&:probabilities) if childs && childs.any?
23
+ end
24
+
25
+ def normalize_weights
26
+ sum = weights.inject(&:+).to_f
27
+ @childs.map! { |x| x.weight /= sum; x }
28
+ end
29
+
30
+ def child(key)
31
+ childs.find { |x| x.value == key }
32
+ end
33
+
34
+ def [](*keys)
35
+ keys.inject(self) { |r, x| break(nil) unless r; r.child(x) }
36
+ end
37
+
38
+ def []=(*keys, weight)
39
+ keys.inject(self) do |r, x|
40
+ r.childs << LoveIs::Node.new(x, 0, []) unless r.child(x)
41
+ r.child(x).weight = weight
42
+ r.child(x)
43
+ end
44
+ end
45
+
46
+ def inc(*keys)
47
+ keys.inject(self) do |r, x|
48
+ r.childs << LoveIs::Node.new(x, 0, []) unless r.child(x)
49
+ r.child(x).weight += 1
50
+ r.child(x)
51
+ end
52
+ end
53
+
54
+ def weights
55
+ childs.map { |x| x.weight }
56
+ end
57
+
58
+ def next
59
+ random_value = Random.new.rand
60
+ childs.find { |x| random_value - x.probability <= 0 }
61
+ end
62
+ end
@@ -0,0 +1,20 @@
1
+ module LoveIs::Parser
2
+ SENTENCE_BEGIN = '*'
3
+
4
+ def parse_file(filename)
5
+ text = File.read(File.expand_path(filename))
6
+ parse(text, @ngram)
7
+ end
8
+
9
+ def parse(text, length)
10
+ text.split("\n").map { |x| sentence2array(x) }.each do |sentence|
11
+ sentence.each_cons(length) { |sequence| @tree.inc(*sequence) }
12
+ end
13
+ @tree.probabilities
14
+ self
15
+ end
16
+
17
+ def sentence2array(sentence)
18
+ sentence.split(/\s/).unshift(SENTENCE_BEGIN)
19
+ end
20
+ end
data/lib/love_is.rb ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module LoveIs
4
+ end
5
+
6
+ require_relative 'love_is/node'
7
+ require_relative 'love_is/parser'
8
+ require_relative 'love_is/generator'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: love_is
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Smagin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Markov chains generator.
14
+ email: smaginsergey1310@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/love_is.rb
20
+ - lib/love_is/generator.rb
21
+ - lib/love_is/node.rb
22
+ - lib/love_is/parser.rb
23
+ homepage: https://github.com/s-mage/love_is
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.3
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Love Is ...
47
+ test_files: []
48
+ has_rdoc: