Module: Scripsi

Defined in:
lib/scripsi.rb

Defined Under Namespace

Classes: SortedSuffixIndexer

Constant Summary collapse

@@partition_size =
10

Class Method Summary collapse

Class Method Details

.connect(options = {}) ⇒ Object

connect to a redis server



6
7
8
# File 'lib/scripsi.rb', line 6

def self.connect(options = {})
  @@redis = Redis.new(options)
end

.find(id) ⇒ Object



52
53
54
# File 'lib/scripsi.rb', line 52

def self.find(id)
  indexer(id)
end

.indexer(id) ⇒ Object

get the indexer with the given id



44
45
46
47
48
49
# File 'lib/scripsi.rb', line 44

def self.indexer(id)
  type = Scripsi.redis.hget "scripsi:used", id.to_s
  if type == "ssi"
    SortedSuffixIndexer.build(id)
  end
end

.partition_sizeObject



16
17
18
# File 'lib/scripsi.rb', line 16

def self.partition_size
  @@partition_size
end

.redisObject



10
11
12
# File 'lib/scripsi.rb', line 10

def self.redis
  @@redis
end

.score(str) ⇒ Number

generate a ‘score’ for a string used for storing it in a sorted set

This method effectively turns a string into a base 27 floating point number, where 0 corresponds to no letter, 1 to A, 2 to B, etc.

Parameters:

  • str (String)

    the string we are computing a score for

Returns:

  • (Number)

    the string’s score



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scripsi.rb', line 28

def self.score(str)
  str = str.downcase
  scrs = []
  str.split('').each_slice(partition_size) do |s|
    mult = 1.0
    scr = 0.0
    s.each do |char|
      mult /= 27
      scr += (char.ord-'a'.ord+1)*mult if ('a'..'z').include? char
    end
    scrs << scr
  end
  scrs
end