Creating Knowledge Graphs from Resumes and Traversing them | by Priya Dwivedi | Towards Data Science

Knowledge graphs are gaining popularity as a data structure for storing unstructured data. In this blog, we show how key elements of resumes can be stored and visualized as knowledge graphs. We then walk through the knowledge graph of resumes to answer questions. Our code is available on this Github link.
A Knowledge Graph is a type of graph which enables us to model knowledge of a particular domain by organizing it in an ontology through data interlinking. Machine learning can then be applied on a knowledge graph to get insights. Knowledge Graphs have all the major components of a graph — nodes, edges and their respective attributes.
Let’s look at the definitions of a node and an edge first:
A node is a point in the graph at which lines intersect or branch out.
An edge is a line joining two nodes in a graph.
As seen in the knowledge graph below, Spock, Star Trek, Star Wars, Leonard Nimoy, Science Fiction, Obi-Wan Kenobi, Alec Guinness are nodes and played, characterIn, starredIn and genre are edges of the graph.
The representation above is an example of a Knowledge Graph which stores information of movies and their characters along with the actors who played these characters. Storing information in this form enables us to model complicated pieces of knowledge of particular domains so that we can access specific information when needed through a structured traversal of the given graph.
Information to be structured in knowledge graphs are modeled in the form of triplets. In the triplets, typically there are two entities and a relation which is there between the entities. In the above graph, for example, played(Spock, Leonard Nimoy) is a triplet where the entities are Spock and Leonard Nimoy and the relation is played.
In this blog, we will be creating a Knowledge graph of people and the programming skills they mention on their resume. The idea is to extract skills from the resume and model it in a graph format, so that it becomes easier to navigate and extract specific information from.
For the purpose of this blog, we will be using 3 dummy resumes.
So, let’s dive into code now! The full code and the resumes are shared on my Github repo here.
For the purpose of simplification, we will just be focusing on programming languages that the different candidates know.
First, we extract the programming languages mentioned in the resume using regex. The code for this is shared below.
The output from this is the languages from the 3 programmers:
Output:['JavaScript', 'HTML5', 'PHP OOP', 'CSS', 'SQL', 'MySQL']['Python,R,CSS,PHP,Machine Learning']['HTML5', 'CSS', 'SQL', 'Jquery’, ‘PHP']
Here, for the sake of simplification, we assume that the name of the file is the candidate name and that the programming languages line is of the same format.
Now, once we have these, we will construct a knowledge graph from this information using the networkx library. NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. The library contains a wide range of functions to create different types of graphs, traverse them and perform a range of manipulations and calculations on top of them.
In our case, we would be using the from_dict_of_lists() function using which we will create a directed graph from a dictionary containing the names as the keys, and the corresponding programming languages as the values.
First, let’s create individual graphs for each candidate just for our understanding:
Here, in the fourth line, we are creating a networkx object from the dictionary that we created earlier. The create_using parameter here specifies the type of graph we need to create.
The different types of graphs and their respective networkx classes are specified in the networkx documentation as follows:
We will be using the MultiDiGraph since we need to represent more than one resume in a single graph and need to show directed edges for a clearer representation.
Next, we will use matplotlib to create a diagram and draw the graphs using networkx. Before drawing the knowledge graph, we have to first specify the layout of the graph. The networkx library provides about 7 layouts using which you can draw your graph depending on your use case. You can explore these layouts here.
The above code will create a graph for Mathew and his skills as shown below:
Similarly, we can create knowledge graph for the other 2 candidates.
Now, let’s try and visualize the graphs as one connected graph:
Here, we use the draw() function to draw the graph using the specified parameters:
With_labels — Using this flag, one can specify whether to show labels in the graphs or not.
Node_color — Using this parameter, the color of the nodes can be specified.
Edge_cmap -Using this parameter, the color map of the graph can be specified.
Pos -Using this parameter, the layout of the graph can be specified.
Node_size — Using this parameter, the size of the nodes can be specified.
Font_size — Using this parameter, we define the font size of the text used on the nodes.
The resultant graph will look like this:
As you can see in the above graph, we have now created a knowledge graph with each candidate and the programming languages they know.
Now that we have the knowledge graph ready, we can use this graph to extract information using graph based operations. There is a wide range of mathematical graph operations that can be used to extract data from knowledge graphs.
Traversing a knowledge graph can be tedious if code is written from scratch. To simplify this, the best practice is to store the graph in a graph database like Neo4j and write queries in SPARQL. For the purpose of this blog, we will showcase some simple extraction techniques using the networkx graph we created in Figure 4 above.
Suppose you are a recruiter of a company and you have received these resumes and have created the knowledge graph above. Now, you want to know the most popular programming language candidates have.
Let’s try it out!
In graph terms, we can convert the problem of getting the most popular programming language, to a node with most number of edges pointing to it. Think about it, the more the number of edges connected to a node, the more the number of candidates who know the skill. So we just have to extract the skill node with the maximum number of edges connected to it. That makes it easy!
Let’s see how we can achieve this using the networkx library:
Output:'CSS'3
Here, we are just using one function from the networkx library called degree(). This function returns the number of edges connected to a particular node. In the function defined above get_max_degree_node(), we are iterating through all skill nodes(all nodes except the name nodes) and getting the maximum degree in the skill nodes.
Similarly, we can use the same function to get the candidate who knows the most number of programming languages, instead of passing the name list, we pass the skill list, which has to be removed from all nodes.
skill_list = languages_mathew+languages_john+languages_maxmax_languages_degree, max_languages_node = get_max_degree_node(skill_list,G)print(max_languages_node)print(max_languages_degree)Output:Mathew Elliot6
These were a couple of examples of how we can use basic code to traverse the knowledge graph and extract information from it. Like mentioned earlier, for more advanced queries, it is recommended to store the knowledge graph in a graph database and then use querying languages such as SPARQL to query.
In the blog, we explain what knowledge graphs are, how they can be constructed and traversed. Knowledge graphs have a wide range of application ranging from conversational agents, search engines, question answering systems etc. They are ideal in a system where a large amount of interconnected data needs to be stored. Google, IBM, Facebook and other technology companies heavily use knowledge graphs in their systems to tie information together and traverse it very efficiently.
At Deep Learning Analytics, we are extremely passionate about using Machine Learning to solve real-world problems. We have helped many businesses deploy innovative AI-based solutions. Contact us through our website here if you see an opportunity to collaborate. This blog was written by Priya Dwivedi and Faizan Khan.
NetworkX Library — https://networkx.github.io/
Intro to Knowledge Graphs — https://towardsdatascience.com/knowledge-graph-bb78055a7884