|
1 | | -import random |
2 | 1 | import re |
3 | 2 | import unicodedata |
4 | | -from abc import abstractmethod |
5 | | -from typing import List, Optional, Protocol |
| 3 | +from typing import List, Optional |
6 | 4 |
|
7 | 5 | import music21 |
8 | 6 |
|
9 | | -from .phoneme import Phoneme |
10 | | - |
11 | | - |
12 | | -class Language(Protocol): |
13 | | - @abstractmethod |
14 | | - def __init__(self, part: music21.stream.Part): |
15 | | - pass |
16 | | - |
17 | | - @abstractmethod |
18 | | - def parse( |
19 | | - self, time: float, lyrics: Optional[str], index: int |
20 | | - ) -> Optional[List[Phoneme]]: |
21 | | - pass |
22 | | - |
23 | | - |
24 | | -class Random(Language): |
25 | | - """Generate random phonemes for parts without any lyrics.""" |
26 | | - |
27 | | - def __init__(self, part: music21.stream.Part, *, strict: bool = False): |
28 | | - """Initialize the language with the complete stream, so parsers |
29 | | - can look around and take decisions based on the surrounding lyrics. |
30 | | - """ |
31 | | - self.part, self.strict = part, strict |
32 | | - |
33 | | - def parse( |
34 | | - self, time: float, lyrics: Optional[str], index: int |
35 | | - ) -> Optional[List[Phoneme]]: |
36 | | - """Parse the given lyrics and return random vowel-consonant pairs. |
37 | | -
|
38 | | - >>> parse(None) |
39 | | - [<Phoneme.I: 8>, <Phoneme.C: 2>] |
40 | | - """ |
41 | | - if lyrics and self.strict: |
42 | | - raise ValueError("random language doesn't accept lyrics") |
43 | | - |
44 | | - random.seed(time) |
45 | | - while not (vowel := random.choice(Phoneme)).vowel(): |
46 | | - pass |
47 | | - while not (consonant := random.choice(Phoneme)).consonant(): |
48 | | - pass |
49 | | - |
50 | | - return [vowel, consonant] |
| 7 | +from ..phoneme import Phoneme |
| 8 | +from .language import Language |
51 | 9 |
|
52 | 10 |
|
53 | 11 | class Generic(Language): |
|
0 commit comments