Skip to content

Instantly share code, notes, and snippets.

@PaladhiDinesh
Last active March 19, 2016 00:35
Show Gist options
  • Save PaladhiDinesh/c7ad7ffc4fb17f4f9411 to your computer and use it in GitHub Desktop.
Save PaladhiDinesh/c7ad7ffc4fb17f4f9411 to your computer and use it in GitHub Desktop.
Webscience Assignment Week-6 Q2

Take the Twitter graph you generated in question #1 and test for male-female homophily. For the purposes of this question you can consider the graph as undirected (i.e., no distinction between "follows" and "following"). Use the twitter name (not "screen name"; for example "Michael L. Nelson" and not "@phonedude_mln") and programatically determine if the user is male or female. Some sites that might be useful:

https://genderize.io/ https://pypi.python.org/pypi/gender-detector/0.0.4

Create a table of Twitter users and their likely gender. List any accounts that can't be determined and remove them from the graph.

Perform the homophily test as described in slides 11-15, Week 7.

Does your Twitter graph exhibit gender homophily?

{
"nodes": [
{"id": 0,"gender":"male","name": "dinesh"},
{"id": 1,"gender":"female","name": "erika siregar"},
{"id": 2,"gender":"male","name": "varun reddy dodda"},
{"id": 3,"gender":"female","name": "Naina Sai Tipparti"},
{"id": 4,"gender":"male","name": "vamshi"},
{"id": 5,"gender":"male","name": "Ravi Teja"},
{"id": 6,"gender":"female","name": "Shivani Bimavarapu"},
{"id": 7,"gender":"male","name": "manoj Kompalli"},
{"id": 8,"gender":"male","name": "Manish Kumar"},
{"id": 9,"gender":"male","name": "Kumaraswamy S"},
{"id": 10,"gender":"male","name": "Prashanth Pappu"},
{"id": 11,"gender":"male","name": "Uday kumar reddy cs"},
{"id": 12,"gender":"male","name": "ashwin srinivas"},
{"id": 13,"gender":"male","name": "Alok Rajasukumaran"},
{"id": 14,"gender":"male","name": "vamsi krishna"},
{"id": 15,"gender":"male","name": "Dinesh Kumar"},
{"id": 16,"gender":"male","name": "Joseph Udith Raj"},
{"id": 17,"gender":"male","name": "Mohan Chandran. P"},
{"id": 18,"gender":"female","name": "Vasavi MIC"},
{"id": 19,"gender":"male","name": "vineeth chandra"},
{"id": 20,"gender":"male","name": "harish kumar"},
{"id": 21,"gender":"male","name": "Sandeep Paladhi"},
{"id": 22,"gender":"female","name": "vani"},
{"id": 23,"gender":"male","name": "madhukar reddy"},
{"id": 24,"gender":"male","name": "Harish kumar"},
{"id": 25,"gender":"male","name": "bharath motha"},
{"id": 26,"gender":"male","name": "sunny"},
{"id": 27,"gender":"female","name": "Rajitha"},
{"id": 28,"gender":"male","name": "bhanu pradeep"},
{"id": 29,"gender":"male","name": "Akkineni Akshay"},
{"id": 30,"gender":"female","name": "Apoorva Dasari"},
{"id": 31,"gender":"female","name": "aruna"},
{"id": 32,"gender":"female","name": "sudha"},
{"id": 33,"gender":"male","name": "Sandeep"},
{"id": 34,"gender":"female","name": "Ramya Mattey"},
{"id": 35,"gender":"male","name": "vineeth"},
{"id": 36,"gender":"male","name": "Isaac Samuel"},
{"id": 37,"gender":"male","name": "santosh"},
{"id": 38,"gender":"female","name": "Addie Ayala"},
{"id": 39,"gender":"male","name": "Maruthi Sharma"},
{"id": 40,"gender":"male","name": "Abhijeet Sai Dasari"}
],
"links": [
{"source": 1, "target": 0},
{"source": 2, "target": 0},
{"source": 3, "target": 0},
{"source": 4, "target": 0},
{"source": 5, "target": 0},
{"source": 6, "target": 0},
{"source": 7, "target": 0},
{"source": 8, "target": 0},
{"source": 9, "target": 0},
{"source": 10, "target": 0},
{"source": 11, "target": 0},
{"source": 12, "target": 0},
{"source": 13, "target": 0},
{"source": 14, "target": 0},
{"source": 15, "target": 0},
{"source": 16, "target": 0},
{"source": 17, "target": 0},
{"source": 18, "target": 0},
{"source": 19, "target": 0},
{"source": 20, "target": 0},
{"source": 21, "target": 0},
{"source": 22, "target": 0},
{"source": 23, "target": 0},
{"source": 24, "target": 0},
{"source": 25, "target": 0},
{"source": 26, "target": 0},
{"source": 27, "target": 0},
{"source": 28, "target": 0},
{"source": 29, "target": 0},
{"source": 30, "target": 0},
{"source": 31, "target": 0},
{"source": 32, "target": 0},
{"source": 33, "target": 0},
{"source": 34, "target": 0},
{"source": 35, "target": 0},
{"source": 36, "target": 0},
{"source": 37, "target": 0},
{"source": 38, "target": 0},
{"source": 39, "target": 0},
{"source": 40, "target": 0},
{"source": 7, "target": 2},
{"source": 2, "target": 7},
{"source": 6, "target": 3},
{"source": 7, "target": 3},
{"source": 3, "target": 7},
{"source": 7, "target": 5},
{"source": 5, "target": 7},
{"source": 7, "target": 6},
{"source": 6, "target": 7},
{"source": 7, "target": 21},
{"source": 9, "target": 17},
{"source": 21, "target": 9},
{"source": 9, "target": 21},
{"source": 9, "target": 39},
{"source": 39, "target": 10},
{"source": 10, "target": 39},
{"source": 10, "target": 40},
{"source": 12, "target": 13},
{"source": 18, "target": 14},
{"source": 14, "target": 18},
{"source": 15, "target": 23},
{"source": 15, "target": 24},
{"source": 25, "target": 15},
{"source": 15, "target": 25},
{"source": 21, "target": 17},
{"source": 17, "target": 21},
{"source": 18, "target": 34},
{"source": 19, "target": 35},
{"source": 39, "target": 19},
{"source": 19, "target": 39},
{"source": 19, "target": 40},
{"source": 20, "target": 23},
{"source": 23, "target": 24},
{"source": 25, "target": 24},
{"source": 24, "target": 25},
{"source": 26, "target": 32},
{"source": 26, "target": 33},
{"source": 22, "target": 25}
]
}
import requests
import json
input= open('followers.json','r')
output=open('gender_data.json','w')
each_line=json.load(input)
dict={}
for i in range(1,61):
name= each_line["nodes"][i]["name"]
name=name.partition(" ")
fname=name[0]
#print fname
url="https://api.genderize.io/?name=" +fname
gen=requests.get(url)
# print type(gen)
dict['gen_data']=gen.content
#print gen_data
#print type(gen_data)
output.write(json.dumps(dict)+',\n')
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: 20px sans-serif;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 1000,
height = 1000
var color = d3.scale.category10();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(0.05)
.distance(325)
.charge(-100)
.size([width, height]);
d3.json("followers_gen.json", function(error, json) {
if (error) throw error;
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
// .style("marker-end", "url(#suit)") // Modified line ;
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 9)
.style("fill", function(d) { return color(d.gender); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
var setEvents = node
// Append hero text
.on( 'click', function (d) {
d3.select("h1").html(d.name);
d3.select("h2").html(d.screenName);
d3.select("h3").html ("Take me to " + "<a href='" + d.link + "' >" + d.name + " web page ⇢"+ "</a>" );
})
.on( 'mouseenter', function() {
// select element in current context
d3.select( this )
.transition()
.attr("x", function(d) { return -60;})
.attr("y", function(d) { return -60;})
.attr("height", 100)
.attr("width", 100);
})
// set back
.on( 'mouseleave', function() {
d3.select( this )
.transition()
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 50)
.attr("width", 50);
});
svg.append("defs").selectAll("marker")
.data(["suit", "licensing", "resolved"])
.enter().append("marker")
.attr("id", function(d) { return d; })
.attr("viewBox", "0 -5 10 10")
.attr("refX", 25)
.attr("refY", 0)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5 L10,0 L0, -5")
.style("stroke", "#4679BD")
.style("opacity", "0.6");
node.append("text")
.attr("dx", 20)
.attr("dy", ".35em")
.text(function(d) { return d.name });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment