-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperson.py
More file actions
174 lines (149 loc) · 4.55 KB
/
person.py
File metadata and controls
174 lines (149 loc) · 4.55 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
#-------------------------------------------------------------------------------
# Name: person
# Purpose:
#
# Author: Martin
#
# Created: 28/03/2014
# Copyright: (c) Martin 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import sys, random, csv
import main, tools
POP_SIZE = 200
nameMale = []
nameFemale = []
nameLast = []
popList = []
probMale = []
probFemale = []
probLast = []
raceList = ["CAUCASIAN", "BLACK", "HISPANIC", "ASIAN"]
ageWeights = {"children": 22, "youngAdults": 24, "adults": 42, "elders": 10,
"extraElderly": 2}
adults = []
elders = []
youngAdults =[]
children = []
extraElderly = []
def nameListGenerator():
# Create name lists
for line in open("data/male.first.txt"):
info = line.split()
nameMale.append(info[0])
probMale.append(float(info[1]))
for line in open("data/female.first.txt"):
info = line.split()
nameFemale.append(info[0])
probFemale.append(float(info[1]))
for line in open("data/last.txt"):
info = line.split()
nameLast.append(info[0])
probLast.append(float(info[1]))
class Person:
def __init__(self, first, last, age, sex, race):
self.first = first
self.last = last
self.name = first + " " + last
self.age = age
if sex == 0:
self.sex = "M"
else:
self.sex = "F"
self.race = race
#chooseInterests(self)
def chooseInterests(self):
interests = []
self.likes = []
self.dislikes = []
for line in open("data/interests.txt"):
line = line.rstrip()
interests.append(line)
for i in range(0,3):
# choose a like
choice = random.choice(interests)
self.likes.append(choice)
interests.remove(choice)
# choose a dislike
choice = random.choice(interests)
self.dislikes.append(choice)
interests.remove(choice)
def storkify(popCap = POP_SIZE):
global numMale
global numFemale
global ages
ages = [0,0,0,0]
numMale = 0
numFemale = 0
for i in range(0, popCap):
sex = random.randint(0,1)
# decide if male or female, pick first name
if sex == 0:
index = int(tools.weighted_choice_b2(probMale))
first = nameMale[index]
numMale += 1
elif sex == 1:
index = int(tools.weighted_choice_b2(probFemale))
first = nameFemale[index]
numFemale += 1
else:
sys.exit("he-she?")
# pick last name
index = int(tools.weighted_choice_b2(probLast))
last = nameLast[index]
age = tools.generate_age(ageWeights) # determine age
if age == "children":
age = random.randint(0, 15)
ages[0] += 1
elif age == "youngAdults":
age = random.randint(16, 29)
ages[1] += 1
elif age == "adults":
age = random.randint(30, 64)
ages[2] += 1
elif age == "elders":
age = random.randint(65, 79)
ages[3] += 1
else:
age = random.randint(80, 100)
ages[3] += 1
#age = str(age)
race = random.choice(raceList)
person = Person(first, last, age, sex, race)
popList.append(person)
#add personality
return popList
def census(sortby):
if sortby == 1:
popList.sort(key = lambda x: x.last)
elif sortby == 2:
popList.sort(key = lambda x: x.first)
elif sortby == 3:
popList.sort(key = lambda x: x.age)
elif sortby == 4:
popList.sort(key = lambda x: x.sex)
out = "data/census.csv"
data = ["Last, First, Sex, Age, Race".split(',')]
for each in popList:
info = "%s,%s,%s,%s,%s" % (each.last, each.first, each.sex, str(each.age), each.race)
info = info.split(",")
data.append(info)
tools.csv_writer(data, out)
def chooseInterests(likes, dislikes):
interests = []
for line in open("data/interests.txt"):
line = line.rstrip()
interests.append(line)
for i in range(0,3):
# choose a like
choice = random.choice(interests)
self.likes.append(choice)
interests.remove(choice)
# choose a dislike
choice = random.choice(interests)
self.dislikes.append(choice)
interests.remove(choice)
def person(sortby):
nameListGenerator()
storkify()
census(sortby)