grc-utils 0.2.2__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.2 → grc_utils-1.0.0}/grc_utils/clitics.py +8 -5
  6. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/filter_dichrona.py +91 -68
  7. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/grc_numerals.py +15 -1
  8. grc_utils-0.2.2/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.2/grc_utils/syllabifier.py → grc_utils-1.0.0/grc_utils/syllabification.py +122 -61
  11. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/utils.py +18 -1
  12. {grc_utils-0.2.2 → 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.2 → grc_utils-1.0.0}/grc_utils.egg-info/SOURCES.txt +12 -4
  15. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils.egg-info/top_level.txt +0 -1
  16. {grc_utils-0.2.2 → 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.2/LICENSE +0 -674
  26. grc_utils-0.2.2/PKG-INFO +0 -23
  27. grc_utils-0.2.2/README.md +0 -8
  28. grc_utils-0.2.2/grc_utils/__init__.py +0 -16
  29. grc_utils-0.2.2/grc_utils/sort_grc.py +0 -0
  30. grc_utils-0.2.2/grc_utils.egg-info/PKG-INFO +0 -23
  31. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/consonants.py +0 -0
  32. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/dichrona.py +0 -0
  33. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/macrons_map.py +0 -0
  34. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/vowels.py +0 -0
  35. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/vowels_long.py +0 -0
  36. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils/vowels_short.py +0 -0
  37. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils.egg-info/dependency_links.txt +0 -0
  38. {grc_utils-0.2.2 → grc_utils-1.0.0}/grc_utils.egg-info/requires.txt +0 -0
  39. {grc_utils-0.2.2 → 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
@@ -25,7 +25,7 @@ import re
25
25
  import unicodedata
26
26
 
27
27
  from .dichrona import DICHRONA
28
- from .syllabifier import patterns, syllabifier
28
+ from .syllabification import patterns, syllabifier
29
29
  from .utils import oxia_to_tonos
30
30
  from .vowels_short import short_set
31
31
  from .vowels import ACUTES, vowel
@@ -40,25 +40,27 @@ from .weight import is_open_syllable_in_word_in_synapheia, open_syllable_in_word
40
40
 
41
41
  def ultima(word):
42
42
  '''
43
- >> ultima('ποτιδέρκομαι')
44
- >> μαι
43
+ The last syllable, or '' if there is none.
44
+ >>> ultima('ποτιδέρκομαι')
45
+ 'μαι'
46
+ >>> ultima('')
47
+ ''
45
48
  '''
46
49
  word = word.replace('_', '').replace('^', '')
47
- list_of_syllables = syllabifier(word)
48
- ultima = list_of_syllables[-1]
49
-
50
- return ultima
50
+ list_of_syllables = syllabifier(word) or []
51
+ return list_of_syllables[-1] if list_of_syllables else ''
51
52
 
52
53
  def penultima(word):
53
54
  '''
54
- >> penultima('ποτιδέρκομαι')
55
- >> κο
55
+ The second-last syllable, or '' if there is none (e.g. a monosyllable).
56
+ >>> penultima('ποτιδέρκομαι')
57
+ 'κο'
58
+ >>> penultima('καί')
59
+ ''
56
60
  '''
57
61
  word = word.replace('_', '').replace('^', '')
58
- list_of_syllables = syllabifier(word)
59
- penultima = list_of_syllables[-2]
60
-
61
- return penultima
62
+ list_of_syllables = syllabifier(word) or []
63
+ return list_of_syllables[-2] if len(list_of_syllables) >= 2 else ''
62
64
 
63
65
  # ============================
64
66
  # Accent Word Classes
@@ -68,59 +70,46 @@ def penultima(word):
68
70
  # paroxytone
69
71
  # proparoxytone
70
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
+
71
79
  def properispomenon(word):
72
80
  '''
73
- >> properispomenon('ὗσον')
74
- >> True
81
+ >>> properispomenon('ὗσον')
82
+ True
83
+ >>> properispomenon('Ἆργος')
84
+ True
75
85
  '''
76
86
  word = word.replace('_', '').replace('^', '')
77
- list_of_syllables = syllabifier(word)
78
- if len(list_of_syllables) >= 2:
79
- penultima = list_of_syllables[-2]
80
- circumflexes = r'[ᾶῆῖῦῶἇἆἦἧἶἷὖὗὦὧἦἧἆἇὧὦᾆᾇᾷᾖᾗᾦᾧῷῇ]'
81
-
82
- if re.search(circumflexes, penultima):
83
- return True
84
- else:
85
- return False
86
- else:
87
- 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)
88
89
 
89
90
  def paroxytone(word):
90
91
  '''
91
- >> paroxytone('λελῠμένος')
92
- >> True
92
+ >>> paroxytone('λελῠμένος')
93
+ True
94
+ >>> paroxytone('λόγος')
95
+ True
96
+ >>> paroxytone('Ἄργος')
97
+ True
93
98
  '''
94
99
  word = word.replace('_', '').replace('^', '')
95
- list_of_syllables = syllabifier(word)
96
- if len(list_of_syllables) >= 2:
97
- penultima = list_of_syllables[-2]
98
- acutes = r'[άέήίύώἄἅἔἕὄὅἤἥἴἵὔὕὤὥΐΰᾄᾅᾴᾔᾕῄᾤᾥῴ]'
99
-
100
- if re.search(acutes, penultima):
101
- return True
102
- else:
103
- return False
104
- else:
105
- 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)
106
102
 
107
103
  def proparoxytone(word):
108
104
  '''
109
- >> proparoxytone('ποτιδέρκομαι')
110
- >> True
105
+ >>> proparoxytone('ποτιδέρκομαι')
106
+ True
107
+ >>> proparoxytone('Ἄλογος')
108
+ True
111
109
  '''
112
110
  word = word.replace('_', '').replace('^', '')
113
- list_of_syllables = syllabifier(word)
114
- if len(list_of_syllables) >= 3:
115
- antepenultima = list_of_syllables[-3]
116
- acutes = r'[άέήόίύώἄἅἔἕὄὅἤἥἴἵὔὕὤὥΐΰᾄᾅᾴᾔᾕῄᾤᾥῴ]'
117
-
118
- if re.search(acutes, antepenultima):
119
- return True
120
- else:
121
- return False
122
- else:
123
- 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)
124
113
 
125
114
  # ============================================================
126
115
  # Basic Properties Related to Syllable Weight and Vowel Length
@@ -190,9 +179,9 @@ def word_with_real_dichrona(string):
190
179
  non_dichrona_long_acutes = r'[ήώἤἥὤὥᾄᾅᾴᾔᾕῄᾤᾥῴ]'
191
180
 
192
181
  dichrona_long_acutes = {
193
- '\u1FB1\u0301' # macronized alpha with combining acute
194
- '\u1FD1\u0301' # macronized iota with combining acute
195
- '\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
196
185
  "\u1f0c\u0304", # Ἄ̄ Greek Capital Letter Alpha with Psili and Macron
197
186
  "\u1f3c\u0304", # Ἴ̄ Greek Capital Letter Iota with Psili and Macron
198
187
  "\u1f0d\u0304", # Ἅ̄ Greek Capital Letter Alpha with Dasia and Macron
@@ -216,6 +205,12 @@ long_acutes = r'(' + '|'.join(re.escape(char) for char in dichrona_long_acutes)
216
205
  def long_acute(syllable):
217
206
  '''
218
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
219
214
  '''
220
215
  if '_' in syllable and any(char in ACUTES for char in syllable):
221
216
  return True
@@ -224,26 +219,49 @@ def long_acute(syllable):
224
219
  def short_vowel(syllable):
225
220
  """
226
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
227
235
  """
228
236
  if '^' in syllable:
229
237
  return True
230
238
 
231
- 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
232
247
 
233
248
  def make_only_greek(string):
234
249
  """
235
250
  Extracts only valid Greek substrings using defined patterns.
236
251
  NB: Removes space and punctuation!
237
252
  NB! Does not work for combining characters with macrons!
238
- 'ἱππιᾱτρῐκός hippiātrikós' -> 'ἱππιᾱτρῐκός'
239
- 'δῆμος' -> 'δῆμος'
240
- '12345' -> ''
241
- 'ἀγορά' -> 'ἀγορά'
242
- 'Hello, world!' -> ''
243
- 'ἔσπερος * & ^ %$#@!' -> 'ἔσπερος'
244
- 'ᾄλφα βήτα' -> 'ᾄλφαβήτα'
245
- 'αἰέν ἀριστεύειν!' -> 'αἰένἀριστεύειν'
246
- 'μῆνιν ἄειδε θεά' -> 'μῆνινἄειδεθεά'
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
+ 'μῆνινἄειδεθεά'
247
265
  """
248
266
  valid_pattern = re.compile('|'.join(patterns.values()))
249
267
 
@@ -265,7 +283,12 @@ def paroxytone_short_ultima_with_dichronon_only_in_penultima(string):
265
283
  Is a word disambiguated by the σωτῆρᾰ-rule for penultimae?
266
284
 
267
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
268
290
  '''
291
+ string = string.replace('_', '').replace('^', '')
269
292
  if not word_with_real_dichrona(string):
270
293
  return False
271
294
 
@@ -281,9 +304,9 @@ def paroxytone_short_ultima_with_dichronon_only_in_penultima(string):
281
304
  if not word_with_real_dichrona(penultima_str):
282
305
  return False
283
306
 
284
- pre_penultima = string[:-len(penultima_str)]
307
+ pre_penultima = string[:-(len(penultima_str) + len(ultima_str))]
285
308
 
286
- 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):
287
310
  return False
288
311
 
289
312
  return True
@@ -527,7 +550,7 @@ def count_ambiguous_dichrona_in_open_syllables(string):
527
550
  continue # Penultima disambiguated
528
551
  elif position == -1 and paroxytone(word) and long_acute(penultima):
529
552
  continue # Ultima disambiguated
530
- elif position == -1 and properispomenon(word) or proparoxytone(word):
553
+ elif position == -1 and (properispomenon(word) or proparoxytone(word)):
531
554
  continue # Ultima disambiguated
532
555
  elif any(char in '^_' for char in syllable): # means syllable has been macronized already
533
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):