Class: BibCard::Crawler
- Inherits:
-
Object
- Object
- BibCard::Crawler
- Defined in:
- lib/bib_card/crawler.rb
Constant Summary collapse
- SPARQL_ENDPOINTS =
{ getty: "http://vocab.getty.edu/sparql?query=", wikidata: "http://query.wikidata.org/sparql?query=", dbpedia: "http://dbpedia.org/sparql?query=" }
Instance Method Summary collapse
- #alma_maters ⇒ Object
- #birth_date ⇒ Object
- #brief_bio ⇒ Object
- #creator_graph ⇒ Object
- #dbpedia_graph ⇒ Object
- #dbpedia_profile ⇒ Object
- #dbpedia_uri ⇒ Object
- #death_date ⇒ Object
- #film_appearances ⇒ Object
- #film_graph ⇒ Object
- #getty_note_graph ⇒ Object
- #getty_scope_notes ⇒ Object
- #getty_uri ⇒ Object
- #influence_graph ⇒ Object
- #influenced ⇒ Object
- #influences ⇒ Object
-
#initialize(uri, repository) ⇒ Crawler
constructor
A new instance of Crawler.
- #loc_uri ⇒ Object
- #notable_works ⇒ Object
- #profile_graph ⇒ Object
- #wikidata_graph ⇒ Object
- #wikidata_uri ⇒ Object
Constructor Details
#initialize(uri, repository) ⇒ Crawler
Returns a new instance of Crawler.
4 5 6 7 |
# File 'lib/bib_card/crawler.rb', line 4 def initialize(uri, repository) @subject = RDF::URI(uri) @repository = repository end |
Instance Method Details
#alma_maters ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/bib_card/crawler.rb', line 328 def alma_maters sparql = " #{self.wikidata_sparql_prefixes} SELECT DISTINCT ?inst ?instLabel ?statement ?reference ?source ?sourceLabel WHERE { <#{self.wikidata_uri.to_s}> p:P69 ?statement . ?statement ps:P69 ?inst . ?inst rdfs:label ?instLabel . FILTER(langMatches(lang(?instLabel), \"en\")) OPTIONAL { ?statement prov:wasDerivedFrom ?reference . ?reference pref:P248 ?source . ?source rdfs:label ?sourceLabel . FILTER(langMatches(lang(?sourceLabel), \"en\")) } } " get_data(sparql, :wikidata) end |
#birth_date ⇒ Object
15 16 17 18 |
# File 'lib/bib_card/crawler.rb', line 15 def birth_date stmt = @repository.query({subject: @subject, predicate: SCHEMA_BIRTHDATE}).first stmt.nil? ? nil : stmt.object end |
#brief_bio ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/bib_card/crawler.rb', line 350 def brief_bio sparql = " #{self.wikidata_sparql_prefixes} SELECT DISTINCT ?description ?workLocation ?workLocationLabel WHERE { <#{self.wikidata_uri.to_s}> schema:description ?description . OPTIONAL { <#{self.wikidata_uri.to_s}> wdt:P937 ?workLocation . } SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\" . } FILTER(langMatches(lang(?description), \"en\")) } " get_data(sparql, :wikidata).first end |
#creator_graph ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bib_card/crawler.rb', line 45 def creator_graph graph = RDF::Graph.new if @repository.size > 0 @repository.query({subject: @subject, predicate: RDF.type}).each {|stmt| graph << stmt} @repository.query({subject: @subject, predicate: SCHEMA_NAME}).each {|stmt| graph << stmt} graph << [@subject, SCHEMA_BIRTHDATE, self.birth_date] if self.birth_date graph << [@subject, SCHEMA_DEATHDATE, self.death_date] if self.death_date graph << [@subject, SCHEMA_SAME_AS, self.loc_uri] if self.loc_uri graph << [@subject, SCHEMA_SAME_AS, self.dbpedia_uri] if self.dbpedia_uri graph << [@subject, SCHEMA_SAME_AS, self.getty_uri] if self.getty_uri graph << [@subject, SCHEMA_SAME_AS, self.wikidata_uri] if self.wikidata_uri graph << dbpedia_graph if self.dbpedia_uri graph << getty_note_graph if self.getty_uri graph << wikidata_graph if self.wikidata_uri end graph end |
#dbpedia_graph ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bib_card/crawler.rb', line 63 def dbpedia_graph graph = RDF::Graph.new begin graph << profile_graph graph << influence_graph graph << film_graph rescue RestClient::RequestTimeout BibCard.logger.warn "DBPedia failed to respond. SPARQL query request timed out after 5 seconds for #{@current_query}." rescue Exception => e BibCard.logger.warn "DBPedia failed to respond. Processing data for SPARQL request: #{@current_query}. Error: #{e.}" end graph end |
#dbpedia_profile ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/bib_card/crawler.rb', line 189 def dbpedia_profile sparql = " #{self.dbpedia_sparql_prefixes} SELECT ?abstract ?foundedDate ?location ?thumbnail ?depiction WHERE { OPTIONAL { <#{self.dbpedia_uri}> dbo:abstract ?abstract . } OPTIONAL {<#{self.dbpedia_uri}> dbp:location ?location . } OPTIONAL { <#{self.dbpedia_uri}> dbp:foundedDate ?foundedDate . } OPTIONAL { <#{self.dbpedia_uri}> dbo:thumbnail ?thumbnail . } OPTIONAL { <#{self.dbpedia_uri}> foaf:depiction ?depiction . } FILTER(langMatches(lang(?abstract), \"en\")) } " get_data(sparql, :dbpedia).first end |
#dbpedia_uri ⇒ Object
30 31 32 33 |
# File 'lib/bib_card/crawler.rb', line 30 def dbpedia_uri stmt = @repository.query({subject: @subject, predicate: SCHEMA_SAME_AS}).select {|s| s.object.to_s.match('http://dbpedia.org/resource')}.first stmt.nil? ? nil : stmt.object end |
#death_date ⇒ Object
20 21 22 23 |
# File 'lib/bib_card/crawler.rb', line 20 def death_date stmt = @repository.query({subject: @subject, predicate: SCHEMA_DEATHDATE}).first stmt.nil? ? nil : stmt.object end |
#film_appearances ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/bib_card/crawler.rb', line 206 def film_appearances sparql = " #{self.dbpedia_sparql_prefixes} SELECT ?film ?filmName ?filmAbstract WHERE { ?film dbo:starring <#{self.dbpedia_uri}> . ?film rdfs:label ?filmName . ?film dbo:abstract ?filmAbstract . FILTER(langMatches(lang(?filmName), \"en\")) FILTER(langMatches(lang(?filmAbstract), \"en\")) } " get_data(sparql, :dbpedia) end |
#film_graph ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bib_card/crawler.rb', line 104 def film_graph @current_query = "film graph" graph = RDF::Graph.new self.film_appearances.each do |appearance| film = RDF::URI.new(appearance["film"]["value"]) graph << [film, DBO_STARRING, self.dbpedia_uri] graph << [film, RDF::RDFS.label, appearance["filmName"]["value"]] graph << [film, DBO_ABSTRACT, appearance["filmAbstract"]["value"]] end graph end |
#getty_note_graph ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/bib_card/crawler.rb', line 222 def getty_note_graph @current_query = "getty note graph" graph = RDF::Graph.new begin getty_subject = self.getty_uri self.getty_scope_notes.each do |scope_note| # Add the scope note itself scope_note_uri = RDF::URI.new(scope_note["scopeNote"]["value"]) graph << [getty_subject, SKOS_SCOPE_NOTE, scope_note_uri] graph << [scope_note_uri, RDF.value, scope_note["scopeNoteValue"]["value"]] # Add the sources/citations for the scope note source_uri = RDF::URI.new(scope_note["source"]["value"]) graph << [scope_note_uri, DC_SOURCE, source_uri] if scope_note["sourceShortTitle"] graph << [source_uri, BIBO_SHORT_TITLE, scope_note["sourceShortTitle"]["value"]] else parent_uri = RDF::URI.new(scope_note["parent"]["value"]) graph << [source_uri, DC_IS_PART_OF, parent_uri] graph << [source_uri, RDF.type, BIBO_DOCUMENT_PART] graph << [parent_uri, BIBO_SHORT_TITLE, scope_note["parentShortTitle"]["value"]] end end rescue RestClient::RequestTimeout BibCard.logger.warn "Getty failed to respond. SPARQL query request timed out after 5 seconds for #{@current_query}." rescue Exception => e BibCard.logger.warn "Getty failed to respond. Processing data for SPARQL request: #{@current_query}. Error: #{e.}" end graph end |
#getty_scope_notes ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/bib_card/crawler.rb', line 253 def getty_scope_notes sparql = " PREFIX ulan: <http://vocab.getty.edu/ulan/> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dct: <http://purl.org/dc/terms/> PREFIX bibo: <http://purl.org/ontology/bibo/> SELECT ?scopeNote ?scopeNoteValue ?source ?sourceShortTitle ?parent ?parentShortTitle WHERE { <#{self.getty_uri.to_s}> skos:scopeNote ?scopeNote . ?scopeNote rdf:value ?scopeNoteValue . ?scopeNote dct:source ?source . OPTIONAL { ?source bibo:shortTitle ?sourceShortTitle . } OPTIONAL { ?source dct:isPartOf ?parent . ?parent bibo:shortTitle ?parentShortTitle . } } " get_data(sparql, :getty) end |
#getty_uri ⇒ Object
35 36 37 38 |
# File 'lib/bib_card/crawler.rb', line 35 def getty_uri stmt = @repository.query({subject: @subject, predicate: SCHEMA_SAME_AS}).select {|s| s.object.to_s.match('vocab.getty.edu')}.first stmt.nil? ? nil : RDF::URI.new( stmt.object.to_s.gsub('-agent', '') ) end |
#influence_graph ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/bib_card/crawler.rb', line 77 def influence_graph graph = RDF::Graph.new [:influences, :influenced].each do |relationship| m = self.method(relationship) m.call.each do |influence| if relationship == :influences field = "influence" predicate = DBO_INFLUENCED_BY else field = "influenced" predicate = DBO_INFLUENCED end influence_entity = RDF::URI.new(influence[field]["value"]) graph << [self.dbpedia_uri, predicate, influence_entity] graph << [influence_entity, RDFS_LABEL, influence["#{field}Label"]["value"]] if influence["#{field}GivenName"] and influence["#{field}Surname"] graph << [influence_entity, FOAF_GIVEN_NAME, influence["#{field}GivenName"]["value"]] graph << [influence_entity, FOAF_SURNAME, influence["#{field}Surname"]["value"]] end if influence["influenceSameAs"] graph << [influence_entity, RDF::OWL.sameAs, influence["#{field}SameAs"]["value"]] end end end graph end |
#influenced ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bib_card/crawler.rb', line 160 def influenced @current_query = "influence upon graph" sparql = " #{self.dbpedia_sparql_prefixes} SELECT ?influenced ?influencedGivenName ?influencedSurname ?influencedSameAs ?influencedLabel WHERE { { <#{self.dbpedia_uri}> dbo:influenced ?influenced . } UNION { ?influenced dbo:influencedBy <#{self.dbpedia_uri}> . } ?influenced rdfs:label ?influencedLabel . OPTIONAL { ?influenced foaf:givenName ?influencedGivenName . ?influenced foaf:surname ?influencedSurname . } OPTIONAL { ?influenced owl:sameAs ?influencedSameAs . FILTER regex(STR(?influencedSameAs), \"viaf.org\"). } FILTER (lang(?influencedLabel) = 'en') } " get_data(sparql, :dbpedia) end |
#influences ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bib_card/crawler.rb', line 131 def influences @current_query = "influences graph" sparql = " #{self.dbpedia_sparql_prefixes} SELECT DISTINCT ?influence ?influenceGivenName ?influenceSurname ?influenceSameAs ?influenceLabel WHERE { { ?influence dbo:influenced <#{self.dbpedia_uri}> . } UNION { <#{self.dbpedia_uri}> dbo:influencedBy ?influence . } ?influence rdfs:label ?influenceLabel . OPTIONAL { ?influence foaf:givenName ?influenceGivenName . ?influence foaf:surname ?influenceSurname . } OPTIONAL { ?influence owl:sameAs ?influenceSameAs . FILTER regex(STR(?influenceSameAs), \"viaf.org\"). } FILTER (lang(?influenceLabel) = 'en') } " get_data(sparql, :dbpedia) end |
#loc_uri ⇒ Object
25 26 27 28 |
# File 'lib/bib_card/crawler.rb', line 25 def loc_uri stmt = @repository.query({subject: @subject, predicate: SCHEMA_SAME_AS}).select {|s| s.object.to_s.match('http://id.loc.gov/authorities/names/')}.first stmt.nil? ? nil : stmt.object end |
#notable_works ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/bib_card/crawler.rb', line 370 def notable_works sparql = " #{self.wikidata_sparql_prefixes} SELECT DISTINCT ?notableWork ?notableWorkLabel ?isbn ?oclcNumber WHERE { <#{self.wikidata_uri.to_s}> wdt:P800 ?notableWork . OPTIONAL { ?notableWork wdt:P212 ?isbn . ?notableWork wdt:P243 ?oclcNumber . } SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\" . } } " notable_works = get_data(sparql, :wikidata) notable_works.select {|work| work["notableWorkLabel"] != nil and !work["notableWorkLabel"]["value"].match(/^Q\d+$/)} end |
#profile_graph ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/bib_card/crawler.rb', line 116 def profile_graph @current_query = "profile graph" graph = RDF::Graph.new dbpedia_subject = self.dbpedia_uri profile = self.dbpedia_profile if profile graph << [dbpedia_subject, DBO_ABSTRACT, profile["abstract"]["value"]] if profile["abstract"] graph << [dbpedia_subject, DBP_FOUNDED, profile["foundedDate"]["value"]] if profile["foundedDate"] graph << [dbpedia_subject, DBP_LOCATION, profile["location"]["value"]] if profile["location"] graph << [dbpedia_subject, DBO_THUMBNAIL, profile["thumbnail"]["value"]] if profile["thumbnail"] graph << [dbpedia_subject, FOAF_DEPICTION, profile["depiction"]["value"]] if profile["depiction"] end graph end |
#wikidata_graph ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/bib_card/crawler.rb', line 276 def wikidata_graph graph = RDF::Graph.new begin wikidata_subject = self.wikidata_uri self.alma_maters.each do |alma_mater| @current_query = "alma maters graph" am_inst_uri = RDF::URI.new(alma_mater["inst"]["value"]) am_edu_stmt = RDF::URI.new(alma_mater["statement"]["value"]) graph << [wikidata_subject, WDT_EDUCATED_AT, am_inst_uri] graph << [am_inst_uri, RDF::RDFS.label, alma_mater["instLabel"]["value"]] graph << [wikidata_subject, WDP_EDUCATED_AT, am_edu_stmt] graph << [am_edu_stmt, WDPS_STMT_EDU_AT, am_inst_uri] # Not all assertions have references/citations if alma_mater["reference"] am_stmt_ref = RDF::URI.new(alma_mater["reference"]["value"]) am_ref_source = RDF::URI.new(alma_mater["source"]["value"]) graph << [am_edu_stmt, PROV_DERIVED_FROM, am_stmt_ref] graph << [am_stmt_ref, WDR_STATED_IN, am_ref_source] graph << [am_ref_source, RDF::RDFS.label, alma_mater["sourceLabel"]["value"]] end end bio = self.brief_bio if bio @current_query = "brief bio graph" graph << [wikidata_subject, SCHEMA_DESCRIPTION, bio["description"]["value"]] if bio["description"] if bio["workLocation"] work_loc_uri = RDF::URI.new(bio["workLocation"]["value"]) graph << [wikidata_subject, WDT_WORK_LOCATION, work_loc_uri] graph << [work_loc_uri, RDF::RDFS.label, bio["workLocationLabel"]["value"]] end end self.notable_works.each do |work| @current_query = "notable works graph" work_uri = RDF::URI.new(work["notableWork"]["value"]) graph << [wikidata_subject, WDT_NOTABLE_WORKS, work_uri] graph << [work_uri, RDF::RDFS.label, work["notableWorkLabel"]["value"]] graph << [work_uri, WDT_ISBN, work["isbn"]["value"]] if work["isbn"] graph << [work_uri, WDT_OCLC_NUMBER, work["oclcNumber"]["value"]] if work["oclcNumber"] end rescue RestClient::RequestTimeout BibCard.logger.warn "WikiData failed to respond. SPARQL query request timed out after 5 seconds for #{@current_query}." rescue Exception => e BibCard.logger.warn "WikiData failed to respond. Processing data for SPARQL request: #{@current_query}. Error: #{e.}" end graph end |
#wikidata_uri ⇒ Object
40 41 42 43 |
# File 'lib/bib_card/crawler.rb', line 40 def wikidata_uri stmt = @repository.query({subject: @subject, predicate: SCHEMA_SAME_AS}).select {|s| s.object.to_s.match('http://www.wikidata.org/entity')}.first stmt.nil? ? nil : stmt.object end |