-
Notifications
You must be signed in to change notification settings - Fork 3
/
ascl.py
executable file
·165 lines (150 loc) · 4.31 KB
/
ascl.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/env python
#
# quick helper code for ASCL editing (Teuben/Allen)
# is really part of the git module 'teuben/ascl-tools' but placed here
# for convenience
#
# @todo: terminal markers highjlight?
# use puncuation to break words? NO
# won't catch "MIRIAD's" - should we search for KEYWORD's as well
#
# Version: 25-jan-2014 for ADASS2013 proceedings
# 21-aug-2016 try to find alternate ascl2.txt from the pathname if a local version not present
from __future__ import print_function
import sys
debug = False
punct = ['.', ',', '/', ':', ';', '{', '}', '@', '(', ')', '[', ']', '\\', '\'', '"']
def printf(format, *args):
sys.stdout.write(format % args)
def parse1(file):
fp = open(file,'r')
lines = fp.readlines()
fp.close()
print("Found %d lines in %s" % (len(lines),file))
# look for
# <tr><td>ascl:1102.023</td><td>21cmFAST:
magic = '<tr><td>ascl:'
len1 = len(magic)
codes = {}
for line in lines:
if line[0:len1] == magic:
line1 = line[len1:80]
id = line1[0:8]
tmp = line1[17:80]
# print " ",line1[0:50]
ic = tmp.find(':')
code = tmp[0:ic]
code_words = code.split()
codes[code] = id
if len(code_words) > 1:
print('# ascl:'+id,code)
else:
print('ascl:'+id,code)
print('Found %d code entries' % len(codes))
return codes
def parse2(file):
""" read quick index for ADASS """
fp = open(file,'r')
lines = fp.readlines()
fp.close()
codes = {}
# file has "ascl:1112.017 ASpec" style lines
for line in lines:
if line[0] == '#': continue
words = line.split()
if len(words) != 2: continue
# print words[0], words[1]
codename = words[1].lower()
codes[codename] = "\\ooindex{%s, %s}" % (words[1],words[0])
return codes
def parse3(file,codes):
"""
codename -> codename\ooindex{codename, ascl_id}
"""
fp = open(file,'r')
lines = fp.readlines()
fp.close()
if debug: print(codes)
for line in lines:
print('LINE: ',line)
line2 = ''
has_a_code = False
words = line.split(' ,')
for word in words:
print('WORD: ',word)
if codes.has_key(word):
has_a_code = True
line2 = line2 + "%s\\ooindex{%s, %s} " % (word,word,codes[word])
else:
line2 = line2 + word + " "
if has_a_code:
print("# " + line)
print(line2)
else:
print(line)
def wclean(word):
"""make clean punctuation-free words
"""
w = word.lower()
len1 = len(punct)
# remove iteratively, all punctuation at the start
while True:
done = True
for p in punct:
if w[0] == p:
w = w[1:]
done = False
if len(w) == 0:
return w
break
if done:
break
len2 = len(w)
if len2 == 0:
return w
# remove iteratively, all punctuation from the end
while True:
done = True
for p in punct:
if w[-1:] == p:
w = w[:-1]
done = False
break
if done:
break
return w
def parse4(file,codes):
"""
codename -> codename\ooindex{codename, ascl_id}
"""
fp = open(file,'r')
lines = fp.readlines()
fp.close()
if debug: print(codes)
for line in lines:
if debug: print('LINE: '+line)
line2 = ''
has_a_code = False
words = line.split()
for dword in words:
word = wclean(dword)
if debug: print('WORD: ',dword,word)
if codes.has_key(word):
has_a_code = True
line2 = line2 + "%s \n" % (codes[word])
if has_a_code:
print("# " + line)
print(line2)
else:
if debug: print(line)
#parse3('sample.tex',codes)
if __name__ == '__main__':
print("CMD: ",sys.argv[0])
if len(sys.argv) > 1:
file = sys.argv[1]
#
if False:
codes = parse1('ascl.php')
else:
codes = parse2('ascl2.txt')
parse4(file,codes)