Module: Extcite
- Defined in:
- lib/extcite.rb,
lib/extcite/version.rb
Constant Summary collapse
- VERSION =
"0.4.0"
Class Method Summary collapse
-
.cont_neg(ids:) ⇒ Object
Get citation(s) using Crossref content negotation.
-
.extract(path:, file: "out.bib", output: "bib") ⇒ Object
Extract DOIs from one or more PDFs.
-
.extract_dois(path:) ⇒ Object
Extract DOIs from one or more PDFs after extracting text.
-
.extract_from_metadata(path:) ⇒ Object
Try to extract DOIs from one or more PDF metadata sections.
-
.extract_text(path:) ⇒ Object
Extract text from a pdf, or many pdfs.
-
.get_ids(txt:) ⇒ Object
Get DOIs from a String or Array of String’s.
Class Method Details
.cont_neg(ids:) ⇒ Object
Get citation(s) using Crossref content negotation
Return: an string of bib data
296 297 298 299 |
# File 'lib/extcite.rb', line 296 def self.cont_neg(ids:) out = Serrano.content_negotiation(ids: ids) return out end |
.extract(path:, file: "out.bib", output: "bib") ⇒ Object
Extract DOIs from one or more PDFs
Return: writes bib files to a .bib file or an array if file is nil
When writing to a file, `extract` by default appends to the end
of the file so you can build up your bibtex file with your
citations
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/extcite.rb', line 38 def self.extract(path:, file: "out.bib", output: "bib") path = make_paths(path) path.each do |x| # try PDF metadata first ids = nil rr = PDF::Reader.new(x); = rr. if !.nil? xml = Oga.parse_xml(); begin tt = xml.xpath('//rdf:Description') # try dc:identifier attribute ss = tt.attr('dc:identifier')[0] if !ss.nil? ids = ss.text.sub(/doi:/, '') else # try prism:doi node pdoi = xml.xpath('//rdf:Description//prism:doi') if pdoi.length == 1 ids = pdoi.text else # try pdf:WPS-ARTICLEDOI node wpsdoi = xml.xpath('//rdf:Description//pdf:WPS-ARTICLEDOI') if wpsdoi.length == 1 ids = wpsdoi.text else # try pdfx:WPS-ARTICLEDOI node pdfxwpsdoi = xml.xpath('//rdf:Description//pdfx:WPS-ARTICLEDOI') if pdfxwpsdoi.length == 1 ids = pdfxwpsdoi.text else ids = nil end end end end rescue ids = nil end end # if not found, try regexing for DOI if ids.nil? ids = Extcite.get_ids(txt: Extcite.extract_text_one(x)) end if ids.length == 0 puts "no DOI found in " + x else if !ids.match(/arxiv/i).nil? && ids.length < 200 conn = Faraday.new(:url => 'http://export.arxiv.org/api/query?id_list=' + ids.gsub(/arxiv:/i, '')).get bibs = conn.body.make_bib_arxiv(ids.gsub(/arxiv:/i, '')) else bibs = Extcite.cont_neg(ids: ids) end # if an error or not found, skip bibstest = nil if bibs.class == Array bibstest = bibs[0] else bibstest = bibs end if !bibstest.nil? if !bibstest.match(/error|not found/i).nil? || !bibstest.match(/<\/html>/i).nil? puts "DOI found: " + ids + " ; but citation not found via content negotation - passing" # do something else? else if file.nil? return bibstest else puts "writing " + ids + " to " + file bibs.write_bib(file) end end end end end end |
.extract_dois(path:) ⇒ Object
Extract DOIs from one or more PDFs after extracting text
210 211 212 213 214 |
# File 'lib/extcite.rb', line 210 def self.extract_dois(path:) txt = Extcite.extract_text(path: path) # return txt.map { |z| z.match("[0-9]+\\.[0-9]+/.+").to_s.gsub(/\s.+/, '') } return Extcite.get_ids(txt: txt) end |
.extract_from_metadata(path:) ⇒ Object
Try to extract DOIs from one or more PDF metadata sections
Return: DOI string
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 188 189 190 191 192 |
# File 'lib/extcite.rb', line 137 def self.(path:) path = make_paths(path) path.each do |x| # try PDF metadata first ids = nil rr = PDF::Reader.new(x) = rr. if !.nil? begin xml = Oga.parse_xml(); rescue Exception => e xml = nil end if !xml.nil? begin tt = xml.xpath('//rdf:Description') # try dc:identifier attribute ss = tt.attr('dc:identifier')[0] if !ss.nil? ids = ss.text.sub(/doi:/, '') else # try prism:doi node pdoi = xml.xpath('//rdf:Description//prism:doi') if pdoi.length == 1 ids = pdoi.text else # try pdf:WPS-ARTICLEDOI node wpsdoi = xml.xpath('//rdf:Description//pdf:WPS-ARTICLEDOI') if wpsdoi.length == 1 ids = wpsdoi.text else # try pdfx:WPS-ARTICLEDOI node pdfxwpsdoi = xml.xpath('//rdf:Description//pdfx:WPS-ARTICLEDOI') if pdfxwpsdoi.length == 1 ids = pdfxwpsdoi.text else ids = nil end end end end rescue ids = nil end end end # if not found, try regexing for DOI if ids.nil? ids = Extcite.get_ids(txt: Extcite.extract_text_one(x)) end return ids end end |
.extract_text(path:) ⇒ Object
Extract text from a pdf, or many pdfs
This method is used internally within fetch to parse PDFs.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/extcite.rb', line 263 def self.extract_text(path:) path = Array(path) if path.length == 1 if File.directory?(path[0]) # keep only files with .pdf extension path = dir_files(path[0]).keep_if { |z| !!z.match(/.pdf/) } end end out = [] path.each do |x| begin rr = PDF::Reader.new(x) txt = rr.pages.map { |page| page.text }.join("\n") rescue Exception => e warn e txt = "" end out << txt end return out end |
.get_ids(txt:) ⇒ Object
Get DOIs from a String or Array of String’s
Return: Array of DOIs
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/extcite.rb', line 226 def self.get_ids(txt:) # see if there's return Array(txt).map { |z| # detect if is an arxiv paper if !z.match(/arxiv:[0-9]+\.[0-9A-Za-z]+/i).nil? # if so, return arxiv id for later extraction of arxiv citation via their API z = z.match(/arxiv:[0-9]+\.[0-9A-Za-z]+/i).to_s else doi_pattern = '(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?![%"#? ])\\S)+)' z = z.match(doi_pattern).to_s.gsub(/\s.+/, '') # z = z.match("10\\.[0-9]+/.+").to_s.gsub(/\s.+/, '') end # clean up doi z = z.gsub(/\.$|\.;$|\.\]$|\.\}$|\.\)$|,$/, '') return z.gsub(/;$|\]$|\}$|\)$/, '') }[0] end |