Skip to content

Commit

Permalink
Added filter option to suggestions method
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 23, 2021
1 parent 774fb81 commit 2c52694
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.1.2 (unreleased)

- Added `filter` option to `suggestions` method
- Improved performance

## 0.1.1 (2021-03-15)
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ autosuggest.block_words ["boom"]
Get suggestions with:

```ruby
autosuggest.suggestions
autosuggest.suggestions(filter: true)
```

Filter queries without results and you’re set. We also prefer to have someone manually approve them by hand.
Expand All @@ -112,8 +112,7 @@ autosuggest.block_words ["boom"]

puts autosuggest.pretty_suggestions
# or
suggestions = autosuggest.suggestions
.reject { |s| s[:duplicate] || s[:misspelling] || s[:profane] || s[:blocked] }
suggestions = autosuggest.suggestions(filter: true)
```

## History
Expand Down
9 changes: 7 additions & 2 deletions lib/autosuggest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def prefer(queries)
end
end

def suggestions
# TODO add queries method for filter: false and make suggestions use filter: true in 0.2.0
def suggestions(filter: false)
stemmed_queries = {}
added_queries = Set.new
@top_queries.sort_by { |_query, count| -count }.map do |query, count|
results = @top_queries.sort_by { |_query, count| -count }.map do |query, count|
query = query.to_s

# TODO do not ignore silently
Expand Down Expand Up @@ -144,6 +145,10 @@ def suggestions
result[:notes] = notes
result
end
if filter
results.reject! { |s| s[:duplicate] || s[:misspelling] || s[:profane] || s[:blocked] }
end
results
end

def pretty_suggestions
Expand Down
8 changes: 8 additions & 0 deletions test/autosuggest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,12 @@ def test_long_query
autosuggest = Autosuggest.new(top_queries)
assert autosuggest.suggestions
end

def test_filter
top_queries = {"tomato" => 2, "tomatoes" => 1}
autosuggest = Autosuggest.new(top_queries)
suggestions = autosuggest.suggestions(filter: true)
assert_equal 1, suggestions.size
assert_equal "tomato", suggestions.first[:query]
end
end

0 comments on commit 2c52694

Please sign in to comment.