I was adapting this code for extracting citations from a text:
#!/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))
However, it recognizes some uppercased words as author names. For example, in the text:
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) ...
The output would be
Also James (2020)
Although James (2020)
Green, 2010
Grimm, 1990
Smith et al. (2020)
Is there a way to "blacklist" some words in the above code without removing the entire match? I wish it recognized James' work but removed "Also" and "Although" from the citation.
Thanks in advance.