Class: ScholarLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/scholar_lookup.rb

Overview

Queries the Semantic Scholar Graph API to cross-reference a rough title guess against real paper metadata. Always returns a Hash or nil; never raises, so a network hiccup just means the caller falls back to its own heuristics.

Constant Summary collapse

ENDPOINT =
'https://api.semanticscholar.org/graph/v1/paper/search'
FIELDS =
'title,authors,year,venue'
SOURCE_NAME =
'Semantic Scholar'
EXPECTED_ERRORS =

Exceptions expected from a flaky network/API, silently treated as "no match". Anything else (e.g. a response-shape change breaking #parse) is still caught so a rename can never crash, but is reported via warn so the regression isn't invisible.

[
  SocketError,
  Timeout::Error,
  SystemCallError,
  OpenSSL::SSL::SSLError,
  Net::ProtocolError,
  JSON::ParserError
].freeze
MIN_WORD_OVERLAP =

A match only counts if at least half of the query's words also appear in the returned title -- guards against a short/generic query (e.g. a running header) fuzzy-matching an unrelated paper.

0.5

Instance Method Summary collapse

Constructor Details

#initialize(open_timeout: 2, read_timeout: 3) ⇒ ScholarLookup

Returns a new instance of ScholarLookup.



35
36
37
38
# File 'lib/scholar_lookup.rb', line 35

def initialize(open_timeout: 2, read_timeout: 3)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details

#lookup(query) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/scholar_lookup.rb', line 44

def lookup(query)
  return nil if query.nil? || query.strip.length < 4

  body = fetch(build_uri(query.strip))
  return nil if body.nil?

  match = parse(body)
  return nil unless match && plausible_match?(query, match[:title])

  match
rescue *EXPECTED_ERRORS
  nil
rescue StandardError => e
  warn "scholar-rename: unexpected lookup error (#{e.class}): #{e.message}"
  nil
end

#source_nameObject



40
41
42
# File 'lib/scholar_lookup.rb', line 40

def source_name
  SOURCE_NAME
end