【发布时间】:2013-07-17 15:15:40
【问题描述】:
在我的论文中,我需要添加一个首字母缩略词列表。我想知道它是如何编程的。我找到了不错的实用程序pdfgrep,它也可以获取正则表达式。我是这样使用的:
pdfgrep "([A-Z]+)" thesis.pdf
这是我为此目的找到的最好的正则表达式,尽管它也有单个大写字母。有没有人有更好的解决方案? 我写了一个处理输出的 Python 代码:
import subprocess
import shlex
import re
FOLDER = 'full folder'
THESIS = '%s/thesis.pdf'%(FOLDER)
OUTPUT_FILE = '%s/acronymsInMyThesis.txt'%(FOLDER)
PATTERN = '([A-Z]+)'
def searchAcronymsInPDF():
output = pdfSearch()
acrs = []
for reg in re.findall(PATTERN, output):
reg.strip()
if (len(reg)>1):
acrs.append(reg)
return set(acrs)
def pdfSearch():
command = 'pdfgrep "%s" %s'%(PATTERN,THESIS)
output = shellCall(command)
return output
def shellCall(command):
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
out, _ = p.communicate()
return out
if __name__ == '__main__':
acrs = searchAcronymsInPDF()
print(acrs)
【问题讨论】:
-
[A-Z][A-Z]+?还是[A-Z]{2,}?你的缩写是什么?是S.H.I.E.L.D.?是ToC? -
这是个好问题。我决定这将是至少 2 个大写字母的序列。但是,正如你提到的,它不会赶上 ToC。