1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

arXivのアラートから前日に新規投稿された論文だけを抽出して確認できるようにする

Posted at

事の起こり

筆者は最新の論文を追える様にarXivのアラームメール機能を利用しています。毎日前日までに投稿された最新の論文のうち、事前に設定した購読したい分野の論文の一覧(タイトル+Abstract)をメールで通知してくれます。ところが、このアラートは前日までの論文一覧が届くので、分量が多く確認が大変です。よって、前日投稿文だけを抽出して、論文を探しやすくするスクリプトを作ることにしました。

実際に実装したスクリプト

Pythonを用いて実装しました。今回はPythonの標準ライブラリだけで実装しました。週明けや連休明けに論文を読む場合、前の週の金曜日または休みに入る前の最後の平日に投稿された論文を読みたいので、day_diffで何日前の投稿かを選択出来る様にしました。今の所は平日のみ論文を探していますが、そのうち休日も論文探しをやるかもしれないので、そうなったらこのスクリプトの出番は増えると思います。

import re

from datetime import datetime, timedelta
from pathlib import Path


def filter_papers(list_file_path, day_diff):

    today = datetime.today()
    date = today.strftime('%Y%m%d')
    if list_file_path is not None:
        # 指定日の論文リストを開く
        list_file_path = Path(list_file_path)
    else:
        # 今日の論文リストを開く
        list_file_path = Path(f'paper_list/{date}.txt')

    if not list_file_path.exists():
        raise FileNotFoundError('Wrong list file path!!')
    if day_diff < 0:
        raise ValueError('Wrong diff value')

    months = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun',
              7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
    weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

    # 検索対象の投稿日を算出
    target_date = today - timedelta(day_diff)
    weekday = today.weekday() - day_diff
    target_paper_date = f'{weekdays[weekday]}, {target_date.day} {months[target_date.month]} {target_date.year}'

    paper_list_data = ''

    with open(list_file_path, 'r') as f:
        paper_list_data = f.readlines()

    paper_list = []
    paper = ''
    for i, content in enumerate(paper_list_data):
        if re.compile(r'---*').search(content):
            if i == 0:
                # アラートメールのフォーマットの都合で1行目は取り込まない
                continue
            else:
                # 文字列=論文の本文をリストに格納
                paper_list.append(paper)
                paper = ''
        else:
            # 論文の本文を文字列として成形
            paper += content

    target_papers = []

    count = 1
    for paper in paper_list:
        if re.compile(f'Date: {target_paper_date}').search(paper):
            paper = f'No. {count}\n{paper}'
            target_papers.append(paper)
            count += 1

    print(f'Target date: {target_paper_date}, {len(target_papers)} papers')

    with open(f'paper_list/{date}_filtering_{target_date.strftime("%Y%m%d")}.txt', 'w') as f:
        f.writelines(target_papers)


if __name__ == '__main__':
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

    arg_parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
    arg_parser.add_argument('--list_path', type=str, default=None, help='File path of paper list')
    arg_parser.add_argument('--day_diff', type=int, default=1, help='Number of days before today')
    args = arg_parser.parse_args()

    filter_papers(args.list_path, args.day_diff)

まとめ

既にこのスクリプトを利用して論文を探しています。今後はキーワード検索を行った結果を出力する機能なども追加して、より便利なスクリプトに改良していこうと思います。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?