(Note:
cn5apinet.dll has
Newtonsoft.Json.dll (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
Introduction
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 be."
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
cn5apinet.GraphML.GraphMLFile
cn5apinet Namespace
The Demo
[Please make certain you have
Microsoft .Net 4 Runtime]
The demo app has two tabs "Test Commonsense" and "get".
"Test Commonsense" tab 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. 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.
The second tab "get", displays more technical information and
allows for some options. 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 Implementation
- Add
cn5apinet.dll to your project 'References' via Solution
Explorer. (For cn5apinet.dll v1.6.x.x make sure that
Newtonsoft.Json.dll
(Json40r5)
resides in the same folder as cn5apinet.dll)
.
- Add 'using
cn5apinet;', etc to your source code to use the namespace.
- Create the main API engine with 'ConceptNetAPI
CNClient = new
ConceptNetAPI();',
etc.
- Below are the methods in ConceptNetAPI

Performing the lookup
cn5_node raw_concept = CNClient.lookup_concept_raw(LANG.en, tbGetPreview.Text.Trim().Split('/')[1]);
if (raw_concept != null)
{
tbOutputGet.AppendText("lookup_concept_raw response : " + Environment.NewLine);
tbOutputGet.AppendText(raw_concept.PPrint());
}
else
tbOutputGet.AppendText("null");
tbOutputGet.AppendText(Environment.NewLine + "--------------------" + Environment.NewLine);
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>();
#region GraphML
if (cBoxGraphML.Checked)
{
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[2].Split(seperatorsForwardSlash, StringSplitOptions.RemoveEmptyEntries);
nodeX.Text = nodetext[nodetext.Length - 1];
nodeList.Add(nodeX);
edge_argList.Add((edge_arg)o);
string[] edgetext = spiltstart[0].Split(seperatorsForwardSlash, StringSplitOptions.RemoveEmptyEntries);
GraphMLEdge edge0 = new GraphMLEdge(i, node0.NodeID, nodeX.NodeID, edgetext[edgetext.Length - 1] + " " + Convert.ToString(((edge_arg)o).score));
edgeList.Add(edge0);
i = i + 1;
}
}
}
int numofNodes = nodeList.Count;
int g = 1;
double radius = 100.0;
if (numofNodes > 10)
radius = (nodeList[0].Geometry.height * 0.45) * numofNodes / 2;
for (double h = 0.0; h < 360.0; h += (360 / numofNodes))
{
if (g >= numofNodes)
break;
double angle = h * System.Math.PI / 180;
if (cBoxQChildren.Checked == true && g < 4)
{
nodeList[g].Geometry.x = (int)(nodeList[0].Geometry.x + radius * System.Math.Cos(angle)) * 2;
nodeList[g].Geometry.y = (int)(nodeList[0].Geometry.y + radius * System.Math.Sin(angle)) * 2;
}
else
{
nodeList[g].Geometry.x = (int)(nodeList[0].Geometry.y + radius * System.Math.Cos(angle));
nodeList[g].Geometry.y = (int)(nodeList[0].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);
System.Diagnostics.Process.Start(graphFile.FullPathLocation);
}
#endregion GraphML
Conclusion
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.
References
- The ConceptNet Web API, Massachusetts Institute of Technology [^]
- Newtonsoft.Json.dll, Json.NET Copyright (c) 2007 James Newton-King [^]
- Open Mind Common Sense Project [^]
Updates
- 12/17/11
- Uploaded
CNAPINetDemo v1.6.0.0 Microsoft Visual C# Express 2010 project files
- Uploaded
cn5apinet.dll v1.6.0.0 and necessary files.
- 12/18/11
- Uploaded
CNAPINetDemo v1.6.0.0 Microsoft Visual C# Express 2010 project files with Json40r5 dll so manual download and reference add is no longer needed.
- Misc article image tweaks.