刘耀文

刘耀文

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()
    
    # CrossRefからbibtexを取得し、検索結果の失敗を処理
    success_bibtexs_crossref, failed_results = get_bibtex_from_crossref.get_bibtexs(raws)
    
    # 各失敗した検索結果について、Google Scholarからbibtexを取得
    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("CrossRefから見つかったbibtex: ", len(success_bibtexs_crossref))
    print("Google Scholarから見つかったbibtex: ", len(success_bibtexs_google))
    print("見つからなかった: ", 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 APIで検索します。一般的には見つかるはずですが、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()のsを外せば大丈夫です。

2024.4.16 更新#

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

from apiModels.get_bibtex_from_dblp import GetBibTexFromDBLP

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

現在、Crosrefと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


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