grc-utils 0.2.1__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. grc_utils-1.0.0/LICENSE +21 -0
  2. grc_utils-1.0.0/PKG-INFO +106 -0
  3. grc_utils-1.0.0/README.md +90 -0
  4. grc_utils-1.0.0/grc_utils/__init__.py +27 -0
  5. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/clitics.py +8 -5
  6. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/filter_dichrona.py +93 -69
  7. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/grc_numerals.py +15 -1
  8. grc_utils-0.2.1/grc_utils/lower_grc.py → grc_utils-1.0.0/grc_utils/letter_case.py +57 -10
  9. grc_utils-1.0.0/grc_utils/sorting.py +115 -0
  10. grc_utils-0.2.1/grc_utils/syllabifier.py → grc_utils-1.0.0/grc_utils/syllabification.py +122 -61
  11. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/utils.py +18 -1
  12. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/weight.py +36 -16
  13. grc_utils-1.0.0/grc_utils.egg-info/PKG-INFO +106 -0
  14. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils.egg-info/SOURCES.txt +12 -4
  15. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils.egg-info/top_level.txt +0 -1
  16. {grc_utils-0.2.1 → grc_utils-1.0.0}/pyproject.toml +9 -2
  17. grc_utils-1.0.0/tests/test_case_and_numerals.py +30 -0
  18. grc_utils-1.0.0/tests/test_filter_dichrona.py +59 -0
  19. grc_utils-1.0.0/tests/test_norma.py +76 -0
  20. grc_utils-1.0.0/tests/test_package.py +34 -0
  21. grc_utils-1.0.0/tests/test_sorting.py +56 -0
  22. grc_utils-1.0.0/tests/test_syllabifier.py +82 -0
  23. grc_utils-1.0.0/tests/test_syllabifier_modes.py +63 -0
  24. grc_utils-1.0.0/tests/test_weight.py +27 -0
  25. grc_utils-0.2.1/LICENSE +0 -674
  26. grc_utils-0.2.1/PKG-INFO +0 -23
  27. grc_utils-0.2.1/README.md +0 -8
  28. grc_utils-0.2.1/grc_utils/__init__.py +0 -16
  29. grc_utils-0.2.1/grc_utils/sort_grc.py +0 -0
  30. grc_utils-0.2.1/grc_utils.egg-info/PKG-INFO +0 -23
  31. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/consonants.py +0 -0
  32. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/dichrona.py +0 -0
  33. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/macrons_map.py +0 -0
  34. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/vowels.py +0 -0
  35. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/vowels_long.py +0 -0
  36. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils/vowels_short.py +0 -0
  37. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils.egg-info/dependency_links.txt +0 -0
  38. {grc_utils-0.2.1 → grc_utils-1.0.0}/grc_utils.egg-info/requires.txt +0 -0
  39. {grc_utils-0.2.1 → grc_utils-1.0.0}/setup.cfg +0 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Albin Thörn Cleland
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,106 @@
1
+ Metadata-Version: 2.4
2
+ Name: grc-utils
3
+ Version: 1.0.0
4
+ Summary: NLP utilities for Ancient Greek
5
+ Author-email: Albin Ruben Johannes Thörn Cleland <albin.thorn_cleland@klass.lu.se>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Urdatorn/grc-utils
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: numpy>=1.21.0
15
+ Dynamic: license-file
16
+
17
+ # Ancient Greek NLP utilities
18
+
19
+ ## Modules
20
+
21
+ - `clitics`
22
+ - `get_accent`
23
+ - `is_enclitic`
24
+ - `is_proclitic`
25
+ - `remove_accents`
26
+ - `consonants`
27
+ - `liquida`
28
+ - `muta`
29
+ - `dichrona`
30
+ - `DICHRONA`
31
+ - `filter_dichrona`
32
+ - `colour_dichrona_in_open_syllables`
33
+ - `count_ambiguous_dichrona_in_open_syllables`
34
+ - `count_dichrona_in_open_syllables`
35
+ - `has_ambiguous_dichrona`
36
+ - `has_ambiguous_dichrona_in_open_syllables`
37
+ - `has_iota_adscriptum`
38
+ - `has_iota_subscriptum`
39
+ - `is_diphthong`
40
+ - `long_acute`
41
+ - `macronization_stats`
42
+ - `make_only_greek`
43
+ - `paroxytone`
44
+ - `paroxytone_long_penultima_with_dichronon_only_in_ultima`
45
+ - `paroxytone_short_ultima_with_dichronon_only_in_penultima`
46
+ - `penultima`
47
+ - `proparoxytone`
48
+ - `proparoxytone_with_dichronon_only_in_ultima`
49
+ - `properispomenon`
50
+ - `properispomenon_with_dichronon_only_in_ultima`
51
+ - `short_vowel`
52
+ - `ultima`
53
+ - `word_with_real_dichrona`
54
+ - `grc_numerals`
55
+ - `is_greek_numeral`
56
+ - `letter_case`
57
+ - `lower_grc`
58
+ - `upper_grc`
59
+ - `macrons_map`
60
+ - `macrons_map`
61
+ - `sorting`
62
+ - `sort_grc`
63
+ - `sort_key`
64
+ - `syllabification`
65
+ - `definitive_syllables`
66
+ - `divide_into_elements`
67
+ - `final_reshuffle`
68
+ - `is_consonant`
69
+ - `is_vowel`
70
+ - `reshuffle_consonants`
71
+ - `syllabifier`
72
+ - `syllabify`
73
+ - `utils`
74
+ - `base`
75
+ - `compose`
76
+ - `contains_greek`
77
+ - `no_macrons`
78
+ - `normalize_word`
79
+ - `only_bases`
80
+ - `oxia_to_tonos`
81
+ - `vowels`
82
+ - `vowel`
83
+ - `vowels_long`
84
+ - `long_set`
85
+ - `vowels_short`
86
+ - `short_set`
87
+ - `weight`
88
+ - `heavy`
89
+ - `is_open_syllable_in_word_in_synapheia`
90
+ - `open_syllable_in_word`
91
+
92
+ ## Tests
93
+
94
+ ```sh
95
+ pip install -e . pytest
96
+ pytest # unit tests and doctests
97
+ pytest --norma # also syllabifies the syllabify task of Norma (needs huggingface_hub)
98
+ ```
99
+
100
+ ## License
101
+
102
+ MIT (see `LICENSE`). `clitics.py` is adapted from Anna Conser's [greek_scansion](https://github.com/aconser/greek_scansion) (MIT).
103
+
104
+ The original syllabifier was written by Eric Cullhed; Albin Thörn Cleland has made it more robust for Unicode and fixed bugs.
105
+
106
+ © Albin Thörn Cleland, PhD student at Lunds universitet, Sweden.
@@ -0,0 +1,90 @@
1
+ # Ancient Greek NLP utilities
2
+
3
+ ## Modules
4
+
5
+ - `clitics`
6
+ - `get_accent`
7
+ - `is_enclitic`
8
+ - `is_proclitic`
9
+ - `remove_accents`
10
+ - `consonants`
11
+ - `liquida`
12
+ - `muta`
13
+ - `dichrona`
14
+ - `DICHRONA`
15
+ - `filter_dichrona`
16
+ - `colour_dichrona_in_open_syllables`
17
+ - `count_ambiguous_dichrona_in_open_syllables`
18
+ - `count_dichrona_in_open_syllables`
19
+ - `has_ambiguous_dichrona`
20
+ - `has_ambiguous_dichrona_in_open_syllables`
21
+ - `has_iota_adscriptum`
22
+ - `has_iota_subscriptum`
23
+ - `is_diphthong`
24
+ - `long_acute`
25
+ - `macronization_stats`
26
+ - `make_only_greek`
27
+ - `paroxytone`
28
+ - `paroxytone_long_penultima_with_dichronon_only_in_ultima`
29
+ - `paroxytone_short_ultima_with_dichronon_only_in_penultima`
30
+ - `penultima`
31
+ - `proparoxytone`
32
+ - `proparoxytone_with_dichronon_only_in_ultima`
33
+ - `properispomenon`
34
+ - `properispomenon_with_dichronon_only_in_ultima`
35
+ - `short_vowel`
36
+ - `ultima`
37
+ - `word_with_real_dichrona`
38
+ - `grc_numerals`
39
+ - `is_greek_numeral`
40
+ - `letter_case`
41
+ - `lower_grc`
42
+ - `upper_grc`
43
+ - `macrons_map`
44
+ - `macrons_map`
45
+ - `sorting`
46
+ - `sort_grc`
47
+ - `sort_key`
48
+ - `syllabification`
49
+ - `definitive_syllables`
50
+ - `divide_into_elements`
51
+ - `final_reshuffle`
52
+ - `is_consonant`
53
+ - `is_vowel`
54
+ - `reshuffle_consonants`
55
+ - `syllabifier`
56
+ - `syllabify`
57
+ - `utils`
58
+ - `base`
59
+ - `compose`
60
+ - `contains_greek`
61
+ - `no_macrons`
62
+ - `normalize_word`
63
+ - `only_bases`
64
+ - `oxia_to_tonos`
65
+ - `vowels`
66
+ - `vowel`
67
+ - `vowels_long`
68
+ - `long_set`
69
+ - `vowels_short`
70
+ - `short_set`
71
+ - `weight`
72
+ - `heavy`
73
+ - `is_open_syllable_in_word_in_synapheia`
74
+ - `open_syllable_in_word`
75
+
76
+ ## Tests
77
+
78
+ ```sh
79
+ pip install -e . pytest
80
+ pytest # unit tests and doctests
81
+ pytest --norma # also syllabifies the syllabify task of Norma (needs huggingface_hub)
82
+ ```
83
+
84
+ ## License
85
+
86
+ MIT (see `LICENSE`). `clitics.py` is adapted from Anna Conser's [greek_scansion](https://github.com/aconser/greek_scansion) (MIT).
87
+
88
+ The original syllabifier was written by Eric Cullhed; Albin Thörn Cleland has made it more robust for Unicode and fixed bugs.
89
+
90
+ © Albin Thörn Cleland, PhD student at Lunds universitet, Sweden.
@@ -0,0 +1,27 @@
1
+ # grc_utils/__init__.py
2
+
3
+ import sys as _sys
4
+
5
+ from .clitics import *
6
+ from .consonants import *
7
+ from .dichrona import *
8
+ from .filter_dichrona import *
9
+ from .grc_numerals import *
10
+ from .letter_case import *
11
+ from .macrons_map import *
12
+ from .sorting import *
13
+ from .syllabification import patterns, syllabifier
14
+ from .utils import *
15
+ from .vowels_long import *
16
+ from .vowels_short import *
17
+ from .vowels import *
18
+ from .weight import *
19
+
20
+ from . import letter_case, syllabification
21
+
22
+ # The modules syllabification.py and letter_case.py used to be called syllabifier.py and
23
+ # lower_grc.py, the same names as the functions they define, so that the package attribute
24
+ # grc_utils.syllabifier (the function) shadowed the module. The old module paths still
25
+ # work for imports such as `from grc_utils.syllabifier import patterns`.
26
+ _sys.modules[__name__ + '.syllabifier'] = syllabification
27
+ _sys.modules[__name__ + '.lower_grc'] = letter_case
@@ -1,4 +1,5 @@
1
- '''Extracted from Anna Conser's Greek_Prosody.class_word.py'''
1
+ '''Extracted from Anna Conser's Greek_Prosody.class_word.py
2
+ (now https://github.com/aconser/greek_scansion, MIT License, (c) Anna Conser)'''
2
3
 
3
4
  import re
4
5
  import unicodedata
@@ -11,7 +12,9 @@ ACUTE = u'\u0301'
11
12
  GRAVE = u'\u0300'
12
13
  CIRCUMFLEX = u'\u0342'
13
14
 
14
- ACCENTS = [ACUTE, GRAVE, CIRCUMFLEX]
15
+ # Named COMBINING_ACCENTS, not ACCENTS, so that it does not collide in `from grc_utils import *`
16
+ # with vowels.ACCENTS (the set of precomposed accented vowels).
17
+ COMBINING_ACCENTS = [ACUTE, GRAVE, CIRCUMFLEX]
15
18
 
16
19
  ACCENT_DICT = {ACUTE: 'A',
17
20
  GRAVE: 'G',
@@ -23,13 +26,13 @@ def remove_accents (text):
23
26
  split_chs = unicodedata.normalize("NFD", text)
24
27
  accentless = ''
25
28
  for ch in split_chs:
26
- if ch not in ACCENTS:
29
+ if ch not in COMBINING_ACCENTS:
27
30
  accentless += ch
28
31
  return accentless
29
32
 
30
33
  def get_accent (syllable):
31
34
  chs = unicodedata.normalize("NFD", syllable)
32
- for a in ACCENTS:
35
+ for a in COMBINING_ACCENTS:
33
36
  if a in chs:
34
37
  return a
35
38
 
@@ -103,7 +106,7 @@ OCT_PROCLITICS = [re.sub('ς|σ', 'ϲ', w) for w in NORMAL_PROCLITICS if re.sear
103
106
  ENCLITICS = NORMAL_ENCLITICS + OCT_ENCLITICS
104
107
  PROCLITICS = NORMAL_PROCLITICS + OCT_PROCLITICS
105
108
 
106
- APOSTROPHE = u'\u02bc', #MODIFIER LETTER APOSTROPHE
109
+ APOSTROPHE = u'\u02bc' #MODIFIER LETTER APOSTROPHE
107
110
 
108
111
  def _clean (text):
109
112
  """Removes whitespace and punctuation from edges of a string. Punctuation
@@ -24,11 +24,12 @@ Note concerning the logical relationship between the five accentuation word clas
24
24
  import re
25
25
  import unicodedata
26
26
 
27
- from .utils import is_open_syllable_in_word_in_synapheia, open_syllable_in_word, oxia_to_tonos
28
27
  from .dichrona import DICHRONA
29
- from .syllabifier import patterns, syllabifier
28
+ from .syllabification import patterns, syllabifier
29
+ from .utils import oxia_to_tonos
30
30
  from .vowels_short import short_set
31
31
  from .vowels import ACUTES, vowel
32
+ from .weight import is_open_syllable_in_word_in_synapheia, open_syllable_in_word
32
33
 
33
34
  # ============================
34
35
  # Syllable Positions
@@ -39,25 +40,27 @@ from .vowels import ACUTES, vowel
39
40
 
40
41
  def ultima(word):
41
42
  '''
42
- >> ultima('ποτιδέρκομαι')
43
- >> μαι
43
+ The last syllable, or '' if there is none.
44
+ >>> ultima('ποτιδέρκομαι')
45
+ 'μαι'
46
+ >>> ultima('')
47
+ ''
44
48
  '''
45
49
  word = word.replace('_', '').replace('^', '')
46
- list_of_syllables = syllabifier(word)
47
- ultima = list_of_syllables[-1]
48
-
49
- return ultima
50
+ list_of_syllables = syllabifier(word) or []
51
+ return list_of_syllables[-1] if list_of_syllables else ''
50
52
 
51
53
  def penultima(word):
52
54
  '''
53
- >> penultima('ποτιδέρκομαι')
54
- >> κο
55
+ The second-last syllable, or '' if there is none (e.g. a monosyllable).
56
+ >>> penultima('ποτιδέρκομαι')
57
+ 'κο'
58
+ >>> penultima('καί')
59
+ ''
55
60
  '''
56
61
  word = word.replace('_', '').replace('^', '')
57
- list_of_syllables = syllabifier(word)
58
- penultima = list_of_syllables[-2]
59
-
60
- return penultima
62
+ list_of_syllables = syllabifier(word) or []
63
+ return list_of_syllables[-2] if len(list_of_syllables) >= 2 else ''
61
64
 
62
65
  # ============================
63
66
  # Accent Word Classes
@@ -67,59 +70,46 @@ def penultima(word):
67
70
  # paroxytone
68
71
  # proparoxytone
69
72
 
73
+ ACUTE_MARK = '\u0301' # acute, tonos and oxia all decompose to U+0301
74
+ CIRCUMFLEX_MARK = '\u0342'
75
+
76
+ def _has_mark(syllable, mark):
77
+ return mark in unicodedata.normalize('NFD', syllable)
78
+
70
79
  def properispomenon(word):
71
80
  '''
72
- >> properispomenon('ὗσον')
73
- >> True
81
+ >>> properispomenon('ὗσον')
82
+ True
83
+ >>> properispomenon('Ἆργος')
84
+ True
74
85
  '''
75
86
  word = word.replace('_', '').replace('^', '')
76
- list_of_syllables = syllabifier(word)
77
- if len(list_of_syllables) >= 2:
78
- penultima = list_of_syllables[-2]
79
- circumflexes = r'[ᾶῆῖῦῶἇἆἦἧἶἷὖὗὦὧἦἧἆἇὧὦᾆᾇᾷᾖᾗᾦᾧῷῇ]'
80
-
81
- if re.search(circumflexes, penultima):
82
- return True
83
- else:
84
- return False
85
- else:
86
- return False
87
+ list_of_syllables = syllabifier(word) or []
88
+ return len(list_of_syllables) >= 2 and _has_mark(list_of_syllables[-2], CIRCUMFLEX_MARK)
87
89
 
88
90
  def paroxytone(word):
89
91
  '''
90
- >> paroxytone('λελῠμένος')
91
- >> True
92
+ >>> paroxytone('λελῠμένος')
93
+ True
94
+ >>> paroxytone('λόγος')
95
+ True
96
+ >>> paroxytone('Ἄργος')
97
+ True
92
98
  '''
93
99
  word = word.replace('_', '').replace('^', '')
94
- list_of_syllables = syllabifier(word)
95
- if len(list_of_syllables) >= 2:
96
- penultima = list_of_syllables[-2]
97
- acutes = r'[άέήίύώἄἅἔἕὄὅἤἥἴἵὔὕὤὥΐΰᾄᾅᾴᾔᾕῄᾤᾥῴ]'
98
-
99
- if re.search(acutes, penultima):
100
- return True
101
- else:
102
- return False
103
- else:
104
- return False
100
+ list_of_syllables = syllabifier(word) or []
101
+ return len(list_of_syllables) >= 2 and _has_mark(list_of_syllables[-2], ACUTE_MARK)
105
102
 
106
103
  def proparoxytone(word):
107
104
  '''
108
- >> proparoxytone('ποτιδέρκομαι')
109
- >> True
105
+ >>> proparoxytone('ποτιδέρκομαι')
106
+ True
107
+ >>> proparoxytone('Ἄλογος')
108
+ True
110
109
  '''
111
110
  word = word.replace('_', '').replace('^', '')
112
- list_of_syllables = syllabifier(word)
113
- if len(list_of_syllables) >= 3:
114
- antepenultima = list_of_syllables[-3]
115
- acutes = r'[άέήόίύώἄἅἔἕὄὅἤἥἴἵὔὕὤὥΐΰᾄᾅᾴᾔᾕῄᾤᾥῴ]'
116
-
117
- if re.search(acutes, antepenultima):
118
- return True
119
- else:
120
- return False
121
- else:
122
- return False
111
+ list_of_syllables = syllabifier(word) or []
112
+ return len(list_of_syllables) >= 3 and _has_mark(list_of_syllables[-3], ACUTE_MARK)
123
113
 
124
114
  # ============================================================
125
115
  # Basic Properties Related to Syllable Weight and Vowel Length
@@ -189,9 +179,9 @@ def word_with_real_dichrona(string):
189
179
  non_dichrona_long_acutes = r'[ήώἤἥὤὥᾄᾅᾴᾔᾕῄᾤᾥῴ]'
190
180
 
191
181
  dichrona_long_acutes = {
192
- '\u1FB1\u0301' # macronized alpha with combining acute
193
- '\u1FD1\u0301' # macronized iota with combining acute
194
- '\u1FE1\u0301' # macronized ypsilon with combining acute
182
+ '\u1FB1\u0301', # macronized alpha with combining acute
183
+ '\u1FD1\u0301', # macronized iota with combining acute
184
+ '\u1FE1\u0301', # macronized ypsilon with combining acute
195
185
  "\u1f0c\u0304", # Ἄ̄ Greek Capital Letter Alpha with Psili and Macron
196
186
  "\u1f3c\u0304", # Ἴ̄ Greek Capital Letter Iota with Psili and Macron
197
187
  "\u1f0d\u0304", # Ἅ̄ Greek Capital Letter Alpha with Dasia and Macron
@@ -215,6 +205,12 @@ long_acutes = r'(' + '|'.join(re.escape(char) for char in dichrona_long_acutes)
215
205
  def long_acute(syllable):
216
206
  '''
217
207
  Function needed to compute the paroxytone version of the σωτῆρα-rule.
208
+ >>> long_acute('ᾱ́')
209
+ True
210
+ >>> long_acute('ή')
211
+ True
212
+ >>> long_acute('ά')
213
+ False
218
214
  '''
219
215
  if '_' in syllable and any(char in ACUTES for char in syllable):
220
216
  return True
@@ -223,26 +219,49 @@ def long_acute(syllable):
223
219
  def short_vowel(syllable):
224
220
  """
225
221
  Determines if a syllable has a short vowel. Compatible with caret markup of brevia.
222
+ A diphthong is long, except final -αι and -οι, which count as short for the accent.
223
+ >>> short_vowel('λος')
224
+ True
225
+ >>> short_vowel('ου')
226
+ False
227
+ >>> short_vowel('κει')
228
+ False
229
+ >>> short_vowel('ται')
230
+ True
231
+ >>> short_vowel('ταις')
232
+ False
233
+ >>> short_vowel('κα^')
234
+ True
226
235
  """
227
236
  if '^' in syllable:
228
237
  return True
229
238
 
230
- return any(vowel in syllable for vowel in short_set)
239
+ letters = [c for c in unicodedata.normalize('NFC', syllable) if unicodedata.category(c).startswith('L')]
240
+ for i, char in enumerate(letters):
241
+ if i + 1 < len(letters) and is_diphthong(char + letters[i + 1]):
242
+ pair = unicodedata.normalize('NFD', char)[0].lower() + unicodedata.normalize('NFD', letters[i + 1])[0]
243
+ return pair in ('αι', 'οι') and i + 2 == len(letters)
244
+ if char in short_set:
245
+ return True
246
+ return False
231
247
 
232
248
  def make_only_greek(string):
233
249
  """
234
250
  Extracts only valid Greek substrings using defined patterns.
235
251
  NB: Removes space and punctuation!
236
252
  NB! Does not work for combining characters with macrons!
237
- 'ἱππιᾱτρῐκός hippiātrikós' -> 'ἱππιᾱτρῐκός'
238
- 'δῆμος' -> 'δῆμος'
239
- '12345' -> ''
240
- 'ἀγορά' -> 'ἀγορά'
241
- 'Hello, world!' -> ''
242
- 'ἔσπερος * & ^ %$#@!' -> 'ἔσπερος'
243
- 'ᾄλφα βήτα' -> 'ᾄλφαβήτα'
244
- 'αἰέν ἀριστεύειν!' -> 'αἰένἀριστεύειν'
245
- 'μῆνιν ἄειδε θεά' -> 'μῆνινἄειδεθεά'
253
+ >>> make_only_greek('ἱππιᾱτρῐκός hippiātrikós')
254
+ 'ἱππιᾱτρῐκός'
255
+ >>> make_only_greek('12345')
256
+ ''
257
+ >>> make_only_greek('Hello, world!')
258
+ ''
259
+ >>> make_only_greek('ἔσπερος * & ^ %$#@!')
260
+ 'ἔσπερος'
261
+ >>> make_only_greek('ᾄλφα βήτα')
262
+ 'ᾄλφαβήτα'
263
+ >>> make_only_greek('μῆνιν ἄειδε θεά')
264
+ 'μῆνινἄειδεθεά'
246
265
  """
247
266
  valid_pattern = re.compile('|'.join(patterns.values()))
248
267
 
@@ -264,7 +283,12 @@ def paroxytone_short_ultima_with_dichronon_only_in_penultima(string):
264
283
  Is a word disambiguated by the σωτῆρᾰ-rule for penultimae?
265
284
 
266
285
  τὸ ἴον implies ῐ by the σωτῆρᾰ-rule, whereas τοῦ ἴου does not.
286
+ >>> paroxytone_short_ultima_with_dichronon_only_in_penultima('ἴον')
287
+ True
288
+ >>> paroxytone_short_ultima_with_dichronon_only_in_penultima('ἴου')
289
+ False
267
290
  '''
291
+ string = string.replace('_', '').replace('^', '')
268
292
  if not word_with_real_dichrona(string):
269
293
  return False
270
294
 
@@ -280,9 +304,9 @@ def paroxytone_short_ultima_with_dichronon_only_in_penultima(string):
280
304
  if not word_with_real_dichrona(penultima_str):
281
305
  return False
282
306
 
283
- pre_penultima = string[:-len(penultima_str)]
307
+ pre_penultima = string[:-(len(penultima_str) + len(ultima_str))]
284
308
 
285
- if pre_penultima and word_with_real_dichrona(pre_penultima) or word_with_real_dichrona(ultima_str):
309
+ if (pre_penultima and word_with_real_dichrona(pre_penultima)) or word_with_real_dichrona(ultima_str):
286
310
  return False
287
311
 
288
312
  return True
@@ -526,7 +550,7 @@ def count_ambiguous_dichrona_in_open_syllables(string):
526
550
  continue # Penultima disambiguated
527
551
  elif position == -1 and paroxytone(word) and long_acute(penultima):
528
552
  continue # Ultima disambiguated
529
- elif position == -1 and properispomenon(word) or proparoxytone(word):
553
+ elif position == -1 and (properispomenon(word) or proparoxytone(word)):
530
554
  continue # Ultima disambiguated
531
555
  elif any(char in '^_' for char in syllable): # means syllable has been macronized already
532
556
  continue
@@ -33,8 +33,22 @@ greek_numeral = re.compile(rf"""(?:
33
33
  [{greek_power_two}][{greek_power_one}]?[{greek_power_zero}]?
34
34
  )[{apostrophes}]""", re.VERBOSE) # the flag allows for multiline regex (i.e. does not match newlines and spaces)
35
35
 
36
+ # Thousands: the lower-left keraia ͵ (U+0375) before a units letter, e.g. ͵α = 1000,
37
+ # optionally followed by the rest of the number, e.g. ͵αφκγ' = 1523
38
+ thousands = re.compile(rf"͵[{greek_power_zero}](?:{greek_numeral.pattern}|[{greek_power_one}{greek_power_two}{greek_power_zero}]*[{apostrophes}]?)", re.VERBOSE)
39
+
36
40
  def is_greek_numeral(word):
37
- return bool(greek_numeral.fullmatch(word))
41
+ '''
42
+ >>> is_greek_numeral("ιβ'")
43
+ True
44
+ >>> is_greek_numeral("͵α")
45
+ True
46
+ >>> is_greek_numeral("͵αφκγ'")
47
+ True
48
+ >>> is_greek_numeral("καί")
49
+ False
50
+ '''
51
+ return bool(greek_numeral.fullmatch(word) or thousands.fullmatch(word))
38
52
 
39
53
  if __name__ == "__main__":
40
54
  print(greek_numeral.pattern)
@@ -1,3 +1,6 @@
1
+ import re
2
+ import unicodedata
3
+
1
4
  '''
2
5
  Python's built in .lower() and .upper() work terribly bad for polytonic Greek.
3
6
  Accents, breathings and iota subscripta vanish into the aether.
@@ -251,22 +254,66 @@ VOWELS_LOWER_TO_UPPER = {VOWELS_UPPER_TO_LOWER[key]: key for key in VOWELS_UPPER
251
254
  def lower_grc(string):
252
255
  '''
253
256
  The lists used have neither oxia nor composed chars, so normalizing is not of the essence.
257
+ Letters missing from the lists are lower-cased by their base letter, keeping their
258
+ combining marks, and a capital sigma at the end of a word becomes final ς.
259
+ >>> lower_grc('ἈΘΗΝΑ͂')
260
+ 'ἀθηνᾶ'
261
+ >>> lower_grc('ΛΌΓΟΣ, ΣΟΦΌΣ')
262
+ 'λόγος, σοφός'
254
263
  '''
255
- string_lower_consonants = "".join([CONSONANTS_UPPER_TO_LOWER.get(char, char) for char in string])
256
- string_lower_consonants_and_vowels = "".join([VOWELS_UPPER_TO_LOWER.get(char, char) for char in string_lower_consonants])
257
-
258
- return string_lower_consonants_and_vowels
264
+ out = []
265
+ for i, char in enumerate(string):
266
+ if char in CONSONANTS_UPPER_TO_LOWER:
267
+ low = CONSONANTS_UPPER_TO_LOWER[char]
268
+ elif char in VOWELS_UPPER_TO_LOWER:
269
+ low = VOWELS_UPPER_TO_LOWER[char]
270
+ else:
271
+ low = _swap_case_by_base(char, upper=False)
272
+ if char == '\u03a3' and i > 0 and string[i - 1].isalpha() and (i + 1 == len(string) or not (string[i + 1].isalpha() or string[i + 1] in "'’ʼ᾿")):
273
+ low = '\u03c2' # final sigma
274
+ out.append(low)
275
+ return _compose(''.join(out))
259
276
 
260
277
 
261
278
  def upper_grc(string):
262
279
  '''
263
- Strictly the inverse of lower_grc.
264
- The lists used have neither oxia nor composed chars, so normalizing is not of the essence.
280
+ Strictly the inverse of lower_grc. Vowels without a precomposed capital (e.g. ᾶ, since
281
+ Unicode has no capital alpha with perispomeni) become the capital letter followed by
282
+ the combining marks.
283
+ >>> upper_grc('ἀθηνᾶ')
284
+ 'ἈΘΗΝΑ͂'
285
+ >>> upper_grc('ὑπὲρ λόγος')
286
+ 'ὙΠῈΡ ΛΌΓΟΣ'
287
+ >>> lower_grc(upper_grc('ἀθηνᾶ'))
288
+ 'ἀθηνᾶ'
265
289
  '''
266
- string_upper_consonants = "".join([CONSONANTS_LOWER_TO_UPPER.get(char, char) for char in string])
267
- string_upper_consonants_and_vowels = "".join([VOWELS_LOWER_TO_UPPER.get(char, char) for char in string_upper_consonants])
268
-
269
- return string_upper_consonants_and_vowels
290
+ out = []
291
+ for char in string:
292
+ if char in CONSONANTS_LOWER_TO_UPPER:
293
+ out.append(CONSONANTS_LOWER_TO_UPPER[char])
294
+ elif char in VOWELS_LOWER_TO_UPPER:
295
+ out.append(VOWELS_LOWER_TO_UPPER[char])
296
+ else:
297
+ out.append(_swap_case_by_base(char, upper=True))
298
+ return ''.join(out)
299
+
300
+
301
+ def _swap_case_by_base(char, upper):
302
+ '''Changes the case of a Greek letter by its base letter, keeping its combining marks.'''
303
+ decomposed = unicodedata.normalize('NFD', char)
304
+ base = decomposed[0]
305
+ if upper and '\u03b1' <= base <= '\u03c9':
306
+ new_base = '\u03a3' if base == '\u03c2' else chr(ord(base) - 0x20)
307
+ elif not upper and '\u0391' <= base <= '\u03a9' and base != '\u03a2':
308
+ new_base = chr(ord(base) + 0x20)
309
+ else:
310
+ return char
311
+ return _compose(new_base + decomposed[1:]) if len(decomposed) > 1 else new_base
312
+
313
+
314
+ def _compose(string):
315
+ '''NFC-composes letters with their combining marks only (NFC would also turn U+037E into ';').'''
316
+ return re.sub(r'[^\W\d_][\u0300-\u036f]+', lambda m: unicodedata.normalize('NFC', m.group()), string)
270
317
 
271
318
  if __name__ == "__main__":
272
319
  if len(UPPER_SMOOTH) != len(LOWER_SMOOTH):