65.9K
CodeProject 正在变化。 阅读更多。
Home

cn5apinet - ConceptNet5 API 库

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.97/5 (6投票s)

2011年12月17日

GPL3

5分钟阅读

viewsIcon

48373

downloadIcon

1263

连接到 ConceptNet 5 的 REST API

(注意:cn5apinet.dll 依赖 Newtonsoft.Json.dll (Json40r5) 来处理 JSON。) 下载并安装 yEd Graph Editor,这样您就可以显示演示创建的 .graphml 文件。要运行演示,请确保您已安装 Microsoft .Net 4 Runtime。解压源代码后,打开 CNAPINetDemo\CNAPINetDemo\CNAPINetDemo.sln

引言

ConceptNetAPI Demo GraphML result GraphML result

更新 2015-11-27:请注意,截至 2015 年 11 月 11 日,ConceptNet5 API 已升级到 5.4.2 版本,高于之前的 5.x 版本;“ConceptNet 5.4 的 API 与 5.x 相同。由于我们一次只托管一个版本的知识库,所有带有 /5.1/ 的 URL 现在都会重定向到相应的 /5.2/ URL。” - 官方文档。要使源代码项目正常工作,只需更新 Common 类文件中的 URL 行,将“5.1”替换为“5.4”(或 ConceptNet 的当前发布版本)。

ConceptNet¹ 是一个常识知识库,主要由麻省理工学院 (MIT) 编写和维护的 Open Mind Project 贡献而成。这个库是我为了与 ConceptNet 5(撰写本文时为 beta 版)进行通信而构建的。它使用了一种代表性状态转移 (REST) API,与 ConceptNet 4 类似,但结构和设计有所不同。ConceptNet 5 “是一个图;更确切地说,它是一个超图,意味着它有关于边的边。ConceptNet 中的每个陈述都有指向它的论证,解释了它的来源以及信息的可靠性。”

.NET C# ConceptNet API 类库 (cn5apinet)

cn5apinet 可以提供一个接口来访问 ConceptNet 5 REST API。使用该 API 可以接收 ConceptNet 5 超图的列表和对象。与 cnapinet(来自 ConceptNet4)相比,新增加了一个用于自动创建 GraphML xml 文件的命名空间。该命名空间将在下面快速解释。演示展示了如何组装 cn5apinet.GraphML.GraphMLFile

ConceptNet 5.1 API 有三种检索其边数据的方法:Lookup(查找)、Search(搜索)和 Association(关联)。正如 ConceptNet 文档所述,“Lookup 适用于您知道 ConceptNet 中对象的 URI,并希望查看包含它的边列表的情况。Search 查找与某些条件匹配的边列表。Association 适用于查找与特定概念或概念列表相似的概念。” cn5apinet 现在有三个函数来执行这些方法。

    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)
    

cn5apinet 命名空间


cn5apinet Classes
 
(点击放大)


cn5apinet namespace

cn5apinetGraphMLNamespace

 

 

 

演示

[请确保您已安装 Microsoft .Net 4 Runtime]

 

 

 

演示应用程序中有三个标签来表示三种不同的查询方法:“Lookup”(查找)、“Search/Test Commonsense”(搜索/测试常识)和“Association”(关联)。“Lookup”执行一个基本查询,输入一个单词或短语;初始查找后,它会用找到的关系填充下拉框。然后,您可以按“lookup relationship”(查找关系)按钮来仅显示具有该选定关系的图。

 

接下来是 “Search/Test Commonsense”(搜索/测试常识)选项卡,它允许您查询 ConceptNet 5 (CN5) 超图的知识,并以“是”、“否”、“也许”的答案返回结果;它基本上告诉您它认为您的陈述是真的、假的,还是可能为真。按“Ask”(询问)按钮实际上并没有执行 Search 函数,只是在演示中用于显示 CN5 的实际用途,并保留了原始演示的逻辑。这是基于 CN5 超图中的得分 [0 <= 为假,1 = 也许,2 > 为真]。例如,得分 42.39 的置信度高于得分 3.87。按“Search and open Graph!”(搜索并打开图!)按钮会实际执行 CN5.1 Search 方法。

第三个选项卡“Association”(关联)执行一个关联查找,用于查找与特定概念或概念列表相似的概念。

下载并安装 yEd Graph Editor,这样您就可以显示演示创建的 .graphml 文件。此选项卡主要基于 ConceptNet5 的 Web API 文档。 有时,截图是最好的解释方式,所以请看下面的内容。

cn5apinet 实现

  1. 通过解决方案资源管理器将 cn5apinet.dll 添加到您的项目的“引用”中。(对于 cn5apinet.dll v1.6.x.x,请确保 Newtonsoft.Json.dll (Json40r5) cn5apinet.dll 位于同一文件夹中)。
  2. 在源代码中添加“using cn5apinet;”等语句来使用命名空间。
  3. 使用“ConceptNetAPI CNClient = new ConceptNetAPI();”等语句创建主 API 引擎。
  4. 以下是 ConceptNetAPI 中的方法

ConceptNetAPI Members

执行三种查找

        
        /// <summary>
        /// Lookup is for when you know the URI of an object in ConceptNet, and want to see a list of edges that include it.
        /// </summary>
        /// <param name="language" />language
        /// <param name="concept_name" />Expects entirely built suffix for url. For example, '/contributor/omcs/rspeer', 'toast?offset=5&limit=5', etc.
        /// <returns>Lookup object</returns>
        public Lookup Lookup(LANG language, String concept_name)......

        /// <summary>
        /// Search finds a list of edges that match certain criteria.
        /// </summary>
        /// <param name="language" />language
        /// <param name="rel" />relationship
        /// <param name="start" />start
        /// <param name="end" />end
        /// <param name="text" />text=word: matches any of startLemmas, endLemmas, or relLemmas.
        /// <param name="limit" />limit=n: change the number of results from the default of 50.
        /// <returns>Search object</returns>
        public Search Search(LANG language, String rel, String start, String end, String text, int limit)...

        /// <summary>
        /// Association is for finding concepts similar to a particular concept or a list of concepts.
        /// </summary>
        /// <param name="language" />language
        /// <param name="word" />If you wish multi, start with use ',' between each word. ie: 'toast,cereal,juice,egg'.
        /// <param name="filter" />filter=URI: return only results that start with the given URI. For example, filter=/c/en returns results in English.
        /// <param name="limit" />limit=n: change the number of results from the default of 10.
        /// <param name="multipleterms" />If true, then expecting a list like 'toast,cereal,juice,egg' etc.
        /// <returns>Association object</returns>
        public Association Associations(LANG language, String word, String filter, int limit, bool multipleterms, int weight)...

创建 .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)
{
    //let's look at the edges, nodes and create a graphML

    //Create the seed node...
    GraphMLNode node0 = new GraphMLNode(0, new Geometry(30.0, 50.0, 1, 1));//customize the node...
    node0.Text = tbGetKey.Text.Trim();
    node0.FillColor = "#00FF00";//Green
    //Add this node to list
    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")
            {
                //need Json to convert o;
                Dictionary<String, String> startDict = new Dictionary<string, string>();
                String[] spiltstart = Convert.ToString(((edge_arg)o).start).Split(',');
                // [0] = "/assertion//relation/AtLocation/"
                // [1] = "/concept/en/book/"
                // [2] = "/concept/en/shelf/"

                //= Convert.ToString(((edge_arg)o).key);
                //Create the connecting Edge...
                //Add this node
                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);//add to edge_argList

                //Add this edge to list
                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;
            }
        }
    }
    //Re-arrange the nodes
    int numofNodes = nodeList.Count;
    int g = 1;
    //Draw the nodes around first node[0] in a circle
    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)
        {
            //push out the top 3 nodes, they should be returned by score in the json
            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

结论

ConceptNet REST API 仍在不断发展,拥有一个 .Net 接口对于探索 ConceptNet 非常有益。它不是执行查找的最有效方式(如果您探索 ConceptNet5 网站,可以找到执行查询的替代方法(MongoDB 2.0 数据库和 24GB 的 JSON 文件等)),但它对于编写一些概念验证应用程序或在 .Net 中进行 ConceptNet 学习非常方便。

参考文献

  • ConceptNet Web API,麻省理工学院 [^]
  • Newtonsoft.Json.dll,Json.NET 版权所有 (c) 2007 James Newton-King [^]
  • 开放思维常识项目 [^]

更新

  • 12/17/11
    • 上传了 CNAPINetDemo v1.6.0.0 Microsoft Visual C# Express 2010 项目文件
    • 上传了 cn5apinet.dll v1.6.0.0 及必要文件。 
  • 12/18/11
    • 上传了 CNAPINetDemo v1.6.0.0 Microsoft Visual C# Express 2010 项目文件,包含 Json40r5 dll,因此不再需要手动下载和添加引用。
    • 对文章图片进行了一些细微调整。
  • 06/20/12
    • 发布了 ConceptNet API 5.1 的说明,指出 cn5apinet 已损坏;正在进行更新。
  • 06/28/12
    • 更新了文章以支持 ConceptNet API 5.1。上传了 CNAPINetDemo v1.9.0.0。上传了 cn5apinet.dll v1.9.0.0 及必要文件。 
  • 03/28/14 
    • 根据邮件列表和文档,添加了 ConceptNet API 从 5.1 升级到 5.2 的说明。
    • 使用新 API 版本测试了演示。 
© . All rights reserved.