Skip to content

Instantly share code, notes, and snippets.

@jnewland
Created October 23, 2008 23:21
Show Gist options
  • Save jnewland/19267 to your computer and use it in GitHub Desktop.
Save jnewland/19267 to your computer and use it in GitHub Desktop.
commit 95101dc9d5a1cd201db25cca25b3847710759930
Author: Jesse Newland <[email protected]>
Date: Thu Oct 23 19:23:28 2008 -0400
don't negate dash-preceded parts of words
diff --git a/lib/scoped_search/query_language_parser.rb b/lib/scoped_search/query_language_parser.rb
index d68e22b..2eb3973 100644
--- a/lib/scoped_search/query_language_parser.rb
+++ b/lib/scoped_search/query_language_parser.rb
@@ -87,10 +87,12 @@ module ScopedSearch
tokens = []
matches = query.scan(pattern).flatten.compact
matches.each { |match|
- tokens << :not unless match.index('-').nil?
- # Remove any escaped quotes and any dashes - the dash usually the first character.
+ tokens << :not if match.index('-') == 0
+ # Remove any escaped quotes
+ # Remove any dashes preceded by a space or at the beginning of a token
# Remove any additional spaces - more that one.
- tokens << match.gsub(/[-"]/,'').gsub(/[ ]{2,}/, ' ')
+ cleaned_token = match.gsub(/"/,'').gsub(/^-| -/,'').gsub(/[ ]{2,}/, ' ')
+ tokens << cleaned_token if cleaned_token.length > 0
}
return tokens
end
diff --git a/lib/scoped_search/reg_tokens.rb b/lib/scoped_search/reg_tokens.rb
index 6755920..d10e94a 100644
--- a/lib/scoped_search/reg_tokens.rb
+++ b/lib/scoped_search/reg_tokens.rb
@@ -1,7 +1,7 @@
# Regular expression tokens to be used for parsing.
module RegTokens
- WORD = '[\w]+'
+ WORD = '[\w-]+'
SPACE = '[ ]'
STRING = '["][\w ]+["]'
OR = 'OR'
diff --git a/test/query_language_test.rb b/test/query_language_test.rb
index 86b2d56..69ef346 100644
--- a/test/query_language_test.rb
+++ b/test/query_language_test.rb
@@ -76,7 +76,17 @@ class QueryLanguageTest < Test::Unit::TestCase
assert_equal 1, parsed.length
assert_equal 'willem', parsed[0].first
assert_equal :not, parsed[0].last
-
+
+ parsed = parse_query('hallo-world')
+ assert_equal 1, parsed.length
+ assert_equal 'hallo-world', parsed.first.first
+
+ parsed = parse_query('hallo -world')
+ assert_equal 2, parsed.length
+ assert_equal 'hallo', parsed.first.first
+ assert_equal 'world', parsed.last.first
+ assert_equal :not, parsed.last.last
+
parsed = parse_query('123 -"456 789"')
assert_equal 2, parsed.length
assert_equal '123', parsed[0].first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment