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 |
# 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) # 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] 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
66 67 68 |
# File 'lib/mongoid-haystack/token.rb', line 66 def next_hex_id "0x#{ hex = sequence.next.to_s(16) }" end |
.sequence ⇒ Object
62 63 64 |
# File 'lib/mongoid-haystack/token.rb', line 62 def sequence Sequence.for(Token.name.scan(/[^:]+/).join('.').downcase) end |
.subtract(tokens) ⇒ Object
59 60 |
# File 'lib/mongoid-haystack/token.rb', line 59 def subtract(tokens) end |
.total ⇒ Object
70 71 72 |
# File 'lib/mongoid-haystack/token.rb', line 70 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
82 83 84 85 86 87 88 |
# File 'lib/mongoid-haystack/token.rb', line 82 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
90 91 92 |
# File 'lib/mongoid-haystack/token.rb', line 90 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
94 95 96 |
# File 'lib/mongoid-haystack/token.rb', line 94 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
98 99 100 101 102 103 104 |
# File 'lib/mongoid-haystack/token.rb', line 98 def rarity_bin(n_tokens = Token.total.value.to_f) if n_tokens.zero? Float::Infinity else (rarity(n_tokens) * 10).truncate end end |