Sha256: 7655e10a5fc4b00c5ffb6385b1f8b305f05be365427913de728b3cbda3b6db42

Contents?: true

Size: 1.25 KB

Versions: 28

Compression:

Stored size: 1.25 KB

Contents

module CloudSesame
  module Query
    module Node
      class Fuzziness

        def initialize(&block)

          # default fuzziness
          @max_fuzziness = 3
          @min_char_size = 6
          @fuzzy_percent = 0.17

          instance_eval &block if block_given?
        end

        def max_fuzziness(int)
          @max_fuzziness = int.to_i
        end

        def min_char_size(int)
          @min_char_size = int.to_i
        end

        def fuzzy_percent(float)
          @fuzzy_percent = float.to_f
        end

        def parse(string)
          join_by_and each_with(string) { |word| fuzziness word }
        end

        private

        def each_with(string, &block)
          string.split(' ').map &block
        end

        def fuzziness(word)
          if word.length >= @min_char_size && !excluding_term?(word)
            fuzziness = (word.length * @fuzzy_percent).round
            fuzziness = [fuzziness, @max_fuzziness].min
            "#{word}~#{fuzziness}"
          else
            word
          end
        end

        def join_by_and(args = [])
          (args = args.compact).size > 1 ? "(#{ args.join('+') })" : args[0]
        end

        def excluding_term?(word)
          !!word.match(/^\-/)
        end

      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
CloudSesame-0.6.4 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.6.3 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.6.2 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.6.1 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.6.0 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.5.5 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.5.4 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.5.3 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.5.2 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.5.1 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.5.0 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.4.6 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.4.5 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.4.4 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.4.3 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.4.2 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.4.1 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.4.0 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.3.6 lib/cloud_sesame/query/node/fuzziness.rb
CloudSesame-0.3.5 lib/cloud_sesame/query/node/fuzziness.rb