a) First of all, I created a function to import the content of the senteces.txt file into memory or my notebook.
This functon reads the content in the file specified and returns it.
b) Also, A function was created to extract all the entities in pasages.
This function loops through each sentence and then extracts the entities and also calls the extract relations fuction to extract the relations in each sentence and return them accordingly. The entities are paired together but the relatons are seperated.
I also created a fuction to extract all relationships from the sentences.This function works be first looking for the ROOT dependency. If the sentence has a ROOT dependency, it will go on to check if the ROOT is followed by an dobj. it will check till all the conditions in the specified rule are met. But if the matcher does not find the root dependency, it will go on to check whether the next item in the rule is available until all the conditions are checked. The image below shows the code snippet of the extract relationship function.
c) I also created a function that takes the extrated entities and relationships as its parameters and then converts them into a datafram of three columns ['source','target','edge']. The source stores the subject and the target stores all the objects while the edge stores all the edges.
This method first extracts the entities and relations into a dictionary and the used pandas.Dataframe to convert the generated dictionary in to a dataframe. The image below shows how the dataframe looks like after it was generated.
a) I made use of the NetworkX and matplotlib libray in generating the Knowlegde Grapth. The NetworkX library was used to draw and configure the Knowlegde graph. It was used to load the dataframe and then converted into the graph. matplotlib was then used to visualize the graph. The image below gives a snippet of the code used to perform this action
The image below shows how the Knowledge graph looked like after it was generated.