Class: Mongoid::Haystack::Token
- Inherits:
-
Object
- Object
- Mongoid::Haystack::Token
- Includes:
- Document
- Defined in:
- lib/mongoid-haystack/token.rb
Class Method Summary collapse
- .add(value) ⇒ Object
- .next_hex_id ⇒ Object
- .sequence ⇒ Object
- .subtract(tokens) ⇒ Object
- .total ⇒ Object
- .values_for(*args) ⇒ Object
Instance Method Summary collapse
- #frequency(n_tokens = Token.total.value.to_f) ⇒ Object
- #frequency_bin(n_tokens = Token.total.value.to_f) ⇒ Object
- #rarity(n_tokens = Token.total.value.to_f) ⇒ Object
- #rarity_bin(n_tokens = Token.total.value.to_f) ⇒ Object
Class Method Details
.add(value) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mongoid-haystack/token.rb', line 11 def add(value) # handle a value or array of values - which may contain dups # values = Array(value) values.flatten! values.compact! # ensure that a token exists for each value seen # existing = where(:value.in => values) missing = values - existing.map(&:value) docs = missing.map{|value| {:_id => Token.next_hex_id, :value => value}} unless docs.empty? collection = mongo_session.with(:safe => false)[collection_name] collection.insert(docs, [:continue_on_error]) end # new we should have one token per uniq value # tokens = where(:value.in => values).to_a # batch update the counts on the tokens by the number of times each # value was seen in the list # # 'dog dog' #=> increment the 'dog' token's count by 2 # counts = {} token_index = tokens.inject({}){|hash, token| hash[token.value] = token; hash} value_index = values.inject({}){|hash, value| hash[value] ||= []; hash[value].push(value); hash} values.each do |value| token = token_index[value] unless token token = Token.create!(:value => value) value_index[value] ||= [] value_index[value].push(value) end count = value_index[value].size counts[count] ||= [] counts[count].push(token.id) end counts.each do |count, token_ids| Token.where(:id.in => token_ids).inc(:count, count) end # return an array or single token depending on whether a list or # single value was added # value.is_a?(Array) ? tokens : tokens.first end |
.next_hex_id ⇒ Object
73 74 75 |
# File 'lib/mongoid-haystack/token.rb', line 73 def next_hex_id "0x#{ hex = sequence.next.to_s(16) }" end |
.sequence ⇒ Object
69 70 71 |
# File 'lib/mongoid-haystack/token.rb', line 69 def sequence Sequence.for(Token.name.scan(/[^:]+/).join('.').downcase) end |
.subtract(tokens) ⇒ Object
66 67 |
# File 'lib/mongoid-haystack/token.rb', line 66 def subtract(tokens) end |
.total ⇒ Object
77 78 79 |
# File 'lib/mongoid-haystack/token.rb', line 77 def total sum(:count) end |
.values_for(*args) ⇒ Object
7 8 9 |
# File 'lib/mongoid-haystack/token.rb', line 7 def values_for(*args) Haystack.tokens_for(*args) end |
Instance Method Details
#frequency(n_tokens = Token.total.value.to_f) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/mongoid-haystack/token.rb', line 89 def frequency(n_tokens = Token.total.value.to_f) if n_tokens.zero? Float::Infinity else (count / n_tokens).round(2) end end |
#frequency_bin(n_tokens = Token.total.value.to_f) ⇒ Object
97 98 99 |
# File 'lib/mongoid-haystack/token.rb', line 97 def frequency_bin(n_tokens = Token.total.value.to_f) (frequency(n_tokens) * 10).truncate end |
#rarity(n_tokens = Token.total.value.to_f) ⇒ Object
101 102 103 |
# File 'lib/mongoid-haystack/token.rb', line 101 def rarity(n_tokens = Token.total.value.to_f) ((n_tokens - count) / n_tokens).round(2) end |
#rarity_bin(n_tokens = Token.total.value.to_f) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/mongoid-haystack/token.rb', line 105 def rarity_bin(n_tokens = Token.total.value.to_f) if n_tokens.zero? Float::Infinity else (rarity(n_tokens) * 10).truncate end end |