Module: Runestone
- Defined in:
- lib/runestone.rb,
lib/runestone/version.rb
Defined Under Namespace
Modules: ActiveRecord, Corpus Classes: Engine, IndexingJob, Model, Settings, WebSearch
Constant Summary collapse
- DEFAULT_APPROXIMATIONS =
{ "À"=>"A", "Á"=>"A", "Â"=>"A", "Ã"=>"A", "Ä"=>"A", "Å"=>"A", "Æ"=>"AE", "Ç"=>"C", "È"=>"E", "É"=>"E", "Ê"=>"E", "Ë"=>"E", "Ì"=>"I", "Í"=>"I", "Î"=>"I", "Ï"=>"I", "Ð"=>"D", "Ñ"=>"N", "Ò"=>"O", "Ó"=>"O", "Ô"=>"O", "Õ"=>"O", "Ö"=>"O", "×"=>"x", "Ø"=>"O", "Ù"=>"U", "Ú"=>"U", "Û"=>"U", "Ü"=>"U", "Ý"=>"Y", "Þ"=>"Th", "ß"=>"ss", "à"=>"a", "á"=>"a", "â"=>"a", "ã"=>"a", "ä"=>"a", "å"=>"a", "æ"=>"ae", "ç"=>"c", "è"=>"e", "é"=>"e", "ê"=>"e", "ë"=>"e", "ì"=>"i", "í"=>"i", "î"=>"i", "ï"=>"i", "ð"=>"d", "ñ"=>"n", "ò"=>"o", "ó"=>"o", "ô"=>"o", "õ"=>"o", "ö"=>"o", "ø"=>"o", "ù"=>"u", "ú"=>"u", "û"=>"u", "ü"=>"u", "ý"=>"y", "þ"=>"th", "ÿ"=>"y", "Ā"=>"A", "ā"=>"a", "Ă"=>"A", "ă"=>"a", "Ą"=>"A", "ą"=>"a", "Ć"=>"C", "ć"=>"c", "Ĉ"=>"C", "ĉ"=>"c", "Ċ"=>"C", "ċ"=>"c", "Č"=>"C", "č"=>"c", "Ď"=>"D", "ď"=>"d", "Đ"=>"D", "đ"=>"d", "Ē"=>"E", "ē"=>"e", "Ĕ"=>"E", "ĕ"=>"e", "Ė"=>"E", "ė"=>"e", "Ę"=>"E", "ę"=>"e", "Ě"=>"E", "ě"=>"e", "Ĝ"=>"G", "ĝ"=>"g", "Ğ"=>"G", "ğ"=>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ary collapse
- .add_synonym(word, *replacements) ⇒ Object
- .add_synonyms(dictionary) ⇒ Object
- .normalize(string) ⇒ Object
- .normalize!(string) ⇒ Object
Instance Method Summary collapse
- #search(query, dictionary: nil, prefix: :last, normalization: nil) ⇒ Object
- #transliterate(string) ⇒ Object
Class Method Details
.add_synonym(word, *replacements) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/runestone.rb', line 74 def self.add_synonym(word, *replacements) word = normalize(word) replacements.map! { |r| normalize(r) } word = word.split(/\s+/) last = word.pop syn = synonyms word.each do |w| syn = if syn.has_key?(w) && h = syn[w].find { |i| i.is_a?(Hash) } h else h = {} syn[w] ||= [] syn[w] << h h end end syn[last] ||= [] syn[last] += replacements syn[last].uniq! end |
.add_synonyms(dictionary) ⇒ Object
68 69 70 71 72 |
# File 'lib/runestone.rb', line 68 def self.add_synonyms(dictionary) dictionary.each do |k, v| add_synonym(k, *v) end end |
.normalize(string) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/runestone.rb', line 50 def self.normalize(string) string = string.downcase string = string.unicode_normalize! string rescue Encoding::CompatibilityError string end |
.normalize!(string) ⇒ Object
58 59 60 61 62 |
# File 'lib/runestone.rb', line 58 def self.normalize!(string) string.downcase! string.unicode_normalize! rescue Encoding::CompatibilityError end |
Instance Method Details
#search(query, dictionary: nil, prefix: :last, normalization: nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/runestone.rb', line 98 def search(query, dictionary: nil, prefix: :last, normalization: nil) exact_search = Runestone::WebSearch.parse(query, prefix: prefix) typo_search = exact_search.typos syn_search = typo_search.synonymize tsqueries = [exact_search, typo_search, syn_search].map(&:to_s).uniq.map do |q| ts_query(q, dictionary: dictionary) end q = if select_values.empty? select( klass.arel_table[Arel.star], *tsqueries.each_with_index.map { |q, i| Arel::Nodes::As.new(ts_rank_cd(:vector, q, dictionary: dictionary, normalization: normalization), Arel::Nodes::SqlLiteral.new("rank#{i}")) } ) else select( *tsqueries.each_with_index.map { |q, i| Arel::Nodes::As.new(ts_rank_cd(:vector, q, dictionary: dictionary, normalization: normalization), Arel::Nodes::SqlLiteral.new("rank#{i}")) } ) end q = if klass == Runestone::Model q.where(ts_match(:vector, tsqueries.last, dictionary: dictionary)) else q.joins(:runestones).where(ts_match(TS::Model.arel_table['vector'], tsqueries.last, dictionary: dictionary)) end q = q.where(dictionary: dictionary) if dictionary q.order( *tsqueries.each_with_index.map { |q, i| Arel::Nodes::Descending.new(Arel::Nodes::SqlLiteral.new("rank#{i}")) } ) end |
#transliterate(string) ⇒ Object
64 65 66 |
# File 'lib/runestone.rb', line 64 def transliterate(string) string.gsub(/[^\x00-\x7f]/u) { |char| approximations[char] || char } end |