Class: SolrLite::Spellcheck

Inherits:
Object
  • Object
show all
Defined in:
lib/spellcheck.rb

Instance Method Summary collapse

Constructor Details

#initialize(solr_reponse_hash) ⇒ Spellcheck

Returns a new instance of Spellcheck.



3
4
5
# File 'lib/spellcheck.rb', line 3

def initialize(solr_reponse_hash)
  @spellcheck = solr_reponse_hash.fetch("spellcheck", {})
end

Instance Method Details

#collationsObject



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
# File 'lib/spellcheck.rb', line 11

def collations()
  @collations ||= begin
    collations = @spellcheck.fetch("collations",nil)
    if collations != nil
      if collations.kind_of?(Array)
        # We must be in Solr6, use the collation information as-is
      else
        # uh-oh...
        []
      end
    else
      # We must be on Solr4, mimic the structure of the Solr6 results
      # which is an array in the form:
      #
      #   ["collation", {"collationQuery": "wordA"}, "collation", {"collationQuery": "wordB"}, ...]
      #
      # As a reference, the structure in Solr4 is slightly different in that
      # the collationQuery information is in an array within an array:
      #
      #   ["collation", ["collationQuery", "wordA"], "collation"["collationQuery", "wordB"], ...]
      #
      collations = []
      suggestions = suggestions()
      suggestions.each_with_index do |x, i|
        if x == "collation"
          collationQuery = suggestions[i+1]
          word = collationQuery[1]
          collations << "collation"
          collations << {"collationQuery" => word}
        end
      end
    end
    collations
  end
end

#suggestionsObject



7
8
9
# File 'lib/spellcheck.rb', line 7

def suggestions()
  @suggestions ||= @spellcheck.fetch("suggestions",[])
end

#top_collation_queryObject

def spellcheck_correctly_spelled()

@spellcheck.fetch("correctlySpelled", true)

end



51
52
53
54
55
56
# File 'lib/spellcheck.rb', line 51

def top_collation_query()
  colls = collations()
  return nil if colls.length < 2
  top_collation = colls[1] || {}
  top_collation.fetch("collationQuery", nil)
end