-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_traveler_gender.py
49 lines (40 loc) · 1.92 KB
/
guess_traveler_gender.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""guess_traveler_gender.py - Looks at the travelers table and uses Wolfram Client Library to
execute code that guesses the traveler's gender from their name. The guess is put in the gender
column if it has null value. If the guess is indeterminate, it will not change the gender in the DB.
If the program spits out
Incompatible variable type (Text) and variable value (First[{}]).
{} has zero length and no first element.
over and over again, that's normal. Wolfram Language just wasn't able to detect a given name from
the name in the DB. If this happens, there is no guess and the traveler in the DB is left alone.
This requires Wolfram Client Library for Python and a Wolfram Kernel.
https://reference.wolfram.com/language/WolframClientForPython/
"""
import sqlite3
WOLFRAM_KERNEL_LOCATION = "/opt/Mathematica/SystemFiles/Kernel/Binaries/Linux-x86-64/WolframKernel"
DATABASE_FILE = "travelogues.sqlite3"
from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
session = WolframLanguageSession(WOLFRAM_KERNEL_LOCATION)
connection = sqlite3.connect(DATABASE_FILE)
db = connection.cursor()
db.execute("SELECT id, name, gender FROM travelers")
travelers = db.fetchall()
for traveler in travelers:
if traveler[2] == None: #traveler[2] is gender
name = traveler[1]
traveler_id = traveler[0]
# The following evaluates
# Classify["NameGender", First[TextCases[name, "GivenName"]]]
# in Wolfram Kernel
gender = session.evaluate(
wl.System.Classify("NameGender",
wl.System.First(wl.System.TextCases(name, "GivenName")))
)
if type(gender) is str and gender != "Indeterminate":
db.execute("""UPDATE travelers
SET gender = ?
WHERE id = ?""", (gender, traveler_id))
connection.commit()
session.stop()