【发布时间】:2020-08-28 11:24:54
【问题描述】:
我正在使用 this code 从文本中提取引文:
#!/usr/bin/env python3
# https://stackoverflow.com/a/16826935
import re
from sys import stdin
text = stdin.read()
author = "(?:[A-Z][A-Za-z'`-]+)"
etal = "(?:et al.?)"
additional = "(?:,? (?:(?:and |& )?" + author + "|" + etal + "))"
year_num = "(?:19|20)[0-9][0-9]"
page_num = "(?:, p.? [0-9]+)?" # Always optional
year = "(?:, *"+year_num+page_num+"| *\("+year_num+page_num+"\))"
regex = "(" + author + additional+"*" + year + ")"
matches = re.findall(regex, text)
matches = list( dict.fromkeys(matches) )
matches.sort()
#print(matches)
print ("\n".join(matches))
但是,它会将一些大写单词识别为作者姓名。比如在文中:
Although James (2020) recognized blablabla, Smith et al. (2020) found mimimi.
Those inconsistent results are a sign of lalala (Green, 2010; Grimm, 1990).
Also James (2020) ...
输出将是
Also James (2020)
Although James (2020)
Green, 2010
Grimm, 1990
Smith et al. (2020)
有没有办法在不删除整个匹配项的情况下将上述代码中的某些单词“列入黑名单”?我希望它能够识别 James 的工作,但从引文中删除了“Also”和“Although”。
提前致谢。
【问题讨论】:
标签: python regex text citations