The content of this article comes from medium article
A Knowledge Graph understanding and implementation tutorial for beginners
What is knowledge map?
The content of knowledge map usually exists in the form of triple, subject predicate object (SPO).
Here's a chestnut:
Leonard Nimoy was an actor who played the character Spock in the science-fiction movie Star Trek
The above sentences can be extracted into the following triples:
In the form of knowledge map, it can be expressed as follows:
The graph composed of nodes and relationships above is a simple knowledge map.
How to build a simple knowledge map?
It can be divided into the following two steps:
- Knowledge extraction
- Information extraction, getting triples
- Entity recognition, entity linking, entity disambiguation, entity resolution
- Figure construction
Knowledge extraction step is the key to construct knowledge map, triples can be obtained by dependency analysis.
Building a simple knowledge map
Only code execution process and results are shown here. For complete code, please refer to github.
1. Triple extraction
inputText = 'Startup companies create jobs and innovation. Bill Gates supports entrepreneurship.' # Step 1: Knowledge Extraction. Output: SOP triples knowledgeExtractionObj = KnowledgeExtraction() sop_list = knowledgeExtractionObj.retrieveKnowledge(inputText) #list_sop = sop_list.as_doc() sop_list_strings =  for sop in sop_list: temp =  temp.append(sop.text) temp.append(sop.text) temp.append(sop.text) sop_list_strings.append(temp) print(sop_list_strings)
2. Entity link
# Step 2: Entity recognition and linking. This step needs to be linked. entityRecognitionLinkingObj = EntityRecognitionLinking() entityRelJson = entityRecognitionLinkingObj.entityRecogLink(inputText) entityLinkTriples =  for sop in sop_list_strings: tempTriple = ['', '', ''] for resource in entityRelJson['Resources']: if resource['@surfaceForm'] == sop: tempTriple = resource['@URI'] if resource['@surfaceForm'] == sop: tempTriple = resource['@URI'] if resource['@surfaceForm'] == sop: tempTriple = resource['@URI'] entityLinkTriples.append(tempTriple) print(entityLinkTriples)
3. Figure construction
# Step 3: Knowledge Graph creation. graphPopulationObj = GraphPopulation() graphPopulationObj = graphPopulationObj.popGraph( sop_list_strings, entityLinkTriples)
The final figure is as follows:
AuthError: The client is unauthorized due to authentication failure.
terms of settlement:
Ensure that the passwords are consistent with the settings when configuring the graph database (as shown in the following configuration, user: neo4j, password: neo4j)
config.DATABASE_URL = 'bolt://neo4j:neo4j@localhost:7687'#default
ServiceUnavailable: Failed to establish connection to (' 127.0.0.1 , 7687) (reason [winerror 10061] could not connect because the target computer actively refused. )
terms of settlement:
Make sure that neo4j is open before executing the diagram creation code
If you have any questions, please leave a message and communicate with us