(Json40r5) dependency for json
handling.) Download and Install yEd Graph Editor
so you can display the .graphml files the
demo creates. For the Demo to run, Please make certain you have Microsoft .Net 4 Runtime
. Open CNAPINetDemo\CNAPINetDemo\CNAPINetDemo.sln after exctracting the source
UPDATE 2014-03-28: Please note that as of March 28th 2014, ConceptNet5
API is now version 5.2 upgraded from 5.1; "ConceptNet 5.2 presents the same API as 5.1. Because we host only one version of the knowledge base at a time, all URLs with /5.1/ now redirect to the equivalent /5.2/ URLs." - Official Doc.
ConceptNet¹ is a commonsense knowledgebase,
composed mainly from the Open Mind Project, written and maintained by Massachusetts
Institute of Technology (MIT). This library is something I threw together to communicate
with ConceptNet 5 (beta at the time of this writing). It uses a Representational
state transfer (REST) API, like ConceptNet 4, but has a different structure and
design. ConceptNet 5 "is a graph; To be precise, it's a hypergraph, meaning
it has edges about edges. Each statement in ConceptNet has justfications pointing
to it, explaining where it comes from and how reliable the information seems to
The .NET C# ConceptNet API Class Library (cn5apinet)
cn5apinet can provide an interface to ConceptNet 5 REST API. Use the API to receive back Lists
and objects of a
ConceptNet 5 hypergraph. What is new from cnapinet (from ConceptNet4) is a new namespace for auto-creating
GraphML xml files. This name space is quickly explained below. The demo displays
how to assemble the
ConceptNet 5.1 API has three methods to retrieve its edge data: Lookup, Search,
and Association. Like the ConceptNet documentation states, "Lookup is for when
you know the URI of an object in ConceptNet, and want to see a list of edges that
include it. Search finds a list of edges that match certain criteria. Association
is for finding concepts similar to a particular concept or a list of concepts."
cn5apinet now has three functions to perform these methods:
public Lookup Lookup(LANG language, String concept_name)
public Search Search(LANG language, String rel, String start, String end, String
text, int limit)
public Association Associations(LANG language, String word, String filter, int limit,
bool multipleterms, int weight)
[Please make certain you have Microsoft .Net 4 Runtime
The demo app has three to represent the three different query methods:"Lookup", "Search/Test Commonsense" and "Association". "Lookup" performs a basic query with one word or phrase; after the initial lookup, it will populate the dropdownbox with relationships that were found in the query. You may then press the 'lookup relationship' button to just display the graph with that selected relationship.
Next is the "Search/Test Commonsense" tab which allows you to query
the ConceptNet 5 (CN5) hypergraph's knowledge and results in an answer of "Yes",
"No", "Maybe"; it basically tells you if it thinks your statement
is true, false, or perhaps true. Pressing the 'Ask' button doesn't really perform the Search function but is just in the demo to display a practical use of CN5 and was left in from the original demo. This is based off the scores in the CN5 hypergraph [0 <= is false, 1 = maybe, 2 > is true]. For example, a score of 42.39 has a higher confidence score than a score of 3.87. Pressing the 'Search and open Graph!' button actually performs a CN5.1 Search method.
The third tab "Association", performs an Association lookup which is for finding concepts similar to a particular concept or a list of concepts.
Download and Install yEd Graph Editor so you can display the .graphml files the
demo creates. This tab was mainly based off of ConceptNet5's Web API documentation. Sometimes, screenshots
are the best to explain things so please take a look below.
cn5apinet.dll to your project 'References' via Solution Explorer.
cn5apinet.dll v1.6.x.x make sure that
(Json40r5) resides in the same folder as
- Add 'using cn5apinet;', etc to your source code to use the
- Create the main API engine with 'ConceptNetAPI CNClient
= new ConceptNetAPI();', etc.
- Below are the methods in ConceptNetAPI
Performing the three lookups:
public Lookup Lookup(LANG language, String concept_name)......
public Search Search(LANG language, String rel, String start, String end, String text, int limit)...
public Association Associations(LANG language, String word, String filter, int limit, bool multipleterms, int weight)...
Creating the .graphml
List<cn5apinet.GraphML.GraphMLNode> nodeList = new List<cn5apinet.GraphML.GraphMLNode>();
List<cn5apinet.GraphML.GraphMLEdge> edgeList = new List<cn5apinet.GraphML.GraphMLEdge>();
List<edge_arg> edge_argList = new List<edge_arg>();
GraphMLNode node0 = new GraphMLNode(0, new Geometry(30.0, 50.0, 1, 1)); node0.Text = tbGetKey.Text.Trim();
node0.FillColor = "#00FF00"; nodeList.Add(node0);
int i = 1;
if ((List<Object>)raw_concept.incoming_edges != null)
foreach (object o in (List<Object>)raw_concept.incoming_edges)
string s = o.GetType().Name;
if (o.GetType().Name == "edge_arg")
Dictionary<String, String> startDict = new Dictionary<string, string>();
String spiltstart = Convert.ToString(((edge_arg)o).start).Split(',');
GraphMLNode nodeX = new GraphMLNode(i, new Geometry(30.0, 50.0, 1, 0));
string nodetext = spiltstart.Split(seperatorsForwardSlash, StringSplitOptions.RemoveEmptyEntries);
nodeX.Text = nodetext[nodetext.Length - 1];
string edgetext = spiltstart.Split(seperatorsForwardSlash, StringSplitOptions.RemoveEmptyEntries);
GraphMLEdge edge0 = new GraphMLEdge(i, node0.NodeID, nodeX.NodeID, edgetext[edgetext.Length - 1] + " " + Convert.ToString(((edge_arg)o).score));
i = i + 1;
int numofNodes = nodeList.Count;
int g = 1;
double radius = 100.0;
if (numofNodes > 10)
radius = (nodeList.Geometry.height * 0.45) * numofNodes / 2;
for (double h = 0.0; h < 360.0; h += (360 / numofNodes))
if (g >= numofNodes)
double angle = h * System.Math.PI / 180;
if (cBoxQChildren.Checked == true && g < 4)
nodeList[g].Geometry.x = (int)(nodeList.Geometry.x + radius * System.Math.Cos(angle)) * 2;
nodeList[g].Geometry.y = (int)(nodeList.Geometry.y + radius * System.Math.Sin(angle)) * 2;
nodeList[g].Geometry.x = (int)(nodeList.Geometry.y + radius * System.Math.Cos(angle));
nodeList[g].Geometry.y = (int)(nodeList.Geometry.y + radius * System.Math.Sin(angle));
g = g + 1;
graphFile = new cn5apinet.GraphML.GraphMLFile(Application.StartupPath + "//" + node0.Text + ".graphml");
graphFile.Create(nodeList, edgeList, Application.ProductName);
ConceptNet REST API
Continues to evolve and having a .Net interface is nice to explore ConceptNet with.
It isn't the most efficient way to perform lookups [if you explore the ConceptNet5
website you can find alternative methods to perform queries (MongoDB 2.0 database
and 24GB file of the json, etc)] but it is handy to code some proof of concept apps
or just fiddling around in .Net learning about ConceptNet.
- The ConceptNet Web API, Massachusetts Institute of Technology [^]
- Newtonsoft.Json.dll, Json.NET Copyright (c) 2007 James Newton-King [^]
- Open Mind Common Sense Project [^]
CNAPINetDemo v18.104.22.168 Microsoft Visual C# Express 2010 project
cn5apinet.dll v22.214.171.124 and necessary files.
CNAPINetDemo v126.96.36.199 Microsoft Visual C# Express 2010 project
files with Json40r5 dll so manual download and reference add is no longer needed.
- Misc article image tweaks.
- Posted ConceptNet API 5.1 note that cn5apinet is broken; working on update.
- Updated article to support ConceptNet API 5.1. Uploaded
CNAPINetDemo v188.8.131.52. Uploaded
cn5apinet.dll v184.108.40.206 and necessary files.
- Added ConceptNet API upgrade note from 5.1 to 5.2 per email list and documentation.
- Tested demo with new API version.