├── README.md ├── abbreviate.py ├── abbreviate_comment-in-english.py ├── extend.py ├── journal_list.txt └── update_bib_from_mendeley.sh /README.md: -------------------------------------------------------------------------------- 1 | # abbreviate_journal_names_in_bib 2 | ### (In English) 3 | One-sentence description: Quickly generate a bib file containing the items of all the references you want, and replace the full journal names in the file with official abbreviated names, or do the opposite. 4 | 5 | Latex provides a wonderful method to include references in your paper. To do this, you need to save the information (title, journal, authours, year, etc) of your references into a `.bib` file. Each reference corresponds to an bib item, which can be copied from Google scholar. 6 | 7 | However, if you have many bib items, it would be tedious to copy them all by hands. A better way is to pull the pdf files of these papers into Mendeley (a reference management software), and then output the bib items of all these papers using the output function of Mendeley. 8 | 9 | But Mendeley often outputs some unnecessary information of papers, such as abstract,DOI,keywords. I write a bash file update_bib_from_mendeley.sh to remove these content. Additionally, Mendeley sometimes would mistake the type of some papers into 'generic' or 'report', which actually should be 'article'. I also add some commands into my bash file to remove these errors. 10 | 11 | Usage of update_bib_from_mendeley.sh: In windows, put this file and your library.bib file under the same directory, and then double click the bash file; In Linux, `. update_bib_from_mendeley.sh` or `source update_bib_from_mendeley.sh`. 12 | 13 | In this way you can quickly get a library.bib file, but the journals in this file are in their full names, while many publishers require them to be abbriviated. I write a python code to replace the full names in a bib file into their official abbreviated names. This code needs a reference file journal_list.txt which keeps most journal's full names and the corresponding official abbreviated names. 14 | 15 | Usage of abbriviate.py: `python abbriviate.py library.bib` 16 | 17 | I also write a python code extend.py to do the reverse thing, i.e., extending the abbreviated names into their full form. As a result, each time you only need to update one of the bib files. 18 | 19 | ### (In Chinese) 20 | 一句话描述:快速生成一个bib文件,里面包含你想要的所有参考文献的条目,把文件中的期刊全名替换成官方缩写名,或者反之。更多信息见知乎文章:https://zhuanlan.zhihu.com/p/503375837。 21 | 22 | Latex提供了很方便的文献引用。你需要把你想引用的文献的信息(题目,期刊,作者,年份,等等)存入一个bib文件。每篇文献对应一个bib item,这些item都可以从谷歌学术上下载到。 23 | 24 | 但如果需要加入大量的items,一个一个下载太tedious了,更好的办法是把这些文章的pdf全部拖入Mendeley(或者手动载入信息),然后用Mendeley一次性全部导出bib形式的items。 25 | 26 | 但是这些items包含了很多不必要的信息,比如abstract,DOI,keywords,所以我写了一个脚本update_bib_from_mendeley.sh来删除这些内容。此外,Mendeley有时候会把文章类型错弄成generic或report,我也用上面的脚本把它们更改为article。脚本使用方法:windows下把它和其他文件放到同一目录下,然后双击;Linux下,直接`. update_bib_from_mendeley.sh` 或者 `source update_bib_from_mendeley.sh`。 27 | 28 | 这样得到的library.bib包含了完整的期刊名。很多杂志要求用期刊名的缩写。同样,一个一个改太慢了,我写了abbriviate.py,又下载了最全的期刊名全写与缩写的对照表,存于journal_list.txt, 29 | 使用方法:`python abbriviate.py library.bib` 30 | 就能得到期刊名缩写的library_abbreviated.bib。 31 | 32 | 我又写了extend.py来实现逆转换。后面只需要更新一个,另一个用代码转换即可。 33 | -------------------------------------------------------------------------------- /abbreviate.py: -------------------------------------------------------------------------------- 1 | # 本代码将bib file中的academic journals全名改成标准缩写名,并保存为一个新文件。 2 | # 配套的journal_list.txt包含了几乎所有journals的全名-缩写名;如果没有,可以在http://www.ncbi.nlm.nih.gov/nlmcatalog/journals查询后手动添加。 3 | 4 | import os 5 | import re 6 | import sys 7 | 8 | try: 9 | # 将bib file的内容存入一个超长string 10 | bibname = sys.argv[1] 11 | bibcontent = ''.join(open(bibname, 'r', errors='ignore').readlines()) 12 | except IndexError: 13 | print("Error: Specify the file to be processed!") 14 | exit(1) 15 | except FileNotFoundError: 16 | print("Error: Provided filename could not be loaded!") 17 | exit(1) 18 | 19 | # 逐个读取journal_list.txt中的全名-缩写名,并用缩写名替换超长string中的全名 20 | fr = open('journal_list.txt', 'r', errors='ignore') 21 | line = fr.readline() 22 | 23 | while(line): 24 | # 全名在前,缩写名在后 25 | full, short = line[:-1].split(" = ")[:2] 26 | # 加{}来变成bib里的期刊名部分,替换以{full}-{short}形式进行,避免替换半个期刊名 27 | full, short = '{%s}'%full, '{%s}'%short 28 | 29 | # 全为大写或只含一个单词的期刊全名不需要被替换 30 | if full != full.upper() and (' ' in full): 31 | # 生成一个正则表达式对象,注意用escape取消{和}的特殊含义,并且忽略大小写 32 | pattern = re.compile(re.escape(full), re.IGNORECASE) 33 | # 用正则表达式对象的subn method实现替换和统计 34 | (bibcontent, num_repl) = pattern.subn(short, bibcontent) 35 | 36 | # 输出替换某个期刊名的次数 37 | if num_repl > 0: 38 | print("%i replacements of %s by %s" % (num_repl, full, short)) 39 | 40 | # 读取下一个期刊 41 | line = fr.readline() 42 | 43 | # 将替换后的超长string写入新文件 44 | with open('library_abbreviated.bib', 'w') as outfile: 45 | outfile.write(bibcontent) 46 | print("Bibtex database with abbreviated names saved into 'library_abbreviated.bib'") -------------------------------------------------------------------------------- /abbreviate_comment-in-english.py: -------------------------------------------------------------------------------- 1 | # This python code is to replace full journal names in a bibtex database file (.bib) into abbreviated names. 2 | # journal_list.txt contains abbreviated names of nearly all journals. 3 | # If one journal is not included, its abbreviated name can be found at http://www.ncbi.nlm.nih.gov/nlmcatalog/journals. 4 | 5 | import os 6 | import re 7 | import sys 8 | 9 | try: 10 | # input the whole content of a bib file as a very long string 11 | bibtexdb = ''.join(open(sys.argv[1], 'r', errors='ignore').readlines()) 12 | # print (bibtexdb) 13 | except IndexError: 14 | print("Error: Specify the file to be processed!") 15 | exit(1) 16 | except FileNotFoundError: 17 | print("Error: Provided filename could not be loaded!") 18 | exit(1) 19 | 20 | fr = open('journal_list.txt', 'r', errors='ignore') 21 | line = fr.readline() 22 | 23 | # go over journal names in journal_list.txt one by one, and replace the full name in bibtexdb with its abbreviated counterpart. 24 | while(line): 25 | patterns = line[:-1].split(" = ")[:2] 26 | pattern1, pattern2 = map(lambda pattern: "{%s}" % pattern, patterns) 27 | 28 | # avoid mere abbreviations 29 | if pattern1 != pattern1.upper() and (' ' in pattern1): 30 | repl = re.compile(re.escape(pattern1), re.IGNORECASE) 31 | (bibtexdb, num_subs) = repl.subn(pattern2, bibtexdb) 32 | 33 | # print the number of replacements 34 | if num_subs > 0: 35 | print("Replaced (%ix) '%s' FOR '%s'" % (num_subs, *patterns)) 36 | 37 | # go to the next journal name 38 | line = fr.readline() 39 | 40 | # write the updated content into a new file 41 | with open('library_abbreviated.bib', 'w') as outfile: 42 | outfile.write(bibtexdb) 43 | print("Bibtex database with abbreviated files saved into 'library_abbreviated.bib'") -------------------------------------------------------------------------------- /extend.py: -------------------------------------------------------------------------------- 1 | # 本代码将bib file中的academic journals缩写名改成全名,并保存为一个新文件。 2 | # 配套的journal_list.txt包含了几乎所有journals的全名-缩写名;如果没有,可以在http://www.ncbi.nlm.nih.gov/nlmcatalog/journals查询后手动添加。 3 | 4 | import os 5 | import re 6 | import sys 7 | 8 | # 1. 将bib file的内容存入一个超长string 9 | try: 10 | bibname = sys.argv[1] 11 | bibcontent = ''.join(open(bibname, 'r', errors='ignore').readlines()) 12 | except IndexError: 13 | print("Error: Specify the file to be processed!") 14 | exit(1) 15 | except FileNotFoundError: 16 | print("Error: Provided filename could not be loaded!") 17 | exit(1) 18 | 19 | # 2. 逐个读取journal_list.txt中的全名-缩写名,并用全名替换超长string中的缩写名 20 | fr = open('journal_list.txt', 'r', errors='ignore') 21 | line = fr.readline() 22 | 23 | while(line): 24 | # 全名在前,缩写名在后 25 | full, short = line[:-1].split(" = ")[:2] 26 | # 加{}来变成bib里的期刊名部分,替换以{full}-{short}形式进行,避免替换半个期刊名 27 | full, short = '{%s}'%full, '{%s}'%short 28 | 29 | # 全为大写或只含一个单词的期刊缩写名不需要被替换 30 | if short != short.upper() and (' ' in short): 31 | # 生成一个正则表达式对象,注意用escape取消{和}的特殊含义,并且忽略大小写 32 | pattern = re.compile(re.escape(short), re.IGNORECASE) 33 | 34 | # 用正则表达式对象的subn method实现替换和统计 35 | (bibcontent, num_repl) = pattern.subn(full, bibcontent) 36 | 37 | # 输出替换某个期刊名的次数 38 | if num_repl > 0: 39 | print("%i replacements of %s by %s" % (num_repl, short, full)) 40 | 41 | # 读取下一个期刊 42 | line = fr.readline() 43 | 44 | # 3. 将替换后的超长string写入新文件 45 | with open('library_full.bib', 'w') as outfile: 46 | outfile.write(bibcontent) 47 | print("Bibtex database with full names saved into 'library_full.bib'") -------------------------------------------------------------------------------- /update_bib_from_mendeley.sh: -------------------------------------------------------------------------------- 1 | # library.bib is exported from the Mendeley software 2 | 3 | # delete abstract, doi, keywords 4 | sed -i '/abstract = {/d' library.bib 5 | sed -i '/doi = {/d' library.bib 6 | sed -i '/keywords = {/d' library.bib 7 | 8 | # change generic, report, to article 9 | sed -i 's/@generic/@article/g' library.bib 10 | sed -i 's/@report/@article/g' library.bib 11 | --------------------------------------------------------------------------------