-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Correct search count #259
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
module IiifPrint | ||
module BlacklightIiifSearch | ||
module AnnotationDecorator | ||
INVALID_MATCH_TEXT = "#xywh=INVALID,INVALID,INVALID,INVALID".freeze | ||
## | ||
# Create a URL for the annotation | ||
# use a Hyrax-y URL syntax: | ||
|
@@ -28,19 +29,30 @@ def canvas_uri_for_annotation | |
# @return [String] | ||
def coordinates | ||
return default_coords if query.blank? | ||
|
||
sanitized_query = sanitize_query | ||
coords_json = fetch_and_parse_coords | ||
return default_coords unless coords_json && coords_json['coords'] | ||
sanitized_query = query.match(additional_query_terms_regex)[1].strip | ||
|
||
coords_check_result = check_coords_json_and_properties(coords_json, sanitized_query) | ||
return coords_check_result if coords_check_result | ||
|
||
query_terms = sanitized_query.split(' ').map(&:downcase) | ||
|
||
matches = coords_json['coords'].select do |k, _v| | ||
k.downcase =~ /(#{query_terms.join('|')})/ | ||
end | ||
return default_coords if matches.blank? | ||
|
||
coords_array = matches.values.flatten(1)[hl_index] | ||
return default_coords unless coords_array | ||
|
||
"#xywh=#{coords_array.join(',')}" | ||
end | ||
|
||
def sanitize_query | ||
query.match(additional_query_terms_regex)[1].strip | ||
end | ||
|
||
## | ||
# return the JSON word-coordinates file contents | ||
# @return [JSON] | ||
|
@@ -54,6 +66,20 @@ def fetch_and_parse_coords | |
end | ||
end | ||
|
||
# This is a bit hacky but it is checking if any of the properties contain the query term | ||
# if there are no coords and there is a metadata property match | ||
# then we return the default coords | ||
# else we insert a invalid match text to be stripped out at a later point | ||
# @see IiifPrint::IiifSearchResponseDecorator#annotation_list | ||
def check_coords_json_and_properties(coords_json, sanitized_query) | ||
return if coords_json && coords_json['coords'] | ||
|
||
properties = @document.keys.select { |key| key.ends_with? "_tesim" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returning_value = INVALID_MATCH_TEXT
@document.each_pair do |key, value|
next if key.ends_with?("_tesim")
next unless value.join.downcase.include?(sanitized_query)
returning_value = default_coords
break
end
end
returning_value |
||
properties.each { |property| return default_coords if @document[property].join.downcase.include?(sanitized_query) } | ||
|
||
INVALID_MATCH_TEXT | ||
end | ||
|
||
## | ||
# a default set of coordinates | ||
# @return [String] | ||
|
@@ -97,6 +123,13 @@ def file_set_id | |
def additional_query_terms_regex | ||
/(.*)(?= AND (\(.+\)|\w+)$)/ | ||
end | ||
|
||
## | ||
# @return [IIIF::Presentation::Resource] | ||
def text_resource_for_annotation | ||
IIIF::Presentation::Resource.new('@type' => 'cnt:ContentAsText', | ||
'chars' => sanitize_query) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about moving the guard condition of the original method outside of the original method, as it has a surprising return value (based on the given method name).
return derived_coords_json_and_properties(coords_json, sanitized_query) unless coords_json['coords']