Skip to content
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

recognize single author with multi word last name and no first initials #116

Merged
merged 1 commit into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions tugboat/tests/tests_unit/test_search_redirect.py
Expand Up @@ -102,6 +102,46 @@ def test_authors(self):
'&format=SHORT' + '/',
author_search) # authors with or

# first author
req.args = MultiDict([('author', urllib.parse.quote('^Huchra, John'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + 'author:' + '"Huchra, John"' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # first author no quotes

# single author
req.args = MultiDict([('author', urllib.parse.quote('^Huchra, John$'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + '(author:"Huchra, John" and author_count:1)' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # single author no quotes

# multi word last name author, no initials
req.args = MultiDict([('author', urllib.parse.quote('^Dorigo Jones'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + 'author:"Dorigo Jones,"' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # single author, no initials, no quotes

# multi word last name author, no initials, quoted
req.args = MultiDict([('author', urllib.parse.quote('"^Dorigo Jones"'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + 'author:"Dorigo Jones,"' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # single author, no initials, with quotes

def test_object(self):
"""object: single, multple, etc"""
req = Request('get', 'http://test.test?')
Expand Down
29 changes: 23 additions & 6 deletions tugboat/views.py
Expand Up @@ -514,12 +514,29 @@ def translate_authors(self, args):
authors_str = args.pop('author', None)
if authors_str:
authors = self.classic_field_to_array(authors_str)
# is it a single author search: ^last, first$
match = re.findall(r'\^(.*)\$', ' '.join(authors))
# yes
if match:
search = '(author:"' + match[0] + '" and author_count:1)'
self.translation.search.append(search)
single_author = ' '.join(authors).replace('"', '')
if re.match(r'\^', single_author):
# is it a single author search: ^last, first$
match = re.findall(r'\^([^$]*)\$', ' '.join(authors).replace('"', ''))
# yes
if match:
search = '(author:"' + match[0] + '" and author_count:1)'
self.translation.search.append(search)
else:
# is it first author search: ^last, first
match = re.findall(r'\^([A-Za-z]+,\s+[A-Za-z]+)', ' '.join(authors).replace('"', ''))
# yes
if match:
search = 'author:"' + match[0] + '"'
self.translation.search.append(search)
else:
# is it first author search, but with no first initials
match = re.findall(r'\^([A-Za-z\s]+)', ' '.join(authors).replace('"', ''))
# yes
if match:
search = 'author:"' + match[0] + ',"'
self.translation.search.append(search)
# multiple authors
else:
search += urllib.parse.quote(author_field) + '('
for author in authors:
Expand Down