使用Python基于BibTeX引用格式自动生成文献的IEEE引用格式

本文介绍了一段Python代码,用于将BibTeX引用格式转换为IEEE标准的作者引用格式,特别关注了姓名排列和作者省略问题。通过调整代码,解决了姓名首字母大写的规范,并提供了一种处理多作者的简洁表示方法。
摘要由CSDN通过智能技术生成

说明

IEEE引用格式最近让我很头疼,因此为了快速解决论文格式转化为IEEE格式,我从网上搜索到相关资料,可供大家参考:
用Python代码自动生成文献的IEEE引用格式

上文大概描述了如何使用python代表通过BibTeX引用格式生成文献的IEEE引用格式,但是在使用时发现以下问题:

  1. 名字并未缩写
    例如下面文献:
@inproceedings{hu2018reinforcement,
title={Reinforcement learning to rank in e-commerce search engine: Formalization, analysis, and application},
author={Hu, Yujing and Da, Qing and Zeng, Anxiang and Yu, Yang and Xu, Yinghui},
booktitle={Proceedings of the 24th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining},
pages={368–377},
year={2018}
}

使用上面文章的代码生成的IEEE引用格式为:

Hu, Yujing and Da, Qing and Zeng, Anxiang and Yu, Yang and Xu, Yinghui, “Reinforcement learning to rank in e-commerce search engine: Formalization, analysis, and application,” , in Proceedings of the 24th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining, 2018, pp. 368-377.

然而在IEEE官网推荐的文章引用格式是名在前(且只保留大写字母),姓在后,如下所示:

B. H. Nguyen, B. Xue, P. Andreae and M. Zhang, "A Hybrid Evolutionary Computation Approach to Inducing Transfer Classifiers for Domain Adaptation," in IEEE Transactions on Cybernetics, vol. 51, no. 12, pp. 6319-6332, Dec. 2021.

因此在原代码中加入该操作。

  1. 在引用中发现作者太多,从有些IEEE文章中发现,可显示前两位作者,后面的省略。

效果如下:

B. Xue and M. Zhang, et al., "A Survey on Evolutionary Computation Approaches to Feature Selection," IEEE Transactions on Evolutionary Computation, vol. 20, no. 4, pp. 606-626, 2016.

完整代码如下:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2022/5/30 20:17
# @Author : doFighter
import re


def getIeeeJournalFormat(bibInfo):
    """
    生成期刊文献的IEEE引用格式:{作者}, "{文章标题}," {期刊名称}, vol. {卷数}, no. {编号}, pp. {页码}, {年份}.
    :return: {author}, "{title}," {journal}, vol. {volume}, no. {number}, pp. {pages}, {year}.
    """
    # 避免字典出现null值
    if "volume" not in bibInfo:
        bibInfo["volume"] = "null"
    if "number" not in bibInfo:
        bibInfo["number"] = "null"
    if "pages" not in bibInfo:
        bibInfo["pages"] = "null"

    journalFormat = bibInfo["author"] + \
                    ", \"" + bibInfo["title"] + \
                    ",\" " + bibInfo["journal"] + \
                    ", vol. " + bibInfo["volume"] + \
                    ", no. " + bibInfo["number"] + \
                    ", pp. " + bibInfo["pages"] + \
                    ", " + bibInfo["year"] + "."

    # 对格式进行调整,去掉没有的信息,调整页码格式
    journalFormatNormal = journalFormat.replace(", vol. null", "")
    journalFormatNormal = journalFormatNormal.replace(", no. null", "")
    journalFormatNormal = journalFormatNormal.replace(", pp. null", "")
    journalFormatNormal = journalFormatNormal.replace("--", "-")
    return journalFormatNormal


def getIeeeConferenceFormat(bibInfo):
    """
    生成会议文献的IEEE引用格式:{作者}, "{文章标题}, " in {会议名称}, {年份}, pp. {页码}.
    :return: {author}, "{title}, " in {booktitle}, {year}, pp. {pages}.
    """
    conferenceFormat = bibInfo["author"] + \
                       ",\"" + bibInfo["title"] + ",\" " + \
                       "in " + bibInfo["booktitle"] + \
                       ", " + bibInfo["year"] + \
                       ", pp. " + bibInfo["pages"] + "."

    # 对格式进行调整,,调整页码格式
    conferenceFormatNormal = conferenceFormat.replace("--", "-")
    return conferenceFormatNormal


def getIeeeFormat(bibInfo):
    """
    本函数用于根据文献类型调用相应函数来输出ieee文献引用格式
    :param bibInfo: 提取出的BibTeX引用信息
    :return: ieee引用格式
    """
    if "journal" in bibInfo:  # 期刊论文
        return getIeeeJournalFormat(bibInfo)
    elif "booktitle" in bibInfo:  # 会议论文
        return getIeeeConferenceFormat(bibInfo)

# 查找名,并按格式进行缩写
def capitalLetter(name):
    resName = ''
    for i in name:
        if i.isupper():
            resName += i + '. '

    return resName

# 按照bib格式,调整作者的姓名缩写形式
def nameModefy(name):
    nameList = name.split(' and ')
    resNames = []
    for index in range(len(nameList)):
        if index > 1:
            break
        names = nameList[index].split(',')
        if len(names) < 2:
            continue

        for i in range(len(names)):
            names[i] = names[i].strip()

        resName = capitalLetter(names[1]) + names[0]
        resNames.append(resName)

    result = ' and '.join(resNames)

    if len(nameList) > 2:
        result += ', et al.'

    return result



def inforDir(bibtex):
    # pattern = "[\w]+={[^{}]+}"   用正则表达式匹配符合 ...={...} 的字符串
    pattern1 = "[\w]+="  # 用正则表达式匹配符合 ...= 的字符串
    pattern2 = "{[^{}]+}"  # 用正则表达式匹配符合 内层{...} 的字符串

    # 找到所有的...=,并去除=号
    result1 = re.findall(pattern1, bibtex)
    for index in range(len(result1)):
        result1[index] = re.sub('=', '', result1[index])
    # 找到所有的{...},并去除{和}号
    result2 = re.findall(pattern2, bibtex)
    for index in range(len(result2)):
        result2[index] = re.sub('\{', '', result2[index])
        result2[index] = re.sub('\}', '', result2[index])

    # 创建BibTeX引用字典,归档所有有效信息
    infordir = {}
    for index in range(len(result1)):
        if result1[index] == 'author':
            infordir[result1[index]] = nameModefy(result2[index])
        else:
            infordir[result1[index]] = result2[index]
    return infordir


def inputBibTex():
    """
    在这里输入BibTeX格式的文献引用信息
    :return:提取出的BibTeX引用信息
    """
    bibtex = []
    
    print("请输入BibTeX格式的文献引用:")
    i = 0
    while i < 15:  # 观察可知BibTeX格式的文献引用不会多于15行
        lines = input()
        if len(lines) == 0:  # 如果输入空行,则说明引用内容已经输入完毕
            break
        else:
            bibtex.append(lines)
        i += 1
    return inforDir("".join(bibtex))


if __name__ == '__main__':
    bibInfo = inputBibTex()  # 获得BibTeX格式的文献引用
    print(getIeeeFormat(bibInfo))  # 输出ieee格式

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值