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

Strip accent and word #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 30 additions & 28 deletions greek_accentuation/accentuation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import enum

from .characters import add_diacritic
from .characters import add_diacritic, base
from .characters import Accent, Length, Breathing
from .syllabify import onset_nucleus_coda, syllabify, syllable_length
from .syllabify import syllable_accent, ultima, penult, antepenult
from .syllabify import debreath, rebreath


class Accentuation(enum.Enum):
Expand Down Expand Up @@ -32,18 +33,14 @@ def __ne__(self, other):
return not self.__eq__(other)

def __lt__(self, other):
other_value = (
other.raw_value if isinstance(other, Accentuation) else other
)
other_value = other.raw_value if isinstance(other, Accentuation) else other
return self.raw_value < other_value

def __le__(self, other):
return self.__lt__(other) or self.__eq__(other)

def __gt__(self, other):
other_value = (
other.raw_value if isinstance(other, Accentuation) else other
)
other_value = other.raw_value if isinstance(other, Accentuation) else other
return self.raw_value > other_value

def __ge__(self, other):
Expand All @@ -60,10 +57,22 @@ def syllable_add_accent(s, a):

def add_accentuation(s, accentuation):
pos, accent = accentuation.value
final = s[1 - pos:] if pos > 1 else [""]
final = s[1 - pos :] if pos > 1 else [""]
return "".join(s[:-pos] + [syllable_add_accent(s[-pos], accent)] + final)


def strip_accentuation(word):
"strip accentuation of a given word conserving breathing"
deword = debreath(word)
no_accent_word = "".join([base(a) for a in word])
return rebreath(no_accent_word)


def strip_word(word):
"strip both accentuation and breathing"
return "".join([base(a) for a in word])


def display_accentuation(accentuation):
return accentuation.name.lower()

Expand Down Expand Up @@ -116,9 +125,7 @@ def get_accentuation(w):
return Accentuation.PROPAROXYTONE


def possible_accentuations(
s, treat_final_AI_OI_short=True, default_short=False
):
def possible_accentuations(s, treat_final_AI_OI_short=True, default_short=False):
ultima_length = syllable_length(s[-1], treat_final_AI_OI_short)
penult_length = syllable_length(s[-2], False) if len(s) >= 2 else None
if ultima_length == Length.UNKNOWN and default_short:
Expand All @@ -131,16 +138,17 @@ def possible_accentuations(
if not (ultima_length == Length.SHORT):
yield Accentuation.PERISPOMENON

if (len(s) >= 2 and not
(penult_length == Length.LONG and ultima_length == Length.SHORT)):
if len(s) >= 2 and not (
penult_length == Length.LONG and ultima_length == Length.SHORT
):
yield Accentuation.PAROXYTONE

if (len(s) >= 2 and not
(penult_length == Length.SHORT or ultima_length == Length.LONG)):
if len(s) >= 2 and not (
penult_length == Length.SHORT or ultima_length == Length.LONG
):
yield Accentuation.PROPERISPOMENON

if (len(s) >= 3 and not
(ultima_length == Length.LONG)):
if len(s) >= 3 and not (ultima_length == Length.LONG):
yield Accentuation.PROPAROXYTONE


Expand All @@ -155,8 +163,8 @@ def recessive(w, treat_final_AI_OI_short=True, default_short=False):
s,
sorted(
possible_accentuations(s, treat_final_AI_OI_short, default_short),
reverse=True
)[0]
reverse=True,
)[0],
)


Expand All @@ -167,9 +175,7 @@ def on_penult(w, default_short=False):
pre = ""

s = syllabify(w)
accentuations = list(
possible_accentuations(s, default_short=default_short)
)
accentuations = list(possible_accentuations(s, default_short=default_short))
if Accentuation.PROPERISPOMENON in accentuations:
return pre + add_accentuation(s, Accentuation.PROPERISPOMENON)
elif Accentuation.PAROXYTONE in accentuations:
Expand All @@ -187,17 +193,13 @@ def persistent(w, lemma, default_short=False):

place, accent = accentuation.value
s = syllabify(w)
possible = [
p.value for p in possible_accentuations(s, default_short=default_short)
]
possible = [p.value for p in possible_accentuations(s, default_short=default_short)]
place2 = len(s) - len(syllabify(lemma)) + place
accent_pair = (place2, accent)
if accent_pair not in possible:
if accent == Accent.ACUTE and (place2, Accent.CIRCUMFLEX) in possible:
accent_pair = (place2, Accent.CIRCUMFLEX)
elif (
accent == Accent.CIRCUMFLEX and (place2, Accent.ACUTE) in possible
):
elif accent == Accent.CIRCUMFLEX and (place2, Accent.ACUTE) in possible:
accent_pair = (place2, Accent.ACUTE)
else:
for i in range(1, 4):
Expand Down