prosodic.words.syllables

  1from typing import List, Optional, Any
  2from ..imports import *
  3from .phonemes import PhonemeList
  4
  5class Syllable(Entity):
  6    """
  7    Represents a syllable in a word.
  8
  9    Attributes:
 10        prefix (str): Prefix for the syllable.
 11        child_type (str): Type of child entities (Phoneme).
 12        list_type (str): Type of list for child entities (PhonemeList).
 13    """
 14
 15    prefix: str = "syll"
 16    child_type: str = "Phoneme"
 17    list_type: str = 'PhonemeList'
 18
 19    @profile
 20    def __init__(self, txt: str, ipa: Optional[str] = None, parent: Optional[Any] = None, children: List[Any] = [], **kwargs: Any) -> None:
 21        """
 22        Initialize a Syllable object.
 23
 24        Args:
 25            txt: The text representation of the syllable.
 26            ipa: The IPA representation of the syllable.
 27            parent: The parent entity of the syllable.
 28            children: List of child entities (Phonemes).
 29            **kwargs: Additional keyword arguments.
 30        """
 31        from .phonemes import Phoneme
 32        from gruut_ipa import Pronunciation
 33
 34        assert ipa or children
 35        if ipa and not children:
 36            sipa = "".join(x for x in ipa if x.isalpha())
 37            pron = Pronunciation.from_string(sipa)
 38            phones = [p.text for p in pron if p.text]
 39            children = [Phoneme(phon) for phon in phones]
 40        super().__init__(txt=txt, ipa=ipa, children=children, parent=parent, **kwargs)
 41
 42    def to_json(self) -> dict:
 43        """
 44        Convert the syllable to a JSON-serializable dictionary.
 45
 46        Returns:
 47            A dictionary representation of the syllable.
 48        """
 49        return super().to_json(ipa=self.ipa)
 50
 51    @cached_property
 52    def stress(self) -> str:
 53        """
 54        Get the stress level of the syllable.
 55
 56        Returns:
 57            The stress level as a string.
 58        """
 59        return get_stress(self.ipa)
 60
 61    @cached_property
 62    def attrs(self) -> dict:
 63        """
 64        Get the attributes of the syllable.
 65
 66        Returns:
 67            A dictionary of syllable attributes.
 68        """
 69        return {
 70            **self._attrs,
 71            "num": self.num,
 72            "txt": self.txt,
 73            "is_stressed": self.is_stressed,
 74            "is_heavy": self.is_heavy,
 75            "is_strong": self.is_strong,
 76            "is_weak": self.is_weak,
 77        }
 78
 79    @cached_property
 80    def has_consonant_ending(self) -> bool:
 81        """
 82        Check if the syllable ends with a consonant.
 83
 84        Returns:
 85            True if the syllable ends with a consonant, False otherwise.
 86        """
 87        return self.children[-1].cons > 0
 88
 89    @cached_property
 90    def num_vowels(self) -> int:
 91        """
 92        Get the number of vowels in the syllable.
 93
 94        Returns:
 95            The number of vowels.
 96        """
 97        return sum(1 for phon in self.children if phon.cons <= 0)
 98
 99    @cached_property
100    def has_dipthong(self) -> bool:
101        """
102        Check if the syllable contains a diphthong.
103
104        Returns:
105            True if the syllable has a diphthong, False otherwise.
106        """
107        return self.num_vowels > 1
108
109    @cached_property
110    def is_stressed(self) -> bool:
111        """
112        Check if the syllable is stressed.
113
114        Returns:
115            True if the syllable is stressed, False otherwise.
116        """
117        return self.stress in {"S", "P"}
118
119    @cached_property
120    def is_heavy(self) -> bool:
121        """
122        Check if the syllable is heavy.
123
124        Returns:
125            True if the syllable is heavy, False otherwise.
126        """
127        return bool(self.has_consonant_ending or self.has_dipthong)
128
129    @cached_property
130    def is_strong(self) -> Optional[bool]:
131        """
132        Check if the syllable is strong.
133
134        Returns:
135            True if the syllable is strong, False if weak, None if undetermined.
136        """
137        if not len(self.parent.children) > 1:
138            return None
139        if not self.is_stressed:
140            return False
141        if self.prev and not self.prev.is_stressed:
142            return True
143        if self.next and not self.next.is_stressed:
144            return True
145
146    @cached_property
147    def is_weak(self) -> Optional[bool]:
148        """
149        Check if the syllable is weak.
150
151        Returns:
152            True if the syllable is weak, False if strong, None if undetermined.
153        """
154        if not len(self.parent.children) > 1:
155            return None
156        if self.is_stressed:
157            return False
158        if self.prev and self.prev.is_stressed:
159            return True
160        if self.next and self.next.is_stressed:
161            return True
162
163    @cached_property
164    def onset(self) -> PhonemeList:
165        """
166        Get the onset of the syllable.
167
168        Returns:
169            A PhonemeList containing the onset phonemes.
170        """
171        return PhonemeList(p for p in self.children if p.is_onset)
172
173    @cached_property
174    def rime(self) -> PhonemeList:
175        """
176        Get the rime of the syllable.
177
178        Returns:
179            A PhonemeList containing the rime phonemes.
180        """
181        return PhonemeList(p for p in self.children if p.is_rime)
182
183    @cached_property
184    def nucleus(self) -> PhonemeList:
185        """
186        Get the nucleus of the syllable.
187
188        Returns:
189            A PhonemeList containing the nucleus phonemes.
190        """
191        return PhonemeList(p for p in self.children if p.is_nucleus)
192
193    @cached_property
194    def coda(self) -> PhonemeList:
195        """
196        Get the coda of the syllable.
197
198        Returns:
199            A PhonemeList containing the coda phonemes.
200        """
201        return PhonemeList(p for p in self.children if p.is_coda)
202
203    @cache
204    def rime_distance(self, syllable: 'Syllable') -> float:
205        """
206        Calculate the rime distance between this syllable and another.
207
208        Args:
209            syllable: The syllable to compare with.
210
211        Returns:
212            The rime distance as a float.
213        """
214        return self.wordform.rime_distance(syllable.wordform)
215
216
217class SyllableList(EntityList):
218    """A list of Syllable objects."""
219    pass
class Syllable(prosodic.ents.Entity):
  6class Syllable(Entity):
  7    """
  8    Represents a syllable in a word.
  9
 10    Attributes:
 11        prefix (str): Prefix for the syllable.
 12        child_type (str): Type of child entities (Phoneme).
 13        list_type (str): Type of list for child entities (PhonemeList).
 14    """
 15
 16    prefix: str = "syll"
 17    child_type: str = "Phoneme"
 18    list_type: str = 'PhonemeList'
 19
 20    @profile
 21    def __init__(self, txt: str, ipa: Optional[str] = None, parent: Optional[Any] = None, children: List[Any] = [], **kwargs: Any) -> None:
 22        """
 23        Initialize a Syllable object.
 24
 25        Args:
 26            txt: The text representation of the syllable.
 27            ipa: The IPA representation of the syllable.
 28            parent: The parent entity of the syllable.
 29            children: List of child entities (Phonemes).
 30            **kwargs: Additional keyword arguments.
 31        """
 32        from .phonemes import Phoneme
 33        from gruut_ipa import Pronunciation
 34
 35        assert ipa or children
 36        if ipa and not children:
 37            sipa = "".join(x for x in ipa if x.isalpha())
 38            pron = Pronunciation.from_string(sipa)
 39            phones = [p.text for p in pron if p.text]
 40            children = [Phoneme(phon) for phon in phones]
 41        super().__init__(txt=txt, ipa=ipa, children=children, parent=parent, **kwargs)
 42
 43    def to_json(self) -> dict:
 44        """
 45        Convert the syllable to a JSON-serializable dictionary.
 46
 47        Returns:
 48            A dictionary representation of the syllable.
 49        """
 50        return super().to_json(ipa=self.ipa)
 51
 52    @cached_property
 53    def stress(self) -> str:
 54        """
 55        Get the stress level of the syllable.
 56
 57        Returns:
 58            The stress level as a string.
 59        """
 60        return get_stress(self.ipa)
 61
 62    @cached_property
 63    def attrs(self) -> dict:
 64        """
 65        Get the attributes of the syllable.
 66
 67        Returns:
 68            A dictionary of syllable attributes.
 69        """
 70        return {
 71            **self._attrs,
 72            "num": self.num,
 73            "txt": self.txt,
 74            "is_stressed": self.is_stressed,
 75            "is_heavy": self.is_heavy,
 76            "is_strong": self.is_strong,
 77            "is_weak": self.is_weak,
 78        }
 79
 80    @cached_property
 81    def has_consonant_ending(self) -> bool:
 82        """
 83        Check if the syllable ends with a consonant.
 84
 85        Returns:
 86            True if the syllable ends with a consonant, False otherwise.
 87        """
 88        return self.children[-1].cons > 0
 89
 90    @cached_property
 91    def num_vowels(self) -> int:
 92        """
 93        Get the number of vowels in the syllable.
 94
 95        Returns:
 96            The number of vowels.
 97        """
 98        return sum(1 for phon in self.children if phon.cons <= 0)
 99
100    @cached_property
101    def has_dipthong(self) -> bool:
102        """
103        Check if the syllable contains a diphthong.
104
105        Returns:
106            True if the syllable has a diphthong, False otherwise.
107        """
108        return self.num_vowels > 1
109
110    @cached_property
111    def is_stressed(self) -> bool:
112        """
113        Check if the syllable is stressed.
114
115        Returns:
116            True if the syllable is stressed, False otherwise.
117        """
118        return self.stress in {"S", "P"}
119
120    @cached_property
121    def is_heavy(self) -> bool:
122        """
123        Check if the syllable is heavy.
124
125        Returns:
126            True if the syllable is heavy, False otherwise.
127        """
128        return bool(self.has_consonant_ending or self.has_dipthong)
129
130    @cached_property
131    def is_strong(self) -> Optional[bool]:
132        """
133        Check if the syllable is strong.
134
135        Returns:
136            True if the syllable is strong, False if weak, None if undetermined.
137        """
138        if not len(self.parent.children) > 1:
139            return None
140        if not self.is_stressed:
141            return False
142        if self.prev and not self.prev.is_stressed:
143            return True
144        if self.next and not self.next.is_stressed:
145            return True
146
147    @cached_property
148    def is_weak(self) -> Optional[bool]:
149        """
150        Check if the syllable is weak.
151
152        Returns:
153            True if the syllable is weak, False if strong, None if undetermined.
154        """
155        if not len(self.parent.children) > 1:
156            return None
157        if self.is_stressed:
158            return False
159        if self.prev and self.prev.is_stressed:
160            return True
161        if self.next and self.next.is_stressed:
162            return True
163
164    @cached_property
165    def onset(self) -> PhonemeList:
166        """
167        Get the onset of the syllable.
168
169        Returns:
170            A PhonemeList containing the onset phonemes.
171        """
172        return PhonemeList(p for p in self.children if p.is_onset)
173
174    @cached_property
175    def rime(self) -> PhonemeList:
176        """
177        Get the rime of the syllable.
178
179        Returns:
180            A PhonemeList containing the rime phonemes.
181        """
182        return PhonemeList(p for p in self.children if p.is_rime)
183
184    @cached_property
185    def nucleus(self) -> PhonemeList:
186        """
187        Get the nucleus of the syllable.
188
189        Returns:
190            A PhonemeList containing the nucleus phonemes.
191        """
192        return PhonemeList(p for p in self.children if p.is_nucleus)
193
194    @cached_property
195    def coda(self) -> PhonemeList:
196        """
197        Get the coda of the syllable.
198
199        Returns:
200            A PhonemeList containing the coda phonemes.
201        """
202        return PhonemeList(p for p in self.children if p.is_coda)
203
204    @cache
205    def rime_distance(self, syllable: 'Syllable') -> float:
206        """
207        Calculate the rime distance between this syllable and another.
208
209        Args:
210            syllable: The syllable to compare with.
211
212        Returns:
213            The rime distance as a float.
214        """
215        return self.wordform.rime_distance(syllable.wordform)

Represents a syllable in a word.

Attributes:
  • prefix (str): Prefix for the syllable.
  • child_type (str): Type of child entities (Phoneme).
  • list_type (str): Type of list for child entities (PhonemeList).
@profile
Syllable( txt: str, ipa: Optional[str] = None, parent: Optional[Any] = None, children: List[Any] = [], **kwargs: Any)
20    @profile
21    def __init__(self, txt: str, ipa: Optional[str] = None, parent: Optional[Any] = None, children: List[Any] = [], **kwargs: Any) -> None:
22        """
23        Initialize a Syllable object.
24
25        Args:
26            txt: The text representation of the syllable.
27            ipa: The IPA representation of the syllable.
28            parent: The parent entity of the syllable.
29            children: List of child entities (Phonemes).
30            **kwargs: Additional keyword arguments.
31        """
32        from .phonemes import Phoneme
33        from gruut_ipa import Pronunciation
34
35        assert ipa or children
36        if ipa and not children:
37            sipa = "".join(x for x in ipa if x.isalpha())
38            pron = Pronunciation.from_string(sipa)
39            phones = [p.text for p in pron if p.text]
40            children = [Phoneme(phon) for phon in phones]
41        super().__init__(txt=txt, ipa=ipa, children=children, parent=parent, **kwargs)

Initialize a Syllable object.

Arguments:
  • txt: The text representation of the syllable.
  • ipa: The IPA representation of the syllable.
  • parent: The parent entity of the syllable.
  • children: List of child entities (Phonemes).
  • **kwargs: Additional keyword arguments.
prefix: str = 'syll'
child_type: str = 'Phoneme'
list_type: str = 'PhonemeList'
def to_json(self) -> dict:
43    def to_json(self) -> dict:
44        """
45        Convert the syllable to a JSON-serializable dictionary.
46
47        Returns:
48            A dictionary representation of the syllable.
49        """
50        return super().to_json(ipa=self.ipa)

Convert the syllable to a JSON-serializable dictionary.

Returns:

A dictionary representation of the syllable.

stress: str
52    @cached_property
53    def stress(self) -> str:
54        """
55        Get the stress level of the syllable.
56
57        Returns:
58            The stress level as a string.
59        """
60        return get_stress(self.ipa)

Get the stress level of the syllable.

Returns:

The stress level as a string.

attrs: dict
62    @cached_property
63    def attrs(self) -> dict:
64        """
65        Get the attributes of the syllable.
66
67        Returns:
68            A dictionary of syllable attributes.
69        """
70        return {
71            **self._attrs,
72            "num": self.num,
73            "txt": self.txt,
74            "is_stressed": self.is_stressed,
75            "is_heavy": self.is_heavy,
76            "is_strong": self.is_strong,
77            "is_weak": self.is_weak,
78        }

Get the attributes of the syllable.

Returns:

A dictionary of syllable attributes.

has_consonant_ending: bool
80    @cached_property
81    def has_consonant_ending(self) -> bool:
82        """
83        Check if the syllable ends with a consonant.
84
85        Returns:
86            True if the syllable ends with a consonant, False otherwise.
87        """
88        return self.children[-1].cons > 0

Check if the syllable ends with a consonant.

Returns:

True if the syllable ends with a consonant, False otherwise.

num_vowels: int
90    @cached_property
91    def num_vowels(self) -> int:
92        """
93        Get the number of vowels in the syllable.
94
95        Returns:
96            The number of vowels.
97        """
98        return sum(1 for phon in self.children if phon.cons <= 0)

Get the number of vowels in the syllable.

Returns:

The number of vowels.

has_dipthong: bool
100    @cached_property
101    def has_dipthong(self) -> bool:
102        """
103        Check if the syllable contains a diphthong.
104
105        Returns:
106            True if the syllable has a diphthong, False otherwise.
107        """
108        return self.num_vowels > 1

Check if the syllable contains a diphthong.

Returns:

True if the syllable has a diphthong, False otherwise.

is_stressed: bool
110    @cached_property
111    def is_stressed(self) -> bool:
112        """
113        Check if the syllable is stressed.
114
115        Returns:
116            True if the syllable is stressed, False otherwise.
117        """
118        return self.stress in {"S", "P"}

Check if the syllable is stressed.

Returns:

True if the syllable is stressed, False otherwise.

is_heavy: bool
120    @cached_property
121    def is_heavy(self) -> bool:
122        """
123        Check if the syllable is heavy.
124
125        Returns:
126            True if the syllable is heavy, False otherwise.
127        """
128        return bool(self.has_consonant_ending or self.has_dipthong)

Check if the syllable is heavy.

Returns:

True if the syllable is heavy, False otherwise.

is_strong: Optional[bool]
130    @cached_property
131    def is_strong(self) -> Optional[bool]:
132        """
133        Check if the syllable is strong.
134
135        Returns:
136            True if the syllable is strong, False if weak, None if undetermined.
137        """
138        if not len(self.parent.children) > 1:
139            return None
140        if not self.is_stressed:
141            return False
142        if self.prev and not self.prev.is_stressed:
143            return True
144        if self.next and not self.next.is_stressed:
145            return True

Check if the syllable is strong.

Returns:

True if the syllable is strong, False if weak, None if undetermined.

is_weak: Optional[bool]
147    @cached_property
148    def is_weak(self) -> Optional[bool]:
149        """
150        Check if the syllable is weak.
151
152        Returns:
153            True if the syllable is weak, False if strong, None if undetermined.
154        """
155        if not len(self.parent.children) > 1:
156            return None
157        if self.is_stressed:
158            return False
159        if self.prev and self.prev.is_stressed:
160            return True
161        if self.next and self.next.is_stressed:
162            return True

Check if the syllable is weak.

Returns:

True if the syllable is weak, False if strong, None if undetermined.

164    @cached_property
165    def onset(self) -> PhonemeList:
166        """
167        Get the onset of the syllable.
168
169        Returns:
170            A PhonemeList containing the onset phonemes.
171        """
172        return PhonemeList(p for p in self.children if p.is_onset)

Get the onset of the syllable.

Returns:

A PhonemeList containing the onset phonemes.

174    @cached_property
175    def rime(self) -> PhonemeList:
176        """
177        Get the rime of the syllable.
178
179        Returns:
180            A PhonemeList containing the rime phonemes.
181        """
182        return PhonemeList(p for p in self.children if p.is_rime)

Get the rime of the syllable.

Returns:

A PhonemeList containing the rime phonemes.

184    @cached_property
185    def nucleus(self) -> PhonemeList:
186        """
187        Get the nucleus of the syllable.
188
189        Returns:
190            A PhonemeList containing the nucleus phonemes.
191        """
192        return PhonemeList(p for p in self.children if p.is_nucleus)

Get the nucleus of the syllable.

Returns:

A PhonemeList containing the nucleus phonemes.

194    @cached_property
195    def coda(self) -> PhonemeList:
196        """
197        Get the coda of the syllable.
198
199        Returns:
200            A PhonemeList containing the coda phonemes.
201        """
202        return PhonemeList(p for p in self.children if p.is_coda)

Get the coda of the syllable.

Returns:

A PhonemeList containing the coda phonemes.

@cache
def rime_distance(self, syllable: Syllable) -> float:
204    @cache
205    def rime_distance(self, syllable: 'Syllable') -> float:
206        """
207        Calculate the rime distance between this syllable and another.
208
209        Args:
210            syllable: The syllable to compare with.
211
212        Returns:
213            The rime distance as a float.
214        """
215        return self.wordform.rime_distance(syllable.wordform)

Calculate the rime distance between this syllable and another.

Arguments:
  • syllable: The syllable to compare with.
Returns:

The rime distance as a float.

class SyllableList(prosodic.ents.EntityList):
218class SyllableList(EntityList):
219    """A list of Syllable objects."""
220    pass

A list of Syllable objects.