-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_feature.py
78 lines (71 loc) · 2.02 KB
/
get_feature.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
# name
# name length
# country's numerical code
# namsor
# genderComputer
import pycountry
import os
import sys
from genderComputer.genderComputer import GenderComputer
from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy.orm import sessionmaker
def parse_feature(uids, output_f_name):
# features:
# name
# name length
# country's numerical code
# namsor
# genderComputer
# uid
pswd = os.environ["SQLPW"]
url = "mysql://sophie:"+pswd+"@localhost/namsor?charset=utf8mb4"
engine = create_engine(url)
Session = sessionmaker(bind = engine)
metadata = MetaData(engine)
ght_namsor = Table("ght_namsor_s", metadata, autoload=True)
conn = engine.connect()
session = Session()
dataPath = os.path.dirname(os.path.abspath(__file__))
gc = GenderComputer(os.path.join(dataPath, 'genderComputer/nameLists'))
output_f = open(output_f_name, "w")
for uid in uids:
r = session.query(ght_namsor).filter(ght_namsor.c.id == uid).first()
if r is None:
continue
firstName = r.firstName
namsor = r.genderScale
country_2 = r.country
if country_2 is not None and country_2 != "null":
cnty_p = pycountry.countries.get(alpha_2=country_2)
cnty = cnty_p.name
cnty_code = cnty_p.numeric
else:
cnty = ""
# name
output_f.write(firstName.encode("utf-8"))
features = ","
# name length
features += (str(len(firstName)) + ",")
# country code
features += (str(cnty_code) + ",")
# namsor
features += (str(namsor) + ",")
# genderComputer
try:
genderC = gc.resolveGender(unicode(firstName), cnty)#.decode('utf-8'), cnty)
except:
genderC = None
if genderC is None:
genderCint = 0
elif genderC == "mostly male":
genderCint = -0.8
elif genderC == "male":
genderCint = -1
elif genderC == "mostly female":
genderCint = 0.8
elif genderC == "female":
genderCint = 1
else:
genderCint = 0
features += (str(genderCint) + "," + str(uid))
output_f.write(features+"\n")