-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathField_D_SupportingClasses.py
More file actions
1938 lines (1877 loc) · 96.6 KB
/
Field_D_SupportingClasses.py
File metadata and controls
1938 lines (1877 loc) · 96.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
####################################################################
# DF_SupportingClasses by Daniel Field #
# #
# This file contains the supporting classes for the SATB algorithm #
# check out www.github.com/dan-field/Text2SATB for info and rights #
####################################################################
class DF_Syllables:
def __init__(self):
"""Initialises a DF_Syllables object"""
self.vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
self.vowels_y = ["a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y"]
self.vowels_c_g_s = ["a", "c", "e", "g", "i", "o", "s", "u", "A", "C", "E", "G", "I", "O", "S", "U"]
self.suffixesOne = ["i", "y"]
self.suffixesTwo = []#"ac", "al", "an", "ar", "ee", "en", "er", "ic", "or"]
self.suffixesThree = ["ade", "age", "ant", "ard", "ary", "ate", "day", "dom", "dox", "eer", "ent", "ern", "ese",
"ess", "est", "ful", "gam", "gon", "ial", "ian", "iel", "ify", "ile", "ily", "ine", "ing", "ion", "ish", "ism", "ist",
"ite", "ity", "ive", "ise", "ize", "let", "log", "oid", "oma", "ory", "ous", "ure"]
self.suffixesFour = ["able", "ance", "bred", "cide", "crat", "cule", "emia", "ence", "ency", "etic", "ette", "gamy", "hood",
"ible", "ical", "ious", "itis", "less", "like", "ling", "ment", "ness", "onym", "opia", "opsy", "osis", "path",
"sect", "ship", "sion", "some", "tion", "tome", "tomy", "tude", "ular", "uous", "ward", "ware", "wise", "xion", "yond"]
self.suffixesFive = ["acity", "algia", "arian", "arium", "ation", "ative", "cracy", "cycle", "esque", "gonic", "guage", "iasis",
"ledge", "loger", "ocity", "ology", "otomy", "orium", "pathy", "phile", "phone", "phyte", "scopy", "scope", "sophy", "thing", "times", "tions", "ulous", "wards"]
self.suffixesSix = ["aholic", "ectomy", "iatric", "logist", "oholic", "ostomy", "phobia", "plegia", "plegic", "scribe",
"script", "sophic", "trophy"]
self.suffixesSeven = ["alogist", "escence", "isation", "ization", "ologist"]
self.prefixesTwo = ["de", "il", "im", "in", "ir", "re", "un", "up"]
self.le_exceptions = ["whole", "mobile", "pole", "male", "female", "hale", "pale", "tale", "sale", "aisle", "whale", "while", "bale"]
self.co_exceptions = ["cool", "coach", "coat", "coal", "could", "count", "coin", "coarse", "coup", "coif", "cook", "coign", "coiffe", "coof", "court"]
self.consonantGroups_two = ["bl", "br", "ch", "ck", "cl", "cr", "dr", "fl", "fr", "gh", "gl", "gr", "kn", "ng", "ph", "pl",
"pr", "sc", "sh", "sk", "sl", "sm", "sn", "sp", "st", "sw", "th", "tr", "tw", "wh", "wr"]
self.consonantGroups_three = ["n't", "nth", "sch", "scr", "shr", "spl", "spr", "squ", "str", "thr"]
def b(self, word):
if len(word) < 4:
return self.breakDownShortWord(word)
else:
start, rest = self.breakDownFromFront(word)
if len(rest) < 4:
adjusted_endLength = 0
else:
ending, endLength = self.searchSuffix(rest)
rest = rest[:len(rest)-endLength]
adjusted_ending = self.breakDownFromBack(rest, ending)
adjusted_endLength = 0
for syllable in adjusted_ending:
for letter in syllable:
adjusted_endLength += 1
if adjusted_endLength > endLength:
rest = rest[:len(rest)+endLength-adjusted_endLength]
restBrokenDown = self.r(rest)
if len(restBrokenDown) > 1: # make a late adjustment for 'ed' endings that don't sound separately
if restBrokenDown[-1] == "ed":
if restBrokenDown[-2][-1] not in ["c", "d", "i", "t"]:
restBrokenDown[-2] = restBrokenDown[-2]+restBrokenDown[-1]
del restBrokenDown[-1]
elif len(restBrokenDown[-1]) > 2:
if restBrokenDown[-1][-2] == "e" and restBrokenDown[-1][-1] == "d":
if restBrokenDown[-1][-3] not in ["c", "d", "i", "t"] and restBrokenDown[-1][0] not in self.vowels:
restBrokenDown[-2] = restBrokenDown[-2]+restBrokenDown[-1]
del restBrokenDown[-1]
# also, make a late adjustment for first two syllables that should actually be one (note: 'co' starts are not affected here if they are part of the prefix i.e. 'start')
if len(restBrokenDown[0]) == 2 and len(restBrokenDown[1]) == 2:
if restBrokenDown[0][0] not in self.vowels and restBrokenDown[0][1] in self.vowels_y:
if restBrokenDown[1][0] not in self.vowels and restBrokenDown[1][1] in ["e", "E"]:
restBrokenDown[0] = restBrokenDown[0]+restBrokenDown[1]
del restBrokenDown[1]
result = []
if start != "":
result.append(start)
if restBrokenDown != []:
for syllable in restBrokenDown:
result.append(syllable)
if adjusted_endLength > 0:
for syllable in adjusted_ending:
result.append(syllable)
return result
def breakDownShortWord(self, word):
# this function breaks an English word down into its syllables, starting at the end of the word
word = str(word)
w = str.lower(word)
wordLength = len(word)
if wordLength < 2: # either there is no word, or it's a single letter. No breakdown necessary
return [word]
elif wordLength == 2:
if w[0] not in self.vowels or w[1] not in self.vowels:
# there is at least one consonant in the two-letter word - it's a single syllable
return [word]
else:
# two vowels (unusual) - let's assume they'll be sung as two separate syllables
return [word[0], word[1]]
elif wordLength == 3:
if w[0] in self.vowels and w[1] not in self.vowels and w[2] in self.vowels_y:
# we have VOWEL-CONSONANT-VOWEL or VOWEL-CONS-y
if w[2] != "e":
# the last vowel is not 'e', so it's probably two syllables
return[word[0], word[1]+word[2]]
# in any other case, it's probably one syllable
return [word]
def breakDownFromBack(self, word, suffix):
# pass in the found suffix plus the preceding word elements
w = str.lower(word)
lengthRemaining = len(word)+len(suffix)
wordLength = lengthRemaining
suffixLength = len(suffix)
lengthRemaining -= suffixLength
if suffix != "":
suffix = self.breakDownSuffix(suffix)
if suffixLength == wordLength: # somehow we've got a whole word that matches a suffix; no need for further work
return suffix
elif suffixLength > wordLength-4: # there are only one or two letters before the suffix - let's deal with them now
if suffixLength == wordLength-1: # in fact there's only one letter before the suffix
if w[0] not in self.vowels: # that first letter is a consonant
suffix[0] = word[0]+suffix[0] # add that consonant on to the start of the first syllable
return suffix # and we're done
else: # we've got a leading vowel; best to treat it as its own syllable
adjusted_suffix = [word[0]] # this is a new list; the first element is the first letter of the word
for syllable in suffix:
adjusted_suffix.append(syllable) # now add the original syllable/s behind the initial vowel syllable
return adjusted_suffix # and we're done
elif suffixLength == wordLength-2: # there are actually two letters before the suffix; if they are both consonants, we tack them on; otherwise, they're a syllable
if w[0] not in self.vowels and w[1] not in self.vowels_y:
suffix[0] = word[0]+word[1]+suffix[0]
return suffix
else:
adjusted_suffix = [word[0]+word[1]]
for syllable in suffix:
adjusted_suffix.append(syllable)
return adjusted_suffix
else: # there are exactly three letters before the suffix; we can treat this like a 3-letter word
if w[0] in self.vowels and w[1] not in self.vowels and w[2] in self.vowels_y:
# we have VOWEL-CONSONANT-VOWEL or VOWEL-CONS-y
if w[2] != "e":
# the last vowel is not 'e', so it's probably two syllables
adjusted_suffix = [word[0], word[1]+word[2]]
for syllable in suffix:
adjusted_suffix.append(syllable)
return adjusted_suffix
# in any other case, it's probably one syllable
adjusted_suffix = [word[0]+word[1]+word[2]]
for syllable in suffix:
adjusted_suffix.append(syllable)
return adjusted_suffix
else: # we have found a suffix and there are more than three letters before it
pass
# now check for 'e' endings (on the whole word, or the word with suffix removed, as applicable)
if w[-1] == "e": # last letter is 'e'
if w[-2] not in self.vowels and w[-3] in self.vowels_y: # second-last is consonant and third-last is vowel
ending = word[-3]+word[-2]+word[-1] # this is a Vowel-Consonant-e ending
if w[-4] not in self.vowels and len(w) == 4: # it's cons-vowel-cons-'e' so it's a single syllable (note the 'le' ending has already been dealt with in the 'searchSuffix' function)
if suffix == "":
return [word]
else:
adjusted_word = [word[:(lengthRemaining)]]
for syllable in suffix:
adjusted_word.append(syllable)
return adjusted_word
else:
return suffix
else:
return suffix
else:
return suffix
def breakDownFromFront(self, word):
word = str(word)
w = str.lower(word)
start = ""
if len(w) > 1:
if w[:2] == "mc":
start = word[0]+word[1]
if len(w) > 2:
if w[:2] == "bi" and w[2] in self.vowels:
start = word[0]+word[1]
if len(w) > 3:
if w[:3] == "tri" and w[3] in self.vowels:
start = word[0]+word[1]+word[2]
if w[:3] == "pre" and w[3] in self.vowels:
start = word[0]+word[1]+word[2]
if len(w) > 5:
if w[:2] == "co" and w[2] in self.vowels and w[:6] not in self.co_exceptions and w[:5] not in self.co_exceptions and w[:4] not in self.co_exceptions:
start = word[0]+word[1]
elif len(w) > 4:
if w[:2] == "co" and w[2] in self.vowels and w[:5] not in self.co_exceptions and w[:4] not in self.co_exceptions:
start = word[0]+word[1]
elif len(w) > 3:
if w[:2] == "co" and w[2] in self.vowels and w[:4] not in self.co_exceptions:
start = word[0]+word[1]
if start != "": # a start has been found
if len(start) == 2 and len(w) > 2:
rest = ""+word[2:len(w)]
elif len(start) == 3 and len(w) > 3:
rest = ""+word[3:len(w)]
else:
rest = ""
else:
rest = word
return start, rest
def r(self, word):
if word == "":
return []
breakdown, tracker = self.regularBreakDown(word)
result = []
skipNext = False
for index, element in enumerate(breakdown):
if index == 0: # this is the beginning of the word
result.append(element)
if len(breakdown) > 1:
if tracker[0] == "c" and tracker[1] == "v":
result[0] = result[0]+breakdown[1]
skipNext = True
elif index == len(breakdown)-1: # there are at least two elements in the word and this is the last one
# in general, we want to join a consonant group unless it's "n't" or starts with an apostrophe
# if it's a vowel group then we want to sound it separately
# also, if it's an unsounded "e" we might need to modify the previous group/s slightly
if tracker[index] == "c" and str.lower(element) != "n't" and element[0] != "'":
result[-1] = result[-1]+element # join this final consonant group onto the end of the last syllable
break
else: # check if it's an unsounded e -- adjust for 'skipNext' dynamic
if str.lower(element) == "e":
if skipNext is True: # will be true if the preceding group was a consonant; if it's false then it's preceded by a vowel group so it's sounded
if len(result[-1]) > 2: # the current last syllable has at least three letters
if result[-1][-2] not in self.vowels and result[-1][-3] in self.vowels:
# we have a final e preceded by something that ends in vowel-consonant
# the 'e' is already in place and it's already a single syllable, there's nothing more to do
# Note: this shouldn't really happen; if 'skipNext' is true then the preceding syllable should NOT have a vowel group at the start
break
if len(result) > 1: # there are at least two syllables already in the result
if len(result[-1]) == 2 and result[-1][0] not in self.vowels and result[-2][-1] in self.vowels:
# we have a final 'e' preceded by a single consonant preceded by a vowel
result[-2] = result[-2]+result[-1] # join the last 'xe' syllable on to the preceding vowel-ending syllable
del result[-1] # remove the 'xe' syllable
break
elif len(result[-1]) == 2 and result[-1][0] in ["c", "g", "s"]:
# most likely the final e is modifying the consonant, and is not a separate syllable
result[-2] = result[-2]+result[-1] # join the last 'xe' syllable on to the preceding vowel-ending syllable
del result[-1] # remove the 'xe' syllable
break
# we arrive here if we have not hit a 'break' statement above; that is, none of the above conditions applied
# so we have n't, 'xyz or a vowel group that's not an unsounded e
if skipNext is False:
result.append(element) # add it as a separate syllable
else: # there's at least one element before and after this one
if tracker[index] == "c" and tracker[index+1] == "v":
result.append(element+breakdown[index+1])
skipNext = True
elif tracker[index] == "c" and tracker[index+1] == "c":
result[-1] = result[-1]+element
skipNext = False
elif tracker[index] == "v":
if skipNext is False:
result.append(element)
else:
skipNext = False
# check for an unsounded e followed by an s - unless the e is preceded by a c, g or s
if str.lower(element) == "e" and str.lower(breakdown[index+1]) == "s" and len(result) > 1: # mostly copied from the 'final e' check above
if len(result[-1]) == 2 and result[-1][0] not in self.vowels_c_g_s and result[-2][-1] in self.vowels_y:
# we have a standalone 'e' preceded by a single consonant preceded by a vowel
result[-2] = result[-2]+result[-1] # join the last 'xe' syllable on to the preceding vowel-ending syllable
del result[-1] # remove the 'xe' syllable
return result
def regularBreakDown(self, word):
w = str.lower(word)
syllableEstimate = 0
thisGroupLength = 0
wordBreakdown = []
VCTracker = []
# look for consonant and vowel groups
for index, letter in enumerate(w):
if index == 0: # first letter
if letter in self.vowels: # the word starts with a vowel (excluding 'y')
syllableEstimate += 1
thisGroupLength += 1
v_c = "v"
else: # we're treating an initial 'y' as a consonant
v_c = "c"
thisGroupLength += 1
else: # every other letter
if letter in self.vowels_y: # from here on, 'y' is treated like a vowel
if v_c[-1] != "v": # the preceding letter was not a vowel
syllableEstimate += 1
grouping = self.checkConsonantGroup(word[index-thisGroupLength:index])
for group in grouping:
wordBreakdown.append(group)
VCTracker.append("c")
thisGroupLength = 1
else: # the preceding letter was also a vowel
thisGroupLength += 1
v_c = v_c+"v"
else: # this letter is a consonant
if v_c[-1] != "c": # the preceding letter was not a consonant
grouping = self.checkVowelGroup(word[index-thisGroupLength:index])
for group in grouping:
wordBreakdown.append(group)
VCTracker.append("v")
thisGroupLength = 1
else: # the preceding letter was also a consonant
thisGroupLength += 1
v_c = v_c+"c"
# the last vowel/consonant grouping has not been dealt with yet (because you have to get 'past' the end of it before it's dealt with)
# so now we have to handle that final group
if index == len(w)-1: # this is the last letter
if v_c[-1] == "v": # the final group is a vowel group
grouping = self.checkVowelGroup(word[-thisGroupLength:])
for group in grouping:
wordBreakdown.append(group)
VCTracker.append("v")
else: # the final group is a consonant group
grouping = self.checkConsonantGroup(word[-thisGroupLength:])
for group in grouping:
wordBreakdown.append(group)
VCTracker.append("c")
return wordBreakdown, VCTracker
def checkVowelGroup(self, group): # for simplicity, we'll say every vowel group is one syllable unless it starts with an 'i' (other than 'ie'); then it's two
if len(group) == 1:
return [group]
elif len(group) == 2:
if group[0] in ["i", "I"] and group[1] not in ["e", "E"]:
return [group[0], group[1]]
else:
return [group]
elif group[0] in ["i", "I"]:
return [group[0], group[1:]]
else:
return [group]
def checkConsonantGroup(self, group):
g = str.lower(group)
if len(g) == 1:
return [group]
elif len(g) == 2:
if g in self.consonantGroups_two:
return [group]
elif g[0] == g[1]: # double letter
return [group]
else:
return [group[0], group[1]]
elif len(g) == 3:
if g in self.consonantGroups_three:
return [group]
elif g[0]+g[1] in self.consonantGroups_two:
return [group[0]+group[1], group[2]]
elif g[1]+g[2] in self.consonantGroups_two:
return [group[0], group[1]+group[2]]
else: # no consonant groups found
return [group[0], group[1]+group[2]]
elif len(g) > 3:
if g[-3]+g[-2]+g[-1] in self.consonantGroups_three:
return [group[:-3], group[-3:]]
elif g[0]+g[1]+g[2] in self.consonantGroups_three:
return [group[:3], group[3:]]
elif g[-2]+g[-1] in self.consonantGroups_two:
return [group[:-2], group[-2:]]
elif g[0]+g[1] in self.consonantGroups_two:
return [group[:2], group[2:]]
else: # no consonant groups found
return [group[:2], group[2:]]
def searchSuffix(self, word):
# conducts a search for common suffixes
word = str(word)
w = str.lower(word)
wordLength = len(word)
lastTwo = w[-2]+w[-1]
lastThree = w[-3]+lastTwo
lastFour = w[-4]+lastThree
if wordLength > 4:
lastFive = w[-5]+lastFour
if wordLength > 5:
lastSix = w[-6]+lastFive
if wordLength > 6:
lastSeven = w[-7]+lastSix
if lastSeven in self.suffixesSeven:
return word[-7:], 7
if lastSix in self.suffixesSix:
return word[-6:], 6
if lastFive in self.suffixesFive:
return word[-5:], 5
if lastFour in self.suffixesFour:
return word[-4:], 4
elif lastThree in self.suffixesThree:
return word[-3:], 3
elif lastTwo in self.suffixesTwo:
return word[-2:], 2
elif w[-1] == 'y' and w[-2] not in self.vowels: # for example, by, ly, ty, etc
return word[-2:], 2
elif w[-1] == 'e' and w[-2] == 'l' and w not in self.le_exceptions: # 'le' ending other than the specified exceptions
return word[-2:], 2
else:
return "", 0
def breakDownSuffix(self, S):
suffix = str.lower(S)
# manually break down the items in the suffix list
# 1. Single-syllable suffixes
if len(suffix) < 3 or suffix in ["bred", "day", "dom", "dox", "ful", "gam", "gon", "let", "log", "cide", "crat", "cule", "guage", "hood",
"ledge", "less", "like", "ling", "ment", "ness", "path", "sect", "ship", "some", "tome", "tude", "ward", "ware", "wise",
"phile", "phone", "phyte", "scope", "wards", "scribe", "script", "thing", "yond", "ade", "age", "ant", "ard", "ate", "eer", "ent",
"ern", "ese", "ess", "est", "ile", "ine", "ing", "ish", "ism", "ist", "ite", "ive", "ise", "ize", "oid", "ous",
"ure", "ance", "ence", "ette", "esque", "sion", "tion", "times", "tions", "xion"]:
return [S]
# 2. Other suffixes
elif suffix in ["ency", "gamy", "opsy", "tomy"]: # 2,2
return [S[-4]+S[-3], S[-2]+S[-1]]
elif suffix in ["cycle", "loger", "pathy", "sophy"]: # 2,3
return [S[-5]+S[-4], S[-3]+S[-2]+S[-1]]
elif suffix in ["cracy", "gonic", "scopy"]: # 3,2
return [S[-5]+S[-4]+S[-3], S[-2]+S[-1]]
elif suffix in ["emia", "opia"]: # 1,2,1
return [S[-4], S[-3]+S[-2], S[-1]]
elif suffix in ["algia"]: # 2,2,1
return [S[-5]+S[-4], S[-3]+S[-2], S[-1]]
elif suffix in ["acity", "arian", "arium", "ocity", "ology", "otomy", "orium"]: # 1,2,2
return [S[-5], S[-4]+S[-3], S[-2]+S[-1]]
elif suffix in ["iasis"]: # 1,1,3
return [S[-5], S[-4], S[-3]+S[-2]+S[-1]]
elif suffix in ["aholic", "oholic"]: # 1,2,3
return [S[-6], S[-5]+S[-4], S[-3]+S[-2]+S[-1]]
elif suffix in ["phobia", "plegia"]: # 3,2,1
return [S[-6]+S[-5]+S[-4], S[-3]+S[-2], S[-1]]
elif suffix in ["ostomy"]: # 1,3,2
return [S[-6], S[-5]+S[-4]+S[-3], S[-2]+S[-1]]
elif suffix in ["iatric"]: # 1,1,4
return [S[-6], S[-5], S[-4]+S[-3]+S[-2]+S[-1]]
elif suffix in ["plegic", "trophy"]: # 3,3
return [S[-6]+S[-5]+S[-4], S[-3]+S[-2]+S[-1]]
elif suffix in ["logist", "sophic"]: # 2,4
return [S[-6]+S[-5], S[-4]+S[-3]+S[-2]+S[-1]]
elif suffix in ["ectomy"]: # 2,2,2
return [S[-6]+S[-5], S[-4]+S[-3], S[-2]+S[-1]]
elif suffix in ["alogist", "isation", "ization", "ologist"]: # 1,2,4
return [S[-7], S[-6]+S[-5], S[-4]+S[-3]+S[-2]+S[-1]]
else:
remainder = ""
for i, letter in enumerate(S):
if i != 0:
remainder = remainder+letter
return [S[0], remainder]
class DF_TextInput:
def __init__(self):
"""Initialises a DF_TextInput object"""
self.S = DF_Syllables()
self.verses = []
self.positions = []
self.scrabbleScores = []
self.verseCount = 0
self.lineCount = 0
self.lastWord = False
lineText = []
inputText = []
infile = None
usrFileName = raw_input("please enter the filename of the text input file, e.g. 'mypoem.txt'\n: ")
try:
infile = open(usrFileName, "r")
except (OSError, IOError):
infile = None
print "\nThere was an issue attempting to open the file."
print "Please double-check your filename; "+usrFileName+"\n"
print "Note your input text file must be in the same"
print "folder as the Python files."
return
if infile is not None:
for line in infile.readlines():
inputText.append(line)
infile.close()
# The input file has been read, and all of the text is in 'inputText'
lineText.append("---BREAK---") # this will be used to signify a new verse
noted = True # this is a flag to avoid multiple 'new verse' indications in a row
for line in inputText:
line = line.strip() # removes the 'newline' characters from the ends of the lines
if line == "" and noted is False: # empty line - used to signify a new verse - and a new verse has not yet been noted
lineText.append("---BREAK---") # insert a marker for a new verse
noted = True # change the flag (just in case there are a few blank lines in a row)
elif line != "" and noted is True: # the 'already noted' flag is set, but it's not a new line;
noted = False # reset the flag ready for the next verse
breakpoints = [0] # start a list of the locations of punctuation breaks
for index, character in enumerate(line):
if character in ".,:;!?-":
breakpoints.append(index+1) # note the positions of any breaking punctuation marks
elif index == len(line)-1:
breakpoints.append(index+1) # also note a breakpoint at the end of the line, if not already noted due to punctuation
for breakpoint in breakpoints:
if breakpoint != 0: # for all breakpoints except the zero point,
lineText.append(line[previousBreakPoint:breakpoint]) # add the text from the previous point to this one to lineText
previousBreakPoint = breakpoint
if lineText[-1] == "---BREAK---": # this is a new verse marker at the end, due to empty lines at the end
del lineText[-1] # but it's the end; there is no new verse, so we delete the marker
for line in lineText:
if line == "---BREAK---": # new verse
self.verseCount += 1 # add one to the verse count
self.verses.append([]) # create a new empty list within 'verses'
self.positions.append([]) # create a new empty list within 'positions'
self.scrabbleScores.append([]) # create a new empty list within 'scrabbleScores'
self.lastWord = False # reset the lastWord flag
self.lineCount = 1 # restart the line count
self.verses[self.verseCount-1].append([]) # create a new empty sub-list within the new verse list
self.positions[self.verseCount-1].append([]) # create a new empty sub-list within the new positions list
self.scrabbleScores[self.verseCount-1].append([]) # create a new empty sub-list within the new scrabbleScores list
else:
if line[-1] not in ".,;:!?-": # if there's a line that doesn't end in punctuation,
line = line+"*" # add an asterisk. Note all the lines will get blended together so the line breaks will be lost unless they're marked
for word in line.split(): # this splits the line into words (using 'space' as the delimiter)
if self.lastWord is True: # the previous word was the last word of its line
self.lineCount += 1 # increment the line counter
self.verses[self.verseCount-1].append([]) # create a new line sublist within the verse list
self.positions[self.verseCount-1].append([]) # create a new line sublist within the position list
self.scrabbleScores[self.verseCount-1].append([]) # create a new line sublist within the scrabbleScores list
self.lastWord = False # reset the lastWord flag
scrabbleScore = self.scoreScrabble(word) # add up the scrabble score for the word using the scoreScrabble function (below)
punctuated = False
punctuation = ""
if word[-1] in ".,;:!?-*": # if there's a punctuation mark (noting that it will appear at the word end, thanks to the previous breakdown)
punctuation = word[-1] # store the punctuation mark separately
punctuated = True # set a flag to note that this has happened
word = word[:len(word)-1] # remove the punctuation mark from the end of the word
syllables = self.S.b(word) # send the word to the syllable breakdown function, get back a list of syllables
if punctuated is True: # this is the flag that was set earlier
if punctuation != "*": # if it's anything other than an asterisk (which is just a temporary marker, not actually needed in the text)
syllables[-1] = syllables[-1]+punctuation # add the punctuation back on where it came from
if punctuation in ".;:!?*": # these punctuation marks will create new lines
self.lastWord = True # this will be the last word in this line
positions = range(len(syllables)) # start an 'empty' list with as many members as there are syllables in the word
if len(positions) == 1: # this means there's only one syllable in the word
positions[0] = "single" # this follows the MusicXML 'syllabic' convention
else: # there are two or more syllables in the word
for index, position in enumerate(positions):
if index == 0: # first syllable
positions[index] = "begin"
elif index == len(positions)-1: # last syllable
positions[index] = "end"
else: # between first and last
positions[index] = "middle"
for element in syllables:
self.verses[self.verseCount-1][self.lineCount-1].append(element)
self.scrabbleScores[self.verseCount-1][self.lineCount-1].append(scrabbleScore) # list the word's Scrabble score at each syllable position
for element in positions:
self.positions[self.verseCount-1][self.lineCount-1].append(element)
def provideVerses(self):
return self.verses
def providePositions(self):
return self.positions
def provideScrabbleScores(self):
return self.scrabbleScores
def provideTitle(self):
usrTitle = raw_input("please enter a Title for the work\n: ")
if usrTitle == "":
usrTitle = None
return usrTitle
def provideLyricist(self):
usrLyricist = raw_input("please enter the Lyricist's name\n: ")
if usrLyricist == "":
usrLyricist = None
return usrLyricist
def scoreScrabble(self, string):
# adds up the Scrabble score of the string
runningTotal = 0
for letter in str.lower(string):
if letter in 'aeioulnrst':
runningTotal += 1
elif letter in 'dg':
runningTotal += 2
elif letter in 'bcmp':
runningTotal += 3
elif letter in 'fhvwy':
runningTotal += 4
elif letter in 'k':
runningTotal += 5
elif letter in 'jx':
runningTotal += 8
elif letter in 'qz':
runningTotal += 10
return runningTotal
class DF_SongPlanner:
def __init__(self, verses, positions, scores):
"""Initialises a DF_SongPlanner object"""
self.homeKey = 8 # MIDI number 8 = 'A flat' (or G sharp)
self.verses = verses
self.positions = positions
self.scores = scores
self.durations, self.sylRests = self.holdPlanner()
self.measureNo = 0
self.voice = 1 # 1 Soprano, 2 Alto, 3 Tenor, 4 Bass
self.numberOfVerses = len(self.verses)
self.versesLinesSyllables = self.getLinesAndSyllables(self.verses)
self.chordPlan, self.keysOfVerses = self.planHarmonicStructure()
self.chordBase, self.chordType = self.determineChordSequence()
self.rangeSop = [60, 80]
self.rangeAlto = [55, 76]
self.rangeTenor = [47, 67]
self.rangeBass = [42, 64]
self.bassNotes = []
self.bassRhythms = []
self.bassWords = []
self.bassPositions = []
self.bassTies = []
self.tenNotes = []
self.tenRhythms = []
self.tenWords = []
self.tenPositions = []
self.tenTies = []
self.altoNotes = []
self.altoRhythms = []
self.altoWords = []
self.altoPositions = []
self.altoTies = []
self.sopNotes = []
self.sopRhythms = []
self.sopWords = []
self.sopPositions = []
self.sopTies = []
self.sopLatestNote = 72
def planHarmonicStructure(self):
# looks at the incoming verse structure and generates a chord sequence
# Try to keep the chord references numeric/relative for easy adaptation
key = 0 # number zero will represent the 'home' key
verseKey = 0
verseKeys = []
lineKey = 0
chords = [] # start an empty list to hold the chords
for index, verse in enumerate(self.verses):
chords.append([]) # start an empty sub-list for each verse
if index == 0: # first verse
verseKey = 0 # start in the 'home' key
elif index == len(self.verses)-1: # last verse
verseKey = 0 # last verse also in the 'home' key
else: # for the middle verses, key change is based on number of syllables in the verse
verseSyllables = 0
for number in self.versesLinesSyllables[index]:
verseSyllables += number # add up the number of syllables in the verse
keyshift = self.keyShift(int(verseSyllables/20)%12) # this shifts the key by one step for every 20 syllables in the verse
# but if there are more than 240 syllables, it goes back to zero and starts over again
verseKey += keyshift
verseKeys.append(verseKey)
for i, line in enumerate(verse): # similarly, we might change key from line to line using the same sort of logic
if i == 0: # first line
lineKey = verseKey # first line of each verse is in the verse's key
elif index == len(self.verses)-1 and i == len(self.verses[index])-1: # this is the last line of the last verse
lineKey = verseKey # last line of the song should be in the home key
else:
lineSyllables = self.versesLinesSyllables[index][i] # this is the previously-calculated number of syllables in the line
keyshift = self.keyShift(int(lineSyllables/12)%12) # shift key one 'step' for every 12 syllables in the line
lineKey += keyshift
chords[index].append(lineKey)
return chords, verseKeys
def getVerseKeys(self):
return self.keysOfVerses
def determineChordSequence(self):
# sets a chord sequence based on the scrabble scores and the pre-determined harmonic structure
chordBase = []
chordType = []
for index, verse in enumerate(self.verses):
chordBase.append([])
chordType.append([])
for i, line in enumerate(verse):
chordBase[index].append([])
chordType[index].append([])
for j, syllable in enumerate(line):
cycleMove = 0
chordComplexity = 0
# we want to set a chord degree and type that reflects the Scrabble score of the word
# There are 7 degrees in a cycle: 4, 7, 3, 6, 2, 5, 1
# There are 4 types: base, base with extensions, alterations level 1, alterations level 2
# Without triples, doubles and bonuses, a Scrabble score of about 30 is quite high.
if self.scores[index][i][j] > 31:
cycleMove = 6
chordComplexity = 3
else:
cycleMove = int(self.scores[index][i][j]/6)%7 # moves one step for every 6 points
chordComplexity = int(self.scores[index][i][j]/8)%4 # one degree of complexity for every 8 points
chordBase[index][i].append(cycleMove)
chordType[index][i].append(chordComplexity)
# now convert the 'cycle moves' to cycle numbers
for index, verse in enumerate(chordBase): # we want to end each line on its 'zero' position
for i, line in enumerate(verse):
totalMove = 0
for move in line:
totalMove += move # work out the total cycle movement in the line
start = totalMove%7 # start the line with that amount of shift (%7 to go back to the start of the cycle if necessary)
last = start # we want to go 'backwards' by the starting amount, to end up in the right place at the finish
for j, move in enumerate(line):
chordBase[index][i][j] = (last-move)%7 # work out which degree we are on, at each given syllable
last = last-move
return chordBase, chordType
def holdPlanner(self):
# determines how many beats each syllable should be given, in accordance with the word's Scrabble score
# as well as the amenity of the vowel sound to being held
sylLengths = []
sylRest = []
for index, verse in enumerate(self.verses):
sylLengths.append([])
sylRest.append([])
beatCount = 0
for i, line in enumerate(verse):
sylLengths[index].append([])
sylRest[index].append([])
for j, syllable in enumerate(line):
thisLength = 4 # default length will be one crotchet
# we will set the length in accordance with the Scrabble score and the vowel sound
score = self.scores[index][i][j]
position = self.positions[index][i][j]
if score < 5 and str.lower(syllable) not in ["a", "i", "o", "u"]: # short common words excluding nice vowels
if beatCount%4 != 0: # we're off the beat
thisLength = 2 # quaver
else: # we're on the beat
thisLength = 4 # crotchet
else:
thisLength = 2 + 2*int(score/6) # eighth plus an extra eighth for every 6 points in the word
# but we want to modify the lengths to suit good vowel sounds
if position != "single": # this is a multisyllabic word
if str.lower(syllable[0]) in 'aou' or str.lower(syllable[-1]) in 'aou': # looks like we might have a nice vowel sound
thisLength += 4 # extend the length
consInARow = 0
consonanty = False
for letter in syllable:
if str.lower(letter) not in 'aeiouy':
consInARow +=1
if consInARow > 2: # we have at least three consonants in a row; probably better to sing this a little shorter
consonanty = True
else:
consInARow = 0
if consonanty is True and thisLength >= 4:
thisLength -= 2
if thisLength < 2:
thisLength = 2 # in case it somehow ends up as zero
if i == len(verse)-1 and j == len(line)-1: # last syllable in a line
remainingBeatsInBar = 16 - beatCount
if remainingBeatsInBar > 7:
thisLength = remainingBeatsInBar
else:
thisLength = remainingBeatsInBar+8
# now we know how long we want this syllable to last, the next thing is to check if we'll go over the barline
remainingBeatsInBar = 16 - beatCount
if thisLength < remainingBeatsInBar:
sylLengths[index][i].append([int(thisLength)])
sylRest[index][i].append([False])
beatCount += thisLength
elif thisLength == remainingBeatsInBar:
sylLengths[index][i].append([int(thisLength)])
sylRest[index][i].append([False])
beatCount = 0
else: # note will go over the next barline
leftoverLength = thisLength - remainingBeatsInBar
if leftoverLength > 16: # the note wants to tie over more than one bar - that's very long
leftoverLength = 16 # limit it to just the next bar.
if leftoverLength < 16: # the note will go into the next bar, but not beyond
sylLengths[index][i].append([int(remainingBeatsInBar), int(leftoverLength)])
sylRest[index][i].append([False, False])
beatCount = leftoverLength
else: # the note will go exactly to the end of the next bar
sylLengths[index][i].append([int(remainingBeatsInBar), 16])
sylRest[index][i].append([False, False])
beatCount = 0
if beatCount%16 != 0: # we're at the end of the text line but we're not at the end of a bar
L = int(4-(beatCount%4)) # counts to next crotchet
if L > 0 and L < 4:
sylLengths[index][i].append([L])
sylRest[index][i].append([True])
beatCount += L
if beatCount == 16:
beatCount = 0
self.verses[index][i].append("")
self.positions[index][i].append(None)
self.scores[index][i].append(0)
if beatCount%16 != 0: # still not at the end of the bar, but at least we know there's a whole number of crotchet beats to go
L = int(16-(beatCount%16))
if L > 0:
sylLengths[index][i].append([L])
sylRest[index][i].append([True])
beatCount = 0
self.verses[index][i].append("")
self.positions[index][i].append(None)
self.scores[index][i].append(0)
return sylLengths, sylRest
def getBassPart(self, homeKey):
bassNotes = []
bassRhythms = []
bassWords = []
bassPositions = []
bassTies = []
count = 0
for index, verse in enumerate(self.verses):
bassNotes.append([])
bassRhythms.append([])
bassWords.append([])
bassPositions.append([])
bassTies.append([])
bassNotes[index].append([])
bassRhythms[index].append([])
bassWords[index].append([])
bassPositions[index].append([])
bassTies[index].append([])
count = 0
barNo = -1
for i, line in enumerate(verse):
for j, syllable in enumerate(line):
keyRef = self.chordPlan[index][i]
chordDegree = self.chordBase[index][i][j] # this is just the step number - it needs to be converted to a chromatic degree number
chordDegree = self.convertChordDegree(chordDegree)
fifth = self.getFifth(chordDegree)
chordType = self.chordType[index][i][j]
if chordType in [0, 1, 2]:
bassRef = [homeKey+keyRef+chordDegree]
else: # complex chord: sing the 5th in the bass
bassRef = [homeKey+keyRef+fifth]
if i == len(verse)-1 and j == len(line)-1: # last syllable in the line
bassRef = [homeKey+keyRef+chordDegree]
bassTargets = self.buildFullRange(bassRef, self.rangeBass[0], self.rangeBass[1])
loose_target = 52.4 # seek out a comfortable mid-point in the bass range
differences = [abs(loose_target - float(note)) for note in bassTargets]
closest = differences.index(min(differences))
thisNote = bassTargets[closest] # pick the bass note that is closest to the comfortable mid point
thisDuration = 4
# we have all the info we need to start assembling the Bass part
if count%16 == 0: # the previous bar is full
bassNotes[index].append([])
bassRhythms[index].append([])
bassWords[index].append([])
bassPositions[index].append([])
bassTies[index].append([])
barNo += 1
bassNotes[index][barNo].append(thisNote)
bassWords[index][barNo].append(self.verses[index][i][j])
bassPositions[index][barNo].append(self.positions[index][i][j])
if len(self.durations[index][i][j]) == 1: # the note does not cross a barline
L = self.durations[index][i][j][0]
R = self.sylRests[index][i][j][0]
bassRhythms[index][barNo].append(L)
bassTies[index][barNo].append(None)
count += L
# now check if it was supposed to be a rest
if self.sylRests[index][i][j][0] is True:
bassNotes[index][barNo][-1] = "RRR"
else: # we are crossing a barline
L = self.durations[index][i][j][0]
bassRhythms[index][barNo].append(L)
bassTies[index][barNo].append("start")
count += L
# add a new bar
bassNotes[index].append([])
bassRhythms[index].append([])
bassWords[index].append([])
bassPositions[index].append([])
bassTies[index].append([])
barNo += 1
bassNotes[index][barNo].append(thisNote)
bassWords[index][barNo].append(None)
bassPositions[index][barNo].append(None)
L = self.durations[index][i][j][1]
bassRhythms[index][barNo].append(L)
bassTies[index][barNo].append("stop")
count += L
self.bassNotes = bassNotes
self.bassRhythms = bassRhythms
self.bassWords = bassWords
self.bassPositions = bassPositions
self.bassTies = bassTies
def getTenorPart(self, homeKey):
tenNotes = []
tenRhythms = []
tenWords = []
tenPositions = []
tenTies = []
count = 0
for index, verse in enumerate(self.verses):
tenNotes.append([])
tenRhythms.append([])
tenWords.append([])
tenPositions.append([])
tenTies.append([])
tenNotes[index].append([])
tenRhythms[index].append([])
tenWords[index].append([])
tenPositions[index].append([])
tenTies[index].append([])
count = 0
barNo = -1
for i, line in enumerate(verse):
for j, syllable in enumerate(line):
keyRef = self.chordPlan[index][i]
chordDegree = self.chordBase[index][i][j] # this is just the step number - it needs to be converted to a chromatic degree number
chordDegree = self.convertChordDegree(chordDegree)
third = self.getThird(chordDegree)
fifth = self.getFifth(chordDegree)
seventh = self.getSeventh(chordDegree)
chordType = self.chordType[index][i][j]
if chordType in [0, 1]:
tenRef = [homeKey+keyRef+chordDegree, homeKey+keyRef+fifth]
else:
tenRef = [homeKey+keyRef+third, homeKey+keyRef+seventh]
if i == len(verse)-1 and j == len(line)-1: # last syllable in the line
tenRef = [homeKey+keyRef+chordDegree, homeKey+keyRef+third, homeKey+keyRef+fifth]
tenTargets = self.buildFullRange(tenRef, self.rangeTenor[0], self.rangeTenor[1])
loose_target = 57.4 # seek out a comfortable mid-point in the tenor range
differences = [abs(loose_target - float(note)) for note in tenTargets]
closest = differences.index(min(differences))
thisNote = tenTargets[closest] # pick the tenor note that is closest to the comfortable mid point
thisDuration = 4
# we have all the info we need to start assembling the Tenor part
if count%16 == 0: # the previous bar is full
tenNotes[index].append([])
tenRhythms[index].append([])
tenWords[index].append([])
tenPositions[index].append([])
tenTies[index].append([])
barNo += 1
if self.sylRests[index][i][j][0] is False:
tenNotes[index][barNo].append(thisNote)
else:
tenNotes[index][barNo].append("RRR")
tenWords[index][barNo].append(self.verses[index][i][j])
tenPositions[index][barNo].append(self.positions[index][i][j])
# add up the total length of this note (since it might be in two parts - if it crosses a barline)
noteTotalLength = 0
trill = False
for partNote in self.durations[index][i][j]:
noteTotalLength += int(partNote)
if noteTotalLength >= 8 and self.sylRests[index][i][j][0] is False and len(tenWords[index][barNo][-1]) < 3:
# the total period of the hold is at least a minim, it's not a rest, and the syllable is short
trill = True # set the flag to say this note should be trilled
if len(self.durations[index][i][j]) == 1: # the note does not cross a barline
L = self.durations[index][i][j][0]
if trill is False:
tenRhythms[index][barNo].append(L)
tenTies[index][barNo].append(None)
count += L
else: # trill is True.
# First, assemble the scale to trill on
if chordType in [2, 3]:
scale = self.getMajorisedScale(chordDegree)
else:
scale = self.getMajorModeScale(chordDegree)
shiftedScale = []
for note in scale:
shiftedScale.append(homeKey+keyRef+note)
scaleTargets = self.buildFullRange(shiftedScale, self.rangeTenor[0], self.rangeTenor[1])
differences = [abs(thisNote - float(note)) for note in scaleTargets]
closest = differences.index(min(differences))
if closest < len(scaleTargets)-2: # there're at least two 'higher' scale note in the range
trillNote = scaleTargets[closest+len(tenWords[index][barNo][-1])] # the next or uber-next note in the list, according to the number of letters in the syllable
else: # we're already on the range top; trill 'down' instead
trillNote = scaleTargets[closest-1]
tenNotes[index][barNo].append(trillNote)
tenNotes[index][barNo].append(thisNote)
tenNotes[index][barNo].append(trillNote)
# add all the rest of the additional list items
tenRhythms[index][barNo].append(2)
tenRhythms[index][barNo].append(2)
tenRhythms[index][barNo].append(2)
tenRhythms[index][barNo].append(2)
tenTies[index][barNo].append(None)
tenTies[index][barNo].append(None)
tenTies[index][barNo].append(None)
tenTies[index][barNo].append(None)
tenWords[index][barNo].append(None) # just three of these because one was already written, above
tenWords[index][barNo].append(None)
tenWords[index][barNo].append(None)
tenPositions[index][barNo].append(None) # similarly, just three of these
tenPositions[index][barNo].append(None)
tenPositions[index][barNo].append(None)
if L > 8:
tenNotes[index][barNo].append(thisNote)
tenRhythms[index][barNo].append(L-8)
tenTies[index][barNo].append(None)
tenWords[index][barNo].append(None)
tenPositions[index][barNo].append(None)
count += L
else: # we are crossing a barline
L = self.durations[index][i][j][0]
tenRhythms[index][barNo].append(L)
tenTies[index][barNo].append("start")
count += L
# add a new bar
tenNotes[index].append([])
tenRhythms[index].append([])
tenWords[index].append([])
tenPositions[index].append([])
tenTies[index].append([])
barNo += 1
tenNotes[index][barNo].append(thisNote)
tenWords[index][barNo].append(None)