Skip to content

Instantly share code, notes, and snippets.

@planetis-m
Last active September 22, 2024 14:19
Show Gist options
  • Save planetis-m/9c9d553a27a3094189265d6476cd8ce3 to your computer and use it in GitHub Desktop.
Save planetis-m/9c9d553a27a3094189265d6476cd8ce3 to your computer and use it in GitHub Desktop.
def is_diacritics_correct(word):
diaeresis_chars = 'ϊϋΐΰ'
vowels_with_accent = 'άέήίόύώ'
diphthongs = ['αι', 'ει', 'οι', 'υι', 'αυ', 'ευ', 'ου']
once = False
for i, char in enumerate(word):
if char in diaeresis_chars:
if once:
return False
once = True
# Rule 1: Check if the preceding vowel is stressed.
if i >= 1 and word[i-1] in vowels_with_accent:
return False
# Rule 2: Check if there's a diphthong before the diacritics.
# (Accented diphthongs are caught by the previous rule.)
if i >= 2 and word[i-2:i] in diphthongs:
return False
# Rule 3: Check if there's no diphthong in the word.
# Only consider vowel combinations ending in ι or υ (e.g., ηυ, ιυ, ωυ, ηι, ωι).
if i >= 1 and ((char in 'ϊΐ' and word[i-1] in 'ηω') or \
(char in 'ϋΰ' and word[i-1] in 'ηιω')):
return False
# If none of the rules apply, diacritics might be necessary.
return True
# Test examples (Correct)
words = ["νεράιδα", "άυλος", "δρύινος", "πλάι", "κορόιδεψα", "Μάιος", "τέιον",
"αρτοποιία", "υιικός", "πιγκουίνος", "παλαιικός", "αλληλούια", "θειικός",
"Πομπηία", "Μωυσής", "διυλιστήριο", "πρωί", "πρωινός", "ζωικός", "ηρωικός"
"αϊδεύω", "παϊδάκια", "Μαΐου", "θεϊκός", "ευφυΐα", "ευνοϊκός", "ναυσιπλοΐα",
"κοροϊδεύω", "μυϊκός", "προϋπηρεσία", "εμποροϋπάλληλος", "καταπραϋντικός",
"Ταΰγετος", "ξεϋφαίνω"]
# # Test the function
# words = [
# 'νεράιδα', # Correct
# 'άυλος', # Correct
# 'πλάι', # Correct
# 'Μάιος', # Correct
# 'αρτοποιΐα', # Incorrect: Rule 2
# 'πιγκουΐνος', # Incorrect: Rule 2
# 'Πομπηΐα', # Incorrect: Rule 3
# 'Μωϋσής', # Incorrect: Rule 3
# 'πρωΐ', # Incorrect: Rule 3
# 'προΐκα', # Incorrect: None of the rules apply
# 'ηρωΐκός', # Incorrect: Rule 3
# ]
for word in words:
print(f"{word}: {'Correctly spelled' if is_diacritics_correct(word) else 'Misspelled'}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment