picky 4.5.6 → 4.5.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,8 @@ module Picky
9
9
 
10
10
  if thing.respond_to? :weight_for
11
11
  thing
12
+ elsif thing.respond_to? :to_int
13
+ Generators::Weights::Logarithmic.new thing
12
14
  else
13
15
  raise <<-ERROR
14
16
  weight options for #{index_name}:#{name} should be either
@@ -14,13 +14,15 @@ module Picky
14
14
  :prepared,
15
15
  :backend
16
16
 
17
- # Mandatory params:
17
+ # Parameters:
18
18
  # * name: Category name to use as identifier and file names.
19
19
  # * index: Index to which this category is attached to.
20
20
  #
21
21
  # Options:
22
- # * partial: Partial::None.new, Partial::Substring.new(from:start_char, to:up_to_char) (defaults from:-3, to:-1)
23
- # * similarity: Similarity::None.new (default), Similarity::DoubleMetaphone.new(amount_of_similarly_linked_words)
22
+ # * partial: Partial::None.new, Partial::Substring.new(from:start_char, to:up_to_char)
23
+ # (defaults from:-3, to:-1)
24
+ # * similarity: Similarity::None.new (default),
25
+ # Similarity::DoubleMetaphone.new(amount_of_similarly_linked_words)
24
26
  # * from: The source category identifier to take the data from.
25
27
  # * key_format: What this category's keys are formatted with (default is :to_i)
26
28
  # * backend: The backend to use. Default is Backends::Memory.new.
@@ -28,10 +30,11 @@ module Picky
28
30
  # * qualifiers: Which qualifiers can be used to predefine the category. E.g. "title:bla".
29
31
  #
30
32
  # Advanced Options:
31
- # * weight: Query::Weights.new( [:category1, :category2] => +2, ... )
32
- # * tokenizer: Use a subclass of Tokenizers::Base that implements #tokens_for and #empty_tokens.
33
- # * use_symbols: Whether to use symbols internally instead of strings.
34
33
  # * source: Use if the category should use a different source.
34
+ # * tokenizer: Use a subclass of Tokenizers::Base that implements #tokens_for and #empty_tokens.
35
+ # * weight: Weights::Logarithmic.new, Weights::Constant.new(int = 0),
36
+ # Weights::Dynamic.new(&block) or an object that responds
37
+ # to #weight_for(amount_of_ids_for_token) and returns a float.
35
38
  #
36
39
  def initialize name, index, options = {}
37
40
  @name = name
@@ -3,24 +3,40 @@ module Picky
3
3
  module Generators
4
4
 
5
5
  module Weights
6
-
6
+
7
7
  # Uses a logarithmic weight.
8
+ #
9
+ # If given a constant, this will be added to the weight.
10
+ #
8
11
  # If for a key k we have x ids, the weight is:
9
12
  # w(x): log(x)
10
13
  # Special case: If x < 1, then we use 0.
11
14
  #
12
15
  class Logarithmic < Strategy
13
-
14
- # Sets the weight value.
15
- #
16
- # If the size is 0 or one, we would get -Infinity or 0.0.
17
- # Thus we do not set a value if there is just one. The default, dynamically, is 0.
18
- #
19
- # BUT: We need the value, even if 0. To designate that there IS a weight!
20
- #
21
- def weight_for amount
22
- return 0 if amount < 1
23
- Math.log(amount).round 3
16
+
17
+ def initialize constant = 0.0
18
+ # Note: Optimisation since it is called
19
+ # once per indexed object.
20
+ #
21
+ if constant == 0.0
22
+ def weight_for amount
23
+ return 0 if amount < 1
24
+ Math.log(amount).round 3
25
+ end
26
+ else
27
+ @constant = constant
28
+ # Sets the weight value.
29
+ #
30
+ # If the size is 0 or one, we would get -Infinity or 0.0.
31
+ # Thus we do not set a value if there is just one. The default, dynamically, is 0.
32
+ #
33
+ # BUT: We need the value, even if 0. To designate that there IS a weight!
34
+ #
35
+ def weight_for amount
36
+ return @constant if amount < 1
37
+ @constant + Math.log(amount).round(3)
38
+ end
39
+ end
24
40
  end
25
41
 
26
42
  end
@@ -19,6 +19,14 @@ describe Picky::API::Category::Weight do
19
19
  object.extract_weight(nil).should == Picky::Weights::Default
20
20
  end
21
21
  end
22
+ context 'with a number' do
23
+ it 'returns a logarithmic weighter' do
24
+ object.extract_weight(7.3).should be_kind_of(Picky::Weights::Logarithmic)
25
+ end
26
+ it 'returns a logarithmic weighter' do
27
+ object.extract_weight(3.14).weight_for(10).should == 5.443 # ln(10) + 3.14 = 2.3025 + 3.14
28
+ end
29
+ end
22
30
  context 'with a weight object' do
23
31
  let(:weighter) do
24
32
  Class.new do
@@ -2,23 +2,47 @@ require 'spec_helper'
2
2
 
3
3
  describe Picky::Generators::Weights::Logarithmic do
4
4
 
5
- let(:logarithmic) { described_class.new }
5
+ describe 'with defaults' do
6
+ let(:logarithmic) { described_class.new }
6
7
 
7
- describe 'saved?' do
8
- it 'is correct' do
9
- logarithmic.saved?.should == true
8
+ describe 'saved?' do
9
+ it 'is correct' do
10
+ logarithmic.saved?.should == true
11
+ end
10
12
  end
11
- end
12
13
 
13
- describe 'weight_for' do
14
- it 'is 0 for 0' do
15
- logarithmic.weight_for(0).should == 0
14
+ describe 'weight_for' do
15
+ it 'is 0 for 0' do
16
+ logarithmic.weight_for(0).should == 0
17
+ end
18
+ it 'is 0 for 1' do
19
+ logarithmic.weight_for(1).should == 0
20
+ end
21
+ it 'is log(x) for x' do
22
+ logarithmic.weight_for(1234).should == Math.log(1234).round(3)
23
+ end
16
24
  end
17
- it 'is 0 for 1' do
18
- logarithmic.weight_for(1).should == 0
25
+ end
26
+
27
+ describe 'with constant' do
28
+ let(:logarithmic) { described_class.new(3.5) }
29
+
30
+ describe 'saved?' do
31
+ it 'is correct' do
32
+ logarithmic.saved?.should == true
33
+ end
19
34
  end
20
- it 'is log(x) for x' do
21
- logarithmic.weight_for(1234).should == Math.log(1234).round(3)
35
+
36
+ describe 'weight_for' do
37
+ it 'is 0 for 0' do
38
+ logarithmic.weight_for(0).should == 3.5
39
+ end
40
+ it 'is 0 for 1' do
41
+ logarithmic.weight_for(1).should == 3.5
42
+ end
43
+ it 'is log(x) for x' do
44
+ logarithmic.weight_for(1234).should == Math.log(1234).round(3) + 3.5
45
+ end
22
46
  end
23
47
  end
24
48
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picky
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.5.6
4
+ version: 4.5.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-18 00:00:00.000000000 Z
12
+ date: 2012-07-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70251933705120 !ruby/object:Gem::Requirement
16
+ requirement: &70151588753500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,21 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70251933705120
24
+ version_requirements: *70151588753500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: picky-client
27
- requirement: &70251933703520 !ruby/object:Gem::Requirement
27
+ requirement: &70151588752540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 4.5.6
32
+ version: 4.5.7
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70251933703520
35
+ version_requirements: *70151588752540
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: text
38
- requirement: &70251933701920 !ruby/object:Gem::Requirement
38
+ requirement: &70151588750500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70251933701920
46
+ version_requirements: *70151588750500
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: multi_json
49
- requirement: &70251933698920 !ruby/object:Gem::Requirement
49
+ requirement: &70151588748300 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70251933698920
57
+ version_requirements: *70151588748300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activesupport
60
- requirement: &70251933714040 !ruby/object:Gem::Requirement
60
+ requirement: &70151588762580 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '3.0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70251933714040
68
+ version_requirements: *70151588762580
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: procrastinate
71
- requirement: &70251933712600 !ruby/object:Gem::Requirement
71
+ requirement: &70151588760860 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0.4'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70251933712600
79
+ version_requirements: *70151588760860
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rack_fast_escape
82
- requirement: &70251933711460 !ruby/object:Gem::Requirement
82
+ requirement: &70151588759980 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70251933711460
90
+ version_requirements: *70151588759980
91
91
  description: Fast Ruby semantic text search engine with comfortable single field interface.
92
92
  email: florian.hanke+picky@gmail.com
93
93
  executables: