V-Soft's Corporate Headquarters

101 Bullitt Lane, Suite #205
Louisville, KY 40222

TOLL FREE: 844.425.8425
FAX: 502.412.5869

Denver, Colorado

6400 South Fiddlers Green Circle Suite #1150
Greenwood Village, CO 80111

TOLL FREE: 844.425.8425

Chicago, Illinois

208 N. Green Street, #302, Chicago, IL 60607

TOLL FREE: 844.425.8425

Madison, Wisconsin

2810 Crossroads Drive, Ste. 4000
Madison, WI 53718

TOLL FREE: 844.425.8425

Atlanta, Georgia

1255 Peachtree Parkway Suite #4201
Cumming, GA 30041

TOLL FREE: 844.425.8425

Cincinnati, Ohio

Spectrum Office Tower 11260
Chester Road Suite 350
Cincinnati, OH 45246

Phone: 513.771.0050

Raritan, New Jersey

216 Route 206 Suite 22 Hillsborough Raritan, NJ 08844

Phone: 513.771.0050

Toronto, Canada

1 St. Clair Ave W Suite #902, Toronto, Ontario, M4V 1K6

Phone: 416.663.0900

Hyderabad, India

Incor 9, 3rd Floor, Kavuri Hills
Madhapur, Hyderabad – 500033 India

PHONE: 040-48482789

Noida, India

H-110 - Sector 63 ,
NOIDA , Gautham Budh Nagar ,
UP – 201301

How to Build a Knowledge Graph

Programmer Building knowledge graph for search

We use Google almost every day, be it in our work or personal lives. Have you ever wondered how the Google search process provides such accurate information from such a vast amount of data? Google, like most search engines, uses a sophisticated knowledge graph on the backend. Many of these advanced concepts are powered by knowledge graphs using artificial intelligence.

What is a Knowledge Graph?

Knowledge graphs are used to search, store and present fact-based data and are also used to power search engines, recommendations and chatbots. Knowledge graphs contain a head entity, relation and a tail entity, or in simpler terms: subject, relation and object.

Building a Knowledge Graph for Search Engines

This process has three steps: 
  1. Generate triples from relevant text
  2. Store triples in graph database
  3. Query the knowledge graph

Step 1: Generating Triples from Relevant Text

Part 1: Extract Entity 

Named entity extraction is a popular technique used in information extraction, which takes the entities from the text based on predefined classes. Different Named Entity Recognition (NER) systems generate entities from the given text.

  • Spacy NER, Stanford NER
  • Pretrained models like BERT, Elmo
  • Machine learning algorithms like conditional random fields
  • Deep learning algorithms like Bidirectional LSTM-CRF, LSTM-CNN

Part 2: Identify Relationship Between Entities

Once the entities are extracted from the text, we must find the relation between these entities, such as extracting semantic relations between two or more entities. For example, "The White House is in Washington D.C." Here, "The White House" is the head entity in the relation and "Washington D.C." is the tail entity. This can be represented as a triple (The White House, is in, Washington D.C.)

There are different methods for doing Relation Extraction (RE):

  • Rule-based RE
  • Supervised RE
  • Unsupervised RE

Alternatively, we can use the below-mentioned tools to extract triples from documents.

  1. Open source Python module Stanford-OpenIE
  2. IBM Watson NLU service 

How to Generate Triples Using the Stanford-OpenIE Python Module

  1. Since it is written in Java, make sure Java 1.8 is installed in your system  
  2. If OS is a Windows system, download and install the Java and set the path
  3. Install Stanford-OpenIE Python package using below command:
    • pip install stanford-openie

The OpenIE is supporting only 100,000 characters, so the length of the text must be below 100,000 characters. To execute, run the below code by passing input text and file name of csv to store triples:


Step 2: Storing Triples in Graph Database

Graph databases are designed to store nodes and their relations (edges). Graph databases give priority to relationships. Unlike other database management systems, in graph databases, connected data is equally important to individual data. 

Applications of Graph Databases:

  • Knowledge Graph
  • Recommendation Engine
  • Fraud Detection

Available Graph Databases:

  • ArangoDB
  • Neo4j
  • OrientDB
  • Amazon Neptune
  • FlockDB
  • DataStax
  • Cassandra
  • Cayley

Here's how to store generated triples in Neo4j. Neo4j is available in two editions.

  • Community edition: Designed for single-instance deployments
  • Enterprise edition: Included all the features of community edition and has extra features like clustering and online backup

Installation steps for community edition:

Once the installation is completed, neo4j can be accessible in the browser. The default username and password for the browser version is neo4j. Install py2neo package using the below command: pip install py2neo

Install py2neo package

Now give the output.csv (which is generated from the previous step) as input to the above code to store triples in neo4j database. You should now be able to visualize a knowledge graph in neo4j database.

Step 3: Querying the Knowledge Graph

You can start searching nodes in the neo4j web browser.

Sample query:

MATCH (n: Subject) RETURN n LIMIT 25

  • This query matches the nodes with label Subject and returns 25 results
  • You can select the label from Subject and Object and query the graph or you can access the neo4j graph with credentials using Python script

New call-to-action

About Author

Durga BhavaniDurga Bhavani works as a Data Scientist with V-Soft Digital. She has 4 years of experience as a Data Scientist and has extensive knowledge on predictive modelling, data processing, and data mining algorithms. She has developed applications in Deep Learning, Python, Machine Learning, and Natural Language Processing (NLP). She holds certifications in Natural Language Processing and Computer Vision. Out of her interest in higher mathematical concepts relating to AI, currently she is also pursuing Post Graduation program in Mathematics.


Topics: AI, Knowledge graph

Get Weekly Updates

Robotic Process Automation Guide