Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Handles name suffixes. Returns empty array when creating name variati… (
Browse files Browse the repository at this point in the history
#365)

…ons.

closes #363
  • Loading branch information
justinlittman authored and mjgiarlo committed Jan 24, 2019
1 parent 6640717 commit e6dcf13
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 35 deletions.
59 changes: 31 additions & 28 deletions lib/rialto/etl/configs/wos_to_sparql_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ def fetch_publishers(json)
JsonPath.on(json, '$.static_data.summary.publishers.publisher.names.name.display_name')
end

# Returns a person for a contributor
# rubocop:disable Metrics/MethodLength
def construct_contributor(name, addresses)
address = lookup_address(addresses, name['addr_no'])
person_params = name.slice('orcid_id', 'first_name', 'last_name', 'full_name')
person_params.merge!(address) if address
person = Rialto::Etl::Transformers::People.resolve_or_construct_person(given_name: name['first_name'] || name['suffix'],
family_name: name['last_name'],
addl_params: person_params)

# Note: Adding a country for each publication, so person may have many countries.
if address&.key?('country') &&
(country = Rialto::Etl::Transformers::Addresses.construct_country(country: address['country']))
person[RDF::Vocab::DC.spatial.to_s] = country
end

# If there is an organization, add a position for the person
if address&.key?('organization')
person_id = remove_vocab_from_uri(RIALTO_PEOPLE, person['@id'])
person['#organization'] = Rialto::Etl::Transformers::People.construct_position(org_name: address['organization'],
person_id: person_id)
end
person
end
# rubocop:enable Metrics/MethodLength

# Lookup the address from this document by the internal (to the document) addr_id
# @param addresses [Hash] a lookup between the addr_no and the data
# @param addr_id [Integer,String,NilClass] the address identifier to lookup
Expand Down Expand Up @@ -125,45 +151,22 @@ def lookup_address(addresses, addr_id)
}, single: true

to_field "!#{VIVO.relatedBy}", literal(true), single: true
# rubocop:disable Metrics/BlockLength
to_field VIVO.relatedBy.to_s, lambda { |json, accumulator|
addresses = fetch_addresses(json)
# Lookup all the contributors in the entity resolution service to find their URIs.
contributors = Array.wrap(JsonPath.on(json, '$.static_data.summary.names.name').first)
authorships = contributors.map do |c|
address = lookup_address(addresses, c['addr_no'])
person_params = c.slice('orcid_id', 'first_name', 'last_name', 'full_name')
person_params.merge!(address) if address

person = Rialto::Etl::Transformers::People.resolve_or_construct_person(given_name: c['first_name'],
family_name: c['last_name'],
addl_params: person_params)
person_id = remove_vocab_from_uri(RIALTO_PEOPLE, person['@id'])
# Note: Adding a country for each publication, so person may have many countries.
if address &&
address.key?('country') &&
(country = Rialto::Etl::Transformers::Addresses.construct_country(country: address['country']))
person[RDF::Vocab::DC.spatial.to_s] = country
end

# If there is an organization, add a position for the person
if address && address.key?('organization')
person['#organization'] = Rialto::Etl::Transformers::People.construct_position(org_name: address['organization'],
person_id: person_id)
end

contributor_names = Array.wrap(JsonPath.on(json, '$.static_data.summary.names.name').first)
accumulator << contributor_names.map do |name|
person = construct_contributor(name, addresses)
{
'@id' => RIALTO_CONTEXT_RELATIONSHIPS["#{json['UID']}_#{person_id}"],
'@type' => c['role'] == 'book_editor' ? VIVO.Editorship : VIVO.Authorship,
'@id' => RIALTO_CONTEXT_RELATIONSHIPS["#{json['UID']}_#{remove_vocab_from_uri(RIALTO_PEOPLE, person['@id'])}"],
'@type' => name['role'] == 'book_editor' ? VIVO.Editorship : VIVO.Authorship,
"!#{VIVO.relates}" => true,
VIVO.relates.to_s => person['@id'],
# Always add with # since always adding country for all people.
'#person' => person
}
end
accumulator << authorships
}, single: true
# rubocop:enable Metrics/BlockLength

to_field "!#{RDF::Vocab::DC.subject}", literal(true), single: true
to_field RDF::Vocab::DC.subject.to_s, lambda { |json, accumulator|
Expand Down
8 changes: 4 additions & 4 deletions lib/rialto/etl/transformers/people.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ def self.construct_person(id: nil, given_name:, middle_name: nil, family_name:)
full_name = names_constructor.fullname_from_names(given_name: given_name,
middle_name: middle_name,
family_name: family_name)
{
person = {
'@id' => Rialto::Etl::Vocabs::RIALTO_PEOPLE[id],
'@type' => [RDF::Vocab::FOAF.Agent, RDF::Vocab::FOAF.Person],
RDF::Vocab::SKOS.prefLabel.to_s => full_name,
RDF::Vocab::RDFS.label.to_s => full_name,
RDF::Vocab::SKOS.altLabel.to_s => name_variations_from_names(given_name: given_name,
middle_name: middle_name,
family_name: family_name),
# Name VCard
RDF::Vocab::VCARD.hasName.to_s => names_constructor.construct_name_vcard(id: id,
given_name: given_name,
middle_name: middle_name,
family_name: family_name)
}
name_variations = name_variations_from_names(given_name: given_name, middle_name: middle_name, family_name: family_name)
person[RDF::Vocab::SKOS.altLabel.to_s] = name_variations if name_variations
person
end
# rubocop:enable Metrics/MethodLength

Expand Down
2 changes: 1 addition & 1 deletion lib/rialto/etl/transformers/people/names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def fullname_from_names(given_name:, middle_name: nil, family_name:)
def name_variations_from_names(given_name:, middle_name: nil, family_name:)
name_variations = Set.new
# Check that a string because WoS sometimes return True
return name_variations unless given_name&.is_a?(String)
return [] unless given_name&.is_a?(String)
name_variations << "#{family_name}, #{given_name}"
name_variations << "#{given_name} #{family_name}"
given_initial = given_name[0]
Expand Down
63 changes: 63 additions & 0 deletions spec/configs/wos_to_sparql_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'sparql/client'
require 'rialto/etl/readers/sparql_statement_reader'
require 'rialto/etl/namespaces'
require 'json'

RSpec.describe Rialto::Etl::Transformer do
let(:config_file_path) { 'lib/rialto/etl/configs/wos_to_sparql_statements.rb' }
Expand Down Expand Up @@ -782,4 +783,66 @@ def transform(source_file)
}
end
end
describe '#construct_contributor' do
subject(:contributor) { indexer.construct_contributor(json, []) }

before do
stub_request(:get, 'http://127.0.0.1:3001/person?first_name=JM&full_name=Yamartino,%20JM&last_name=Yamartino')
.to_return(status: 404, body: '', headers: {})
end

let(:indexer) do
Traject::Indexer.new.tap do |indexer|
indexer.load_config_file(config_file_path)
end
end

context 'with a plain-old name' do
let(:json) do
JSON.parse(
<<~JSON
{
"seq_no": 2503,
"role": "author",
"full_name": "Yamartino, JM",
"last_name": "Yamartino",
"display_name": "Yamartino, JM",
"wos_standard": "Yamartino, JM",
"daisng_id": 27906250,
"first_name": "JM"
}
JSON
)
end

it 'parses out the name correctly' do
expect(contributor[RDF::Vocab::VCARD.hasName.to_s][RDF::Vocab::VCARD['family-name'].to_s]).to eq('Yamartino')
expect(contributor[RDF::Vocab::VCARD.hasName.to_s][RDF::Vocab::VCARD['given-name'].to_s]).to eq('JM')
end
end

context 'with a name with a suffix' do
let(:json) do
JSON.parse(
<<~JSON
{
"seq_no": 2503,
"role": "author",
"full_name": "Yamartino, JM",
"last_name": "Yamartino",
"display_name": "Yamartino, JM",
"wos_standard": "Yamartino, JM",
"daisng_id": 27906250,
"suffix": "JM"
}
JSON
)
end

it 'parses out the name correctly' do
expect(contributor[RDF::Vocab::VCARD.hasName.to_s][RDF::Vocab::VCARD['family-name'].to_s]).to eq('Yamartino')
expect(contributor[RDF::Vocab::VCARD.hasName.to_s][RDF::Vocab::VCARD['given-name'].to_s]).to eq('JM')
end
end
end
end
17 changes: 15 additions & 2 deletions spec/transformers/people_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,30 @@
let(:given_name) { true }
let(:middle_name) { nil }

it 'returns the correct fullname' do
it 'returns empty array' do
expect(variations).to be_empty
expect(variations).to be_a(Array)
end
end

context 'when middle name is True' do
let(:middle_name) { true }
let(:given_name) { 'Justin' }

it 'returns the correct fullname' do
it 'returns non-empty array' do
expect(variations).not_to be_empty
expect(variations).to be_a(Array)
end
end

context 'when family name is True' do
let(:family_name) { true }
let(:given_name) { 'Justin' }
let(:middle_name) { nil }

it 'returns empty array' do
expect(variations).not_to be_empty
expect(variations).to be_a(Array)
end
end
end
Expand Down

0 comments on commit e6dcf13

Please sign in to comment.