xxxxxxxxxx
162
//NOTE:
/*names in chinese follow a specific order:
1. names are read left to right
2. the first character is the surname
3. the second and third character make up the "first name" or personal name
*/
//array to hold last character for male names
let last_char_m = [];
//array to hold middle character for male names
let middle_char_m = ['党', '国', '毛'];
//array to hold middle character for female names
let last_char_f = [];
//array to hold middle character for female names
let middle_char_f = ['党', '国', '毛'];
//array to hold first character (surname)
let first_char = [];
//var to decide the gender of name
let gender;
function preload() {
//preload all files
province_outfile = loadStrings('province_names.txt');
top_50_outfile = loadStrings('top50char.year.csv');
givenname_outfile = loadStrings('givenname.csv');
}
function setup() {
createCanvas(450, 600);
//DATA FOR FIRST CHARACTER (least personalized)
//get the first character of all cities in Guangdong province
for (let i = 0; i < province_outfile.length; i++) {
first_char.push(province_outfile[i][0]);
}
//DATA FOR MIDDLE CHARACTER (second-most personalized)
//get the top characters for male first names from 1950-2000
for (let i = 1; i < top_50_outfile.length; i++){
//read each row individually
current_row = split(top_50_outfile[i], ",");
//search through each row and only push the elements which correspond to names into the middle_char_m array
for (el = 0; el < current_row.length; el++) {
//indexes for male character
if ((el >= 13) && (el <=18)){
middle_char_m.push(current_row[el]);
}
//indexes for female character
if ((el >= 25) && (el <=30)) {
middle_char_f.push(current_row[el]);
}
}
}
//DATA FOR LAST CHARACTER (most personalized)
for (let i = 0; i < givenname_outfile.length; i++) {
//read each row
current_row = split(givenname_outfile[i], ",");
//determine whether it is male or female name based on number of occurence
if (current_row[3] > current_row[4]) {
last_char_m.push(current_row[0]); //push to male
}
else {
last_char_f.push(current_row[0]); //push to female
}
}
}
function draw() {
// background(241, 233, 212);
background(255, 255,255);
//frameRate(1);
//the font
noStroke();
textFont('Noto Serif SC');
textSize(20);
fill(0);
//drawing the grid on the paper
for (let i = 20; i < height; i +=20) {
stroke(0);
strokeWeight(0.2);
line(0, i, width, i);
}
for (let i = 90; i<width; i+=90) {
stroke(0);
strokeWeight(0.2);
line(i, 0, i, height);
}
let results = [];
for (let i = 0; i < 155; i++ ){
//decide gender of the name to be generated
gender = male_or_female();
//randomly get a surname
surname = first_char[int(random(first_char.length))];
//if name is female, pull characters from f arrays
if (gender == 'f') {
middle = middle_char_f[int(random(middle_char_f.length))];
last = last_char_f[int(random(last_char_f.length))];
personal_name = middle+last;
gender= '(女)';
}
//otherwise pull characters from m arrays
else {
middle = middle_char_m[int(random(middle_char_m.length))];
last = last_char_m[int(random(last_char_m.length))];
personal_name = middle+last;
gender = '(男)';
}
let full_name = surname+personal_name
results.push([full_name, gender]);
}
let count = 0;
for (let i = 15; i < width; i+=90){
for (let j = 15; j < height+20; j+=20) {
textFont('Noto Sans SC');
textSize(14);
fill(0);
noStroke();
text(results[count][0], i, j);
textSize(10);
fill(169, 0, 0);
text(results[count][1], i+45, j);
count++;
}
}
noLoop();
}
//function that determines the gender of the name
//based on generating random values between 0-1
//given that 60% of adoptees are female, returns 'female' if the number is between 0 and 0.6 and 'male' otherwise
function male_or_female() {
let num = random();
if (num <=0.6) {
return 'f';
}
else {
return 'm';
}
}