Skip to content

Commit

Permalink
Rewrite protect_case(): simplify, protect individual letters. Related…
Browse files Browse the repository at this point in the history
… to #9
  • Loading branch information
kasnerz committed Mar 12, 2023
1 parent b99cf0c commit fdea5b7
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions reffix/reffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,35 @@ def protect_titlecase(title):
words = []

for word in title.split():
if word[0] == "{" and word[-1] == "}":
if "{" in word:
# already (presumably) protected
words.append(word)
else:
protect = False
continue

letters = []

for i, letter in enumerate(word):
# protect individual capital letters
protect = True
subwords = word.split("-")
for sw in subwords:
if len(subwords) > 1 and len(sw) == 1:
# 3-D, U-Net
protect = True
break
if any(l.isupper() for l in sw[1:]):
# Only considers capitals after the first letter
# Mip-NeRF: protect
# Spatially-Varying: don't protect
protect = True
break
if protect:
# leave the : out of the protection
if word[-1] == ":":
words.append(r"{" + word[:-1] + r"}:")
else:
words.append(r"{" + word + r"}")

if (
len(subwords) > 1
and len(subwords[0]) > 1
and i > 0
and word[i - 1] == "-"
and all(l.islower() for l in word[i + 1 :])
):
# 3-D, U-Net, Mip-NeRF: protect
# Spatially-Varying: don't protect
protect = False

if letter.isupper() and protect:
letters.append(r"{" + letter + r"}")
else:
words.append(word)
letters.append(letter)

words.append("".join(letters))

return " ".join(words)

Expand Down

0 comments on commit fdea5b7

Please sign in to comment.