-
Notifications
You must be signed in to change notification settings - Fork 17
/
journal_abbrev.py
50 lines (38 loc) · 1.59 KB
/
journal_abbrev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import json
import re
import argparse
def abbreviate(line, journal_to_abbr):
if re.search('".*"', line) is not None:
journal_name_template = '"{}"'
journal_str = re.search('".*"', line).group(0)
elif re.search('{.*}', line) is not None:
journal_name_template = '{{{}}}'
journal_str = re.search('{.*}', line).group(0)
else:
raise ValueError('the format "{}" is not valid'.format(line))
journal_name_strip = journal_str[1:-1]
journal_name = journal_name_strip.replace('{','').replace('}','')
journal_name = journal_to_abbr.get(journal_name.lower(), journal_name_strip)
journal_name = journal_name_template.format(journal_name)
return line.replace(journal_str, journal_name)
def main(journal_to_abbr):
for line in sys.stdin:
line_strip = line.strip()
if line_strip.startswith('journal'):
new_line = abbreviate(line, journal_to_abbr)
print(new_line.rstrip())
else:
print(line.rstrip())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Journal abbreviation")
parser.add_argument('--user-json', type=str, default=None, help="customized json file")
args = parser.parse_args()
with open('journals.json') as fin:
journal_to_abbr = json.load(fin)
if args.user_json is not None:
with open(args.user_json) as fin:
customize_json = json.load(fin)
journal_to_abbr.update(customize_json)
journal_to_abbr = {k.lower(): v for k, v in journal_to_abbr.items()}
main(journal_to_abbr)