刘耀文

刘耀文

java开发者
github

pythonスクリプト、bibtexを一括取得

要求:LaTeX で論文を書く際に、引用を bibtex 形式に変換する必要があるかどうか、文献の量が多い場合はこの重複作業は本当に価値がないと感じるかもしれません。文献管理ツール(endnote、zotero など)を使用している場合は、一括エクスポートすることができますが、そうでない場合は、この記事で解決策を提供します。

解決策:crossref API+google scholar API

crossref は最大の外国語 DOI 公開プラットフォームであり、基本的にはすべての外国語文献のメタデータを含んでいますが、arXiv などの文献を含む一部の文献は検索できない場合があります。その場合は google scholar の助けが必要です。

皆さんの時間を節約するために、これらの 2 つの API をすでにパッケージ化していますので、pip を使用してダウンロードするだけで済みます。

pip install get_bibtex

その後、以下の使用方法に従ってください。

from apiModels.get_bibtex_from_crossref import GetBibTex
from apiModels.get_bibtex_from_google_scholar import GetBibTexFromGoogleScholar

if __name__ == '__main__':
    google_scholar_api_key = "your_google_scholar_api_key"
    get_bibtex_from_crossref = GetBibTex("[email protected]")
    get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(google_scholar_api_key, GetBibTexFromGoogleScholar.APA)

    with open("inputfile/Bibliographyraw.txt", "r", encoding='utf-8') as f:
        raws = f.readlines()
    
    # get bibtex from CrossRef and failed search results
    success_bibtexs_crossref, failed_results = get_bibtex_from_crossref.get_bibtexs(raws)
    
    # for each failed search result, get bibtex from Google Scholar
    success_bibtexs_google, failed_results = get_bibtex_from_google_scholar.get_bibtexs(failed_results)

    with open("outputfile/BibliographyCrossRef.txt", "w", encoding='utf-8') as f:
        for bibtex in success_bibtexs_crossref:
            f.write(bibtex)

    with open("outputfile/BibliographyGoogleScholar.txt", "w", encoding='utf-8') as f:
        for index, bibtex in enumerate(success_bibtexs_google):
            f.write("[]".format(index) + " " + bibtex + "\n")

    with open("outputfile/not_find.txt", "w", encoding='utf-8') as f:
        for result in failed_results:
            f.write(result+"\n")

    print("find bibtex from CrossRef: ", len(success_bibtexs_crossref))
    print("find bibtex from Google Scholar: ", len(success_bibtexs_google))
    print("not find: ", len(failed_results))

キーコードの説明

Bibliographyraw.txtには検索するファイルが含まれています
例:
J. Hu, L. Shen, S. Albanie, G. Sun, and A. Vedaldi, “Gather-Excite: Exploiting Feature Context in Convolutional Neural Networks.” arXiv, Jan. 12, 2019. doi: 10.48550/arXiv.1810.12348.
X. Wang, R. Girshick, A. Gupta, and K. He, “Non-local Neural Networks.” arXiv, Apr. 13, 2018. doi: 10.48550/arXiv.1711.07971.
------------------
success_bibtexs_crossref, failed_results = get_bibtex_from_crossref.get_bibtexs(raws)
返される最初のパラメータはbibtexのリストであり、2番目のパラメータは検索に失敗した元の文献です

success_bibtexs_google, failed_results = get_bibtex_from_google_scholar.get_bibtexs(failed_results)
検索に失敗した文献ごとに、Google Scholarからbibtexを取得します。通常、Google Scholarでは文献が見つからないことはありません。
ヒント:ここで返されるのは実際にはAPA形式ですが、初期化で指定されています。bibtex形式を返すように設定するためのパラメータもあります。例えば
get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(google_scholar_api_key, GetBibTexFromGoogleScholar.APA, flag = True)
ただし、プロキシサーバーを設定する必要があります。例:
import os
import re
import requests
os.environ["http_proxy"]="127.0.0.1:7890"
os.environ["https_proxy"]="127.0.0.1:7890"

!!!!!!!注意:API を申請する必要があります。毎月 100 回の無料クエリがありますが、通常は十分です。serpapi.com で申請してください。

その後のコードは非常に明確です。

もちろん、個別のクエリを要求することもできます。

get_bibtex()で個別のクエリを行うことができます

2024.4.16 更新#

1、DBLP インターフェースを追加しました

from apiModels.get_bibtex_from_dblp import GetBibTexFromDBLP

2、使用の利便性を向上させました

現在、クロスレフとDBLPのAPIを提供するために、パッケージ化されたクラスを提供しています。
from apiModels.workflow.crossref2dblp import Crossref2Dblp

使用方法(google scholar APIなし)
crossref2dblp = Crossref2Dblp("your email", "inputfile/Bibliographyraw.txt", "outputfile/Bibliography.txt")
crossref2dblp.running()
完了を待つだけです

(google scholar APIあり)
from apiModels.workflow.crossref2dblp import Crossref2Dblp
from apiModels.get_bibtex_from_google_scholar import GetBibTexFromGoogleScholar
get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(api_key="your api key")
パラメータの最後に封装したAPIを追加します
crossref2dblp = Crossref2Dblp("[email protected]", "inputfile/Bibliographyraw.txt", "outputfile/Bibliography.txt",get_bibtex_from_google_scholar)
crossref2dblp.running()
完了を待つだけです

または、API間の呼び出し順序を自分で定義することもできます
from apiModels.workflow.make_workflow import MakeWorkflow
from apiModels.get_bibtex_from_google_scholar import GetBibTexFromGoogleScholar
from apiModels.get_bibtex_from_crossref import GetBibTex

get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(api_key="your api key")
get_bibtex_from_crossref = GetBibTex("[email protected]")
make_workflow = MakeWorkflow("inputfile/Bibliographyraw.txt", "outputfile/Bibliography.txt", get_bibtex_from_google_scholar, get_bibtex_from_crossref)
make_workflow.running()

使用前に:
pip install get_bibtex = 1.1.0

改善案を歓迎します

この記事は Mix Space から xLog に同期されています。
元のリンクは https://me.liuyaowen.club/posts/default/20240816and2


読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。