-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenderbias.js
141 lines (130 loc) · 4.62 KB
/
genderbias.js
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
// Code derived from Thomas Forth's original at http://tomforth.co.uk/genderbias/
var femaleWords,maleWords,examples = [];
function ready(fn){
// Version 1.1
if(document.readyState != 'loading') fn();
else document.addEventListener('DOMContentLoaded', fn);
};
ready(function(){
var dict = document.getElementById('dictionary');
var path = "";
if(dict){
path = dict.value;
dict.addEventListener('change',function(e){ getDictionary(e.target.value); });
}
getDictionary(path);
});
function getDictionary(path){
if(path){
fetch(path,{})
.then(response => { return response.json(); })
.then(json => {
femaleWords = json.female;
maleWords = json.male;
textChanged();
}).catch(error => {
console.error('Unable to load dictionary from '+path);
});
}else{
maleWords = [];
femaleWords = [];
}
}
function textChanged() {
document.getElementById("foundFemaleWords").innerHTML = "";
document.getElementById("foundMaleWords").innerHTML = "";
var letterText = document.getElementById("recommendationLetter").value;
var splitLetterText = letterText.split(" ");
var score = {'male':0,'female':0};
var words = {'male':{},'female':{}};
var html = {'male':'','female':''};
for (var i = 0; i < splitLetterText.length; i++) {
letterWord = splitLetterText[i];
letterWord = letterWord.replace(/\.$/g,'');
for (var maleCounter = 0; maleCounter < maleWords.length; maleCounter++) {
if(letterWord.toLowerCase().search(maleWords[maleCounter]) == 0) {
if(!words.male[letterWord]) words.male[letterWord] = 0;
words.male[letterWord]++;
score.male++;
}
}
for (var femaleCounter = 0; femaleCounter < femaleWords.length; femaleCounter++) {
if(letterWord.toLowerCase().search(femaleWords[femaleCounter]) == 0) {
if(!words.female[letterWord]) words.female[letterWord] = 0;
words.female[letterWord]++;
score.female++;
}
}
}
for(word in words.female) html.female += '<p>'+word+(words.female[word] > 1 ? ' ×'+words.female[word] : '')+'</p>';
document.getElementById("foundFemaleWords").innerHTML = html.female;
for(word in words.male) html.male += '<p>'+word+(words.male[word] > 1 ? ' ×'+words.male[word] : '')+'</p>';
document.getElementById("foundMaleWords").innerHTML = html.male;
function scoreIt(a,b){ return (100*(a-b)/(a+b)).toFixed(); }
var html = "";
if(score.male > score.female) html = "Male-biased ("+scoreIt(score.male,score.female)+"%)";
if(score.male < score.female) html = "Female-biased ("+scoreIt(score.female,score.male)+"%)";
if(score.male == score.female) html = "Neutral - Congratulations!";
document.getElementById('score').innerHTML = html;
}
function example() {
var v = "Melinda was one of the first users of my now widely-used and successful software, MetNetMaker. Her early bug reports and insightful suggestions were invaluable to making the product what it is today. I have not since worked with anyone so at ease communicating with those in other scientific fields.";
if(examples.length > 0){
var i = Math.floor(Math.random()*examples.length);
v = parsePage(examples[i]);
}
document.getElementById("recommendationLetter").value = v;
textChanged();
}
function parsePage(data){
if(typeof data==="string") data = data.split(/[\n\r]/);
// If it looks like YAML we remove it
start = 0;
if(data[0].indexOf('---')==0){
for(var i = 1; i < data.length; i++){
if(data[i].indexOf('---')==0){
start = i+1;
if(data[i+1]=="") start++;
continue;
}
}
}
var out = "";
for(var i = start; i < data.length; i++){
out += data[i]+'\n';
}
// Replace HTML
out = out.replace(/\<[^\>]+\>/g,'');
return out;
}
function getExamples(data){
// Split into an array by new line characters
if(typeof data==="string") data = data.split(/[\n\r]/);
var m,md,html,path;
// Define the callback function for success
var success = function(data,a){ if(data) examples.push(data); };
for(var i = 0; i < data.length ; i++){
m = data[i].match(/\* \[[^\]]+\]\(([^\)]+)\)/);
if(m){
md = m[1];
// We will try to load an HTML version first because gh-pages converts the Markdown files to HTML
loadFILE('examples/'+md,success,{alternate:'examples/'+md});
}
}
}
// Load the examples
loadFILE('examples/README.md',getExamples);
// Function to load a file (same domain)
function loadFILE(file,fn,attrs){
if(!attrs) attrs = {};
attrs['_file'] = file;
var error = "";
fetch(file,{})
.then(response => { return response.text(); })
.then(txt => {
if(typeof fn==="function") fn.call((attrs.context ? attrs.context : this),txt);
}).catch(error => {
console.error('Unable to load file from '+file);
if(attrs.alternate && attrs.alternate!=file) loadFILE(attrs.alternate,fn,attrs);
});
}