inferx 0.1.5 → 0.1.6
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.
- data/lib/inferx/adapter.rb +5 -4
- data/lib/inferx/categories.rb +6 -7
- data/lib/inferx/category.rb +17 -15
- data/lib/inferx/version.rb +1 -1
- data/lib/inferx.rb +8 -9
- metadata +3 -2
data/lib/inferx/adapter.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
class Inferx
|
2
2
|
class Adapter
|
3
3
|
|
4
|
-
# @param [Redis] an instance of Redis
|
5
|
-
# @param [String] namespace of keys to be used to Redis
|
4
|
+
# @param [Redis] redis an instance of Redis
|
5
|
+
# @param [String] namespace namespace of keys to be used to Redis
|
6
6
|
def initialize(redis, namespace = nil)
|
7
7
|
@redis = redis
|
8
8
|
@namespace = namespace
|
@@ -26,7 +26,7 @@ class Inferx
|
|
26
26
|
|
27
27
|
# Make the key for access to scores stored each by word.
|
28
28
|
#
|
29
|
-
# @param [Symbol] a category name
|
29
|
+
# @param [Symbol] category_name a category name
|
30
30
|
# @return [String] the key
|
31
31
|
def make_category_key(category_name)
|
32
32
|
"#{categories_key}:#{category_name}"
|
@@ -34,8 +34,9 @@ class Inferx
|
|
34
34
|
|
35
35
|
# Spawn an instance of any class.
|
36
36
|
#
|
37
|
-
# @param [Class] any class, constructor takes the instance of Redis to
|
37
|
+
# @param [Class] klass any class, constructor takes the instance of Redis to
|
38
38
|
# first argument, and takes the namespace to last argument
|
39
|
+
# @param [Array] args any arguments
|
39
40
|
# @return [Object] a instance of the class
|
40
41
|
def spawn(klass, *args)
|
41
42
|
klass.new(@redis, *args, @namespace)
|
data/lib/inferx/categories.rb
CHANGED
@@ -3,6 +3,7 @@ require 'inferx/category'
|
|
3
3
|
|
4
4
|
class Inferx
|
5
5
|
class Categories < Adapter
|
6
|
+
include Enumerable
|
6
7
|
|
7
8
|
# Get all category names.
|
8
9
|
#
|
@@ -11,9 +12,9 @@ class Inferx
|
|
11
12
|
(hkeys || []).map(&:to_sym)
|
12
13
|
end
|
13
14
|
|
14
|
-
# Get a category according name.
|
15
|
+
# Get a category according the name.
|
15
16
|
#
|
16
|
-
# @param [Symbol] category name
|
17
|
+
# @param [Symbol] category_name category name
|
17
18
|
# @return [Inferx::Category] category
|
18
19
|
def get(category_name)
|
19
20
|
raise ArgumentError, "'#{category_name}' is missing" unless hexists(category_name)
|
@@ -23,7 +24,7 @@ class Inferx
|
|
23
24
|
|
24
25
|
# Add categories.
|
25
26
|
#
|
26
|
-
# @param [Array<Symbol>] category names
|
27
|
+
# @param [Array<Symbol>] category_names category names
|
27
28
|
def add(*category_names)
|
28
29
|
@redis.pipelined do
|
29
30
|
category_names.each { |category_name| hsetnx(category_name, 0) }
|
@@ -32,7 +33,7 @@ class Inferx
|
|
32
33
|
|
33
34
|
# Remove categories.
|
34
35
|
#
|
35
|
-
# @param [Array<Symbol>] category names
|
36
|
+
# @param [Array<Symbol>] category_names category names
|
36
37
|
def remove(*category_names)
|
37
38
|
@redis.pipelined do
|
38
39
|
category_names.each { |category_name| hdel(category_name) }
|
@@ -40,12 +41,10 @@ class Inferx
|
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
43
|
-
include Enumerable
|
44
|
-
|
45
44
|
# Apply process for each category.
|
46
45
|
#
|
47
46
|
# @yield a block to be called for every category
|
48
|
-
# @yieldparam [Inferx::Category] category
|
47
|
+
# @yieldparam [Inferx::Category] category category
|
49
48
|
def each
|
50
49
|
all.each { |category_name| yield spawn(Category, category_name) }
|
51
50
|
end
|
data/lib/inferx/category.rb
CHANGED
@@ -3,22 +3,25 @@ require 'inferx/adapter'
|
|
3
3
|
class Inferx
|
4
4
|
class Category < Adapter
|
5
5
|
|
6
|
-
# @param [Redis] an instance of Redis
|
7
|
-
# @param [Symbol] a category name
|
8
|
-
# @param [String] namespace of keys to be used to Redis
|
6
|
+
# @param [Redis] redis an instance of Redis
|
7
|
+
# @param [Symbol] name a category name
|
8
|
+
# @param [String] namespace namespace of keys to be used to Redis
|
9
9
|
def initialize(redis, name, namespace = nil)
|
10
10
|
super(redis, namespace)
|
11
11
|
@name = name
|
12
12
|
end
|
13
13
|
|
14
|
+
# Get a category name.
|
15
|
+
#
|
16
|
+
# @attribute [r] name
|
17
|
+
# @return [Symbol] a category name
|
14
18
|
attr_reader :name
|
15
19
|
|
16
20
|
# Get words with scores in the category.
|
17
21
|
#
|
18
22
|
# @param [Hash] options
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
23
|
+
# @option options [Integer] :score lower limit for getting by score
|
24
|
+
# @option options [Integer] :rank upper limit for getting by rank
|
22
25
|
# @return [Hash<String, Integer>] words with scores
|
23
26
|
def all(options = {})
|
24
27
|
words_with_scores = if score = options[:score]
|
@@ -41,10 +44,9 @@ class Inferx
|
|
41
44
|
|
42
45
|
# Get score of a word.
|
43
46
|
#
|
44
|
-
# @param [String] a word
|
45
|
-
# @return [Integer,
|
46
|
-
#
|
47
|
-
# - when the word is not member, nil
|
47
|
+
# @param [String] word a word
|
48
|
+
# @return [Integer] when the word is member, score of the word
|
49
|
+
# @return [nil] when the word is not member
|
48
50
|
def get(word)
|
49
51
|
score = zscore(word)
|
50
52
|
score ? score.to_i : nil
|
@@ -53,7 +55,7 @@ class Inferx
|
|
53
55
|
|
54
56
|
# Enhance the training data giving words.
|
55
57
|
#
|
56
|
-
# @param [Array<String>] words
|
58
|
+
# @param [Array<String>] words an array of words
|
57
59
|
def train(words)
|
58
60
|
@redis.pipelined do
|
59
61
|
increase = collect(words).inject(0) do |count, pair|
|
@@ -67,7 +69,7 @@ class Inferx
|
|
67
69
|
|
68
70
|
# Attenuate the training data giving words.
|
69
71
|
#
|
70
|
-
# @param [Array<String>] words
|
72
|
+
# @param [Array<String>] words an array of words
|
71
73
|
def untrain(words)
|
72
74
|
decrease = 0
|
73
75
|
|
@@ -97,9 +99,9 @@ class Inferx
|
|
97
99
|
|
98
100
|
# Get effectively scores for each word.
|
99
101
|
#
|
100
|
-
# @param [Array<String>] words
|
101
|
-
# @param [Hash<String, Integer>] words with scores
|
102
|
-
# reduce access to Redis
|
102
|
+
# @param [Array<String>] words a set of words
|
103
|
+
# @param [Hash<String, Integer>] words_with_scores words with scores
|
104
|
+
# prepared in advance for reduce access to Redis
|
103
105
|
# @return [Array<Integer>] scores for each word
|
104
106
|
def scores(words, words_with_scores = {})
|
105
107
|
scores = @redis.pipelined do
|
data/lib/inferx/version.rb
CHANGED
data/lib/inferx.rb
CHANGED
@@ -5,11 +5,10 @@ require 'inferx/categories'
|
|
5
5
|
|
6
6
|
class Inferx
|
7
7
|
|
8
|
-
# @param [Hash] options
|
9
|
-
# -
|
8
|
+
# @param [Hash] options other options are passed to Redis#initialize in
|
9
|
+
# {https://github.com/redis/redis-rb redis}
|
10
10
|
#
|
11
|
-
#
|
12
|
-
# https://github.com/redis/redis-rb
|
11
|
+
# @option options [String] :namespace namespace of keys to be used to Redis
|
13
12
|
def initialize(options = {})
|
14
13
|
namespace = options.delete(:namespace)
|
15
14
|
redis = Redis.new(options)
|
@@ -20,8 +19,8 @@ class Inferx
|
|
20
19
|
|
21
20
|
# Get a score of a category according to a set of words.
|
22
21
|
#
|
23
|
-
# @param [Inferx::Category] a category for scoring
|
24
|
-
# @param [Array<String>] a set of words
|
22
|
+
# @param [Inferx::Category] category a category for scoring
|
23
|
+
# @param [Array<String>] words a set of words
|
25
24
|
# @return [Float] a score of the category
|
26
25
|
def score(category, words)
|
27
26
|
size = category.size.to_f
|
@@ -33,7 +32,7 @@ class Inferx
|
|
33
32
|
|
34
33
|
# Get a score for each category according to a set of words.
|
35
34
|
#
|
36
|
-
# @param [Array<String>] a set of words
|
35
|
+
# @param [Array<String>] words a set of words
|
37
36
|
# @return [Hash<Symbol, Float>] scores to key a category
|
38
37
|
#
|
39
38
|
# @see #score
|
@@ -42,9 +41,9 @@ class Inferx
|
|
42
41
|
Hash[@categories.map { |category| [category.name, score(category, words)] }]
|
43
42
|
end
|
44
43
|
|
45
|
-
# Classify words to any one category
|
44
|
+
# Classify words to any one category.
|
46
45
|
#
|
47
|
-
# @param [Array<String>] a set of words
|
46
|
+
# @param [Array<String>] words a set of words
|
48
47
|
# @return [Symbol] most high-scoring category name
|
49
48
|
#
|
50
49
|
# @see #score
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inferx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
@@ -97,3 +97,4 @@ test_files:
|
|
97
97
|
- spec/inferx/category_spec.rb
|
98
98
|
- spec/inferx_spec.rb
|
99
99
|
- spec/spec_helper.rb
|
100
|
+
has_rdoc:
|