-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_combinations.py
96 lines (81 loc) · 3.11 KB
/
get_combinations.py
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
import re
import itertools
import math
def get_syllable_combinations(line_syllables=7, min_word_syllables=1, max_word_syllables=4):
def get_possibilities_for_word_length(word_syllables):
max_occurrences = 0
if word_syllables > line_syllables:
max_occurrences = 0
elif word_syllables == line_syllables:
max_occurrences = 1
else:
max_occurrences = math.floor(
line_syllables/word_syllables
)
return [n for n in range(0, max_occurrences+1)]
possibilities_for_words = []
for word_syllables in range(min_word_syllables, max_word_syllables+1):
possibilities_for_words.append(
get_possibilities_for_word_length(word_syllables)
)
valid_combinations = []
occurrences = []
combinations = [n for n in itertools.product(*possibilities_for_words)]
for combination in combinations:
total_syllables = 0
for i, occurrences_in_line in enumerate(combination):
word_syllables = i + min_word_syllables
total_syllables += word_syllables * occurrences_in_line
# print("word syllables "+str(word_syllables)+" occurrences"+str(occurrences_in_line))
# print("total syllables "+str(total_syllables))
if total_syllables == line_syllables:
valid_combinations.append(combination)
return valid_combinations
def get_orderings(combinations):
"""
combinations = {
2: 2,
3: 1
}
"""
line = []
for syllables, occurrences in combinations.items():
for i in range(occurrences):
line.append(syllables)
varied_lines = []
for n in itertools.permutations(line):
if n not in varied_lines:
varied_lines.append(n)
return varied_lines
def get_line_variations(line_syllables=7, min_word_syllables=1, max_word_syllables=4):
orderings = []
combinations = get_syllable_combinations(
line_syllables=line_syllables,
min_word_syllables=min_word_syllables,
max_word_syllables=max_word_syllables
)
for i, combination in enumerate(combinations):
combinations_dictionary = {}
for i, occurrences in enumerate(combination):
if occurrences == 0:
continue
combinations_dictionary[i+min_word_syllables] = occurrences
orderings += get_orderings(combinations_dictionary)
return orderings
def get_accentuated_line(line, words):
accentuated_words = []
line = re.sub('-', '', line)
counter = 0
for word_syllables in words:
accentuated_words.append(line[counter:counter+word_syllables])
counter += word_syllables
return accentuated_words
def get_accentuated_variations(line, min_word_syllables=2, max_word_syllables=5):
variations = get_line_variations(
line_syllables=len(re.sub('-', '', line)),
min_word_syllables=min_word_syllables,
max_word_syllables=max_word_syllables
)
return [get_accentuated_line(line, v) for v in variations]
if __name__ == "__main__":
print(get_accentuated_variations("10-10-10-1"))