From eee74c781871fd00e7ff577b464ea3fc86f75592 Mon Sep 17 00:00:00 2001 From: HOC3426 Date: Fri, 27 Sep 2024 14:16:05 -0500 Subject: [PATCH 1/2] Create script.py This is a script to add citations to books from info in raw_ref. --- scripts/add_book_citations/script.py | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 scripts/add_book_citations/script.py diff --git a/scripts/add_book_citations/script.py b/scripts/add_book_citations/script.py new file mode 100644 index 0000000..2422863 --- /dev/null +++ b/scripts/add_book_citations/script.py @@ -0,0 +1,58 @@ +from itertools import chain +from inspirehep.curation.search_check_do import SearchCheckDo + +BOOKS = [ + {'recid': '2735748', + 'book_info': ['Rezzolla', 'Zanotti', 'Relativistic Hydrodynamics', + 'Oxford University Press', '2013']}, + {'recid': '181166', + 'book_info': ['Birrell', 'Davies', 'Quantum Fields in Curved Space', + 'Cambridge University Press', '1982']}, + {'recid': '159194', + 'book_info': ['Itzykson', 'Zuber', 'Quantum Field Theory', + 'McGraw', '1980']}, + {'recid': '640063', + 'book_info': ['Dodelson', 'Modern Cosmology', + 'Academic Press', '2003']}, + {'recid': '1120339', + 'book_info': ['Baxter', 'Exactly solved models in statistical mechanics', + 'Academic Press', '1982']}, +] + + +class AddBookCitations(queryCheckDo): + '''Add recid to reference for the citation of a book.''' + + query = '' + book = BOOKS[0] + book_info = book['book_info'] + recid = book['recid'] + url = f'https://inspirehep.net/api/literature/{recid}' + for element in book_info: + query += f' ft:{element}' + query = f'{query} -refersto:recid:{recid}' + + @staticmethod + def check(record, logger, state): + global url + cited_records = chain.from_iterable( + record.get_value('references.record.ref', [])) + return url in cited_records + + @staticmethod + def do(record, logger, state): + global book_info + try: + references = record['references'] + except KeyError: + return None + for reference in record['references']: + try: + raw_ref = reference.get('raw_refs', {})[0]['value'].lower() + except KeyError: + continue + if all(book_element.lower() in raw_ref for + book_element in book_info): + references['record']['$ref'] = url + +AddBookCitations() From 2fe89f1a7c44714dcc1d3834382706c387a6b8ee Mon Sep 17 00:00:00 2001 From: hoc3426 Date: Fri, 27 Sep 2024 19:17:22 +0000 Subject: [PATCH 2/2] Auto-format python code --- scripts/add_book_citations/script.py | 81 ++++++++++++++++++---------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/scripts/add_book_citations/script.py b/scripts/add_book_citations/script.py index 2422863..6cb607f 100644 --- a/scripts/add_book_citations/script.py +++ b/scripts/add_book_citations/script.py @@ -2,57 +2,80 @@ from inspirehep.curation.search_check_do import SearchCheckDo BOOKS = [ - {'recid': '2735748', - 'book_info': ['Rezzolla', 'Zanotti', 'Relativistic Hydrodynamics', - 'Oxford University Press', '2013']}, - {'recid': '181166', - 'book_info': ['Birrell', 'Davies', 'Quantum Fields in Curved Space', - 'Cambridge University Press', '1982']}, - {'recid': '159194', - 'book_info': ['Itzykson', 'Zuber', 'Quantum Field Theory', - 'McGraw', '1980']}, - {'recid': '640063', - 'book_info': ['Dodelson', 'Modern Cosmology', - 'Academic Press', '2003']}, - {'recid': '1120339', - 'book_info': ['Baxter', 'Exactly solved models in statistical mechanics', - 'Academic Press', '1982']}, + { + "recid": "2735748", + "book_info": [ + "Rezzolla", + "Zanotti", + "Relativistic Hydrodynamics", + "Oxford University Press", + "2013", + ], + }, + { + "recid": "181166", + "book_info": [ + "Birrell", + "Davies", + "Quantum Fields in Curved Space", + "Cambridge University Press", + "1982", + ], + }, + { + "recid": "159194", + "book_info": ["Itzykson", "Zuber", "Quantum Field Theory", "McGraw", "1980"], + }, + { + "recid": "640063", + "book_info": ["Dodelson", "Modern Cosmology", "Academic Press", "2003"], + }, + { + "recid": "1120339", + "book_info": [ + "Baxter", + "Exactly solved models in statistical mechanics", + "Academic Press", + "1982", + ], + }, ] class AddBookCitations(queryCheckDo): - '''Add recid to reference for the citation of a book.''' + """Add recid to reference for the citation of a book.""" - query = '' + query = "" book = BOOKS[0] - book_info = book['book_info'] - recid = book['recid'] - url = f'https://inspirehep.net/api/literature/{recid}' + book_info = book["book_info"] + recid = book["recid"] + url = f"https://inspirehep.net/api/literature/{recid}" for element in book_info: - query += f' ft:{element}' - query = f'{query} -refersto:recid:{recid}' + query += f" ft:{element}" + query = f"{query} -refersto:recid:{recid}" @staticmethod def check(record, logger, state): global url cited_records = chain.from_iterable( - record.get_value('references.record.ref', [])) + record.get_value("references.record.ref", []) + ) return url in cited_records @staticmethod def do(record, logger, state): global book_info try: - references = record['references'] + references = record["references"] except KeyError: return None - for reference in record['references']: + for reference in record["references"]: try: - raw_ref = reference.get('raw_refs', {})[0]['value'].lower() + raw_ref = reference.get("raw_refs", {})[0]["value"].lower() except KeyError: continue - if all(book_element.lower() in raw_ref for - book_element in book_info): - references['record']['$ref'] = url + if all(book_element.lower() in raw_ref for book_element in book_info): + references["record"]["$ref"] = url + AddBookCitations()