-
Notifications
You must be signed in to change notification settings - Fork 0
/
genealogy.py
executable file
·180 lines (131 loc) · 3.69 KB
/
genealogy.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
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
#!/usr/bin/env python3
import random
import re
def prime_factors(n):
"""Returns all the prime factors of a positive integer"""
factors = []
d = 2
while n > 1:
while n % d == 0:
factors.append(d)
n //= d
d = d + 1
if d*d > n:
if n > 1: factors.append(n)
break
return factors
class Phrase:
def __init__(self, typ):
self.typ = typ
self.value = ""
class Male:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class Female:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class Begat:
def __init__(self, father, son):
self.father = father
self.son = son
def __str__(self):
return str(self.father) + " was the father of "+str(self.son)+",\n"
class Stanza:
def __init__(self, opening, begats, closing):
self.opening = opening
self.begats = begats
self.closing = closing
def __str__(self):
return str(self.opening)+''.join(str(begat) for begat in self.begats)+str(self.closing)
class Triple:
def __init__(self, opening, stanzas, closing):
self.opening = opening
self.stanzas = stanzas
self.closing = closing
def __str__(self):
return str(self.opening)+'\n'.join(str(s) for s in self.stanzas)+str(self.closing)
class Gean:
def __init__(self, males, females):
self.males = males
self.females = females
def random_triple(self, first):
closing = ""
s1 = self.random_stanza(first)
s2 = self.random_stanza(s1.begats[-1].son)
s3 = self.random_stanza(s2.begats[-1].son)
return Triple('',[s1,s2,s3],closing)
def random_stanza(self, father):
opening = ""
closing = ""
begats = []
for i in range(13):
b = self.random_begat(father)
begats.append(b)
father = b.son
return Stanza(opening, begats, closing)
def random_begat(self, father):
#father = self.random_male()
son = self.random_male()
return Begat(father, son)
def random_male(self):
return Male(random.choice(self.males))
def random_show(self):
s = self.random_triple('Aaban')
print(s)
class Analyse:
def __init__(self, triple):
self.triple = triple
def word_list(self):
s = str(self.triple)
re_word = re.compile(r'\w+')
ls = re_word.findall(s)
return ls
def letter_list(self):
s = str(self.triple)
re_word = re.compile(r'\w')
ls = re_word.findall(s)
return ls
def words(self):
return len(self.word_list())
def letters(self):
return len(self.letter_list())
def vowels(self):
count = 0
for v in 'aeiou':
count += self.letter_list().count(v)
#print(v,count)
return count
def consonants(self):
return self.letters() - self.vowels()
def vowel_start(self):
#print ([w for w in self.word_list() if w[0].lower() in 'aeiou'])
return len([w for w in self.word_list() if w[0].lower() in 'aeiou'])
def consonant_start(self):
#print ([w for w in self.word_list() if w[0].lower() not in 'aeiou'])
return len([w for w in self.word_list() if w[0].lower() not in 'aeiou'])
if __name__ == '__main__':
with open('male.txt') as fid:
males = [x.strip() for x in fid.readlines()]
with open('female.txt') as fid:
females = [x.strip() for x in fid.readlines()]
g = Gean(males, females)
t = g.random_triple('Aaban')
#g.random_show()
print(t)
a = Analyse(t)
def show_attr(attr):
val = getattr(a, attr)()
print(attr, val, prime_factors(val))
show_attr('words')
show_attr('letters')
show_attr('vowels')
show_attr('consonants')
show_attr('vowel_start')
show_attr('consonant_start')
#print ('words:',a.words(),prime_factors(a.words()))
#print('letters:',a.letters(),
#print('vowels:',a.vowels(), prime_factors(a.vowels()))