Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mismatching of sentence final punctuation #100

Merged
merged 7 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions opuscleaner/filters/fix_sent_final_punct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bilingual",
"description": "Fixes mismatched punctuation at the end of the sentences. Works for latin/cyrillic based languages, WILL BREAK CJK AND ANY LANGUAGE THAT USES NON ENGLISH LIKE SENTENCE ENDING TOKENS.",
"parameters": {},
"command": "./fix_sent_final_punct.py"
}
40 changes: 40 additions & 0 deletions opuscleaner/filters/fix_sent_final_punct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
import sys

my_punct = {'!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', '»', '«', '“', '”'}

for line in sys.stdin:
src, trg = line.rstrip("\r\n").split("\t")
if len(src) == 0 or len(trg) == 0:
print(src + '\t' + trg)
continue

# Sometimes we have a space between the final letter and the punctuation, which is wrong except if using french quotes
if len(src) >= 2 and src[-1] in my_punct and src[-2] == " " and src[-1] != '»' and src[-1] != '«':
src = src[:-2] + src[-1]
if len(trg) >= 2 and trg[-1] in my_punct and trg[-2] == " " and trg[-1] != '»' and trg[-1] != '«':
Comment on lines +13 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are two spaces/end puncts? Such as weird quote ."

Copy link
Collaborator Author

@XapaJIaMnu XapaJIaMnu Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other filters should take care of this (normalising whitespace)

if len(src) >= 2 and src[-1] in my_punct and src[-2] == " " and src[-1] != '»' and src[-1] != '«':

trg = trg[:-2] + trg[-1]
# Sometimes two punctuation marks are swapped...
if len(src) >=2 and len(trg) >= 2 and src[-2] == trg[-1] and src[-1] == trg[-2]:
trg = trg[:-2] + src[-2] + src[-1]
# Sometimes they are swapped with space around eg SPACE». -> .SPACE»
if len(src) >=3 and src[-1] in my_punct and src[-2] == '»' and src[-3] == ' ':
src = src[:-3] + src[-1] + ' ' + src[-2]
if len(trg) >=3 and trg[-1] in my_punct and trg[-2] == '»' and trg[-3] == ' ':
trg = trg[:-3] + trg[-1] + ' ' + trg[-2]


# check for the french quotes special case
if (src[-1] == '»' or src[-1] == '«') and trg[-1] not in my_punct:
trg = trg + '"'
elif (trg[-1] == '»' or trg[-1] == '«') and src[-1] not in my_punct:
src = src + '"'
elif src[-1] in my_punct and trg[-1] not in my_punct:
trg = trg + src[-1]
elif trg[-1] in my_punct and src[-1] not in my_punct:
src = src + trg[-1]
# Final case. Fix mismatched punctuation on the src and trg. EXCEPT in cases like french quotes. And in cases where we have emdash at the front, as it means spech
elif trg[-1] in my_punct and src[-1] in my_punct and src[-1] != trg[-1] and src[-1] != '»' \
and src[-1] != '«' and trg[-1] != '»' and trg[-1] != '«' and src[0] != '–' and trg[0] != '–' and src[0] != '—' and trg[0] != '—':
Comment on lines +37 to +38

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting the special cases like and not(case1 or case2 or..) is much more readable.

What about the situation when we have end of quote." mapping to fin de devis».

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the situation when we have end of quote." mapping to fin de devis».
This is handled earlier in another edge case.

trg = trg[:-1] + src[-1]
print(src + '\t' + trg)