在 C# 中玩转 MIT 的 ConceptNet 2.1。






4.23/5 (10投票s)
2006年1月3日
8分钟阅读

101531

1662
一个 ConceptNet .Net 工具类库。
引言
大约三年前,我在研究自然语言处理、常识数据、人工智能代码和理论时,第一次了解到 Hugo Liu 的研究。他的工作,以及 MIT 其他人的工作,可以说是当今研究的最新和最伟大的成果,对于任何程序设计者来说都非常有价值,至少也很有信息量。MIT 提供的项目大多不是用 Microsoft Visual Studio .Net 编写的,我正试图将 ConceptNet 用于教育、研究目的,以及仅仅为了好玩而利用其中的技术。
什么是 ConceptNet?
ConceptNet¹ 是一个常识知识库,主要由 Open Mind Project 组成,由 Hugo Liu 和 Push Singh (麻省理工学院媒体实验室) 编写和整理。ConceptNet 2.1 (撰写本文时为当前版本) 还包含了 MontyLingua,一个自然语言处理包。ConceptNet 是用 Python 编写的,但其常识知识库存储在文本文件中。要阅读关于 ConceptNet 完整概述的更具体细节,请阅读 Liu 和 Singh 的杰出论文 (pdf)。
ConceptNet 有何独特之处?
与其他项目(如 WordNet 或 Cyc)不同,ConceptNet 更侧重于上下文。ConceptNet 使得在知识库的限制内,允许计算机通过使用称为知识线 (K-Lines: Minsky 提出的术语,参见《思维的社会》 (1987)) 的概念关联来理解新概念甚至未知概念。知识线可以被认为是关于某个主题或任务的先前知识的列表。ConceptNet 实际上使用其二十种关系类型(分为八个类别,包括知识线)将这些知识线组合起来,形成一个数据网络来模拟概念推理。真正使这一切成为可能的是 ConceptNet 的关系本体 (八个类别和二十种关系类型)。
ConceptNet 以 MIT 的 Open Mind Common Sense Project 知识库为基础构建。ConceptNet 在两个过程中使用其数据:标准化过程和松弛过程。标准化过程涉及所有谓词的过滤和词法提炼 (动词和名词被简化为其基本形式)。ConceptNet 在此阶段还删除定冠词 ("a"、"the" 等) 和情态动词 ("may"、"could"、"will" 等)。它还使用词性标注来验证结构良好的词序。(标准化过程未在此演示中展示。请随意使用您自己的 POS Tagger 与此类库结合使用) 松弛过程会提高或“提升”高度加权的常识谓词节点(来自谓词文件的一行),并合并重复的节点。这反映在每个谓词的 "f" 和 "i" 元数据标签中。其中 f 等于陈述次数,i 等于推断次数。
ConceptNet 的关系本体
ConceptNet 将主题联系在一起的能力归功于其关系本体定义的二十种关系类型。这是 2.1 版的二十种关系类型和八个类别。
![]() (由 Hugo Liu 和 Push Singh 提供,ConceptNet:实用的常识推理工具包) |
• K-Lines: ConceptuallyRelatedTo, ThematicKLine, SuperThematicKLine • Things: IsA, PartOf, PropertyOf, De.nedAs, MadeOf • Spatial: LocationOf • Events: SubeventOf, PrerequisiteEventOf, First-SubeventOf, LastSubeventOf • Causal: EffectOf, DesirousEffectOf • Affective: MotivationOf, DesireOf • Functional: CapableOfReceivingAction, UsedFor • Agents: CapableOf Example lines from ConceptNet's 2.1 predicate files: (UsedFor "ball" "throw" "f=4;i=0;") (LocationOf "popcorn" "at movie" "f=7;i=0;") (CapableOfReceivingAction "film" "place on reel" "f=2;i=0;") (IsA "guitar" "musical instrument with string" "f=2;i=0;") (SubeventOf "talk" "debate" "f=2;i=0;") (CapableOf "person" "write book" "f=11;i=1;") (MotivationOf "audition for part" "act in play" "f=2;i=0;") (PropertyOf "bacteria" "small" "f=2;i=0;") |
让我们开始吧,让乐趣滚滚而来!
需要什么以及在哪里下载…
再次提醒,请记住 ConceptNet 2.1 是用 Python 编程语言编写的,而不是 C#,但其常识知识库数据是以文本文件格式存储的,解压后总计约 96mb。您必须通过同意其用户协议来下载 ConceptNet 文本文件 (当然,以下所有下载项目也适用),然后下载整个 ConceptNet Python 项目。
ConceptNet 2.1 附带 MontyLingua (总下载量 28mb) - 本文类库演示的**必需**。 下载标准 Python 软件 - **可选**。可以在该页面找到 Python 2.4.2 Windows 安装程序。 下载 IronPython Beta 3 - **可选**。它是针对 .Net 和 WinFX/Mono 的 Python 语言实现。(我目前正在编写一个简短的演示应用程序,使用 IronPython 运行 ConceptNet Mini-Browser) |
我的 VS.Net C# ConceptNet 类库
这是一个非常简单的、无附加功能的类库,用 MS VS.Net 编写。我很快地将其整合起来,主要是因为我昨天才刚下载了 ConceptNet,并注意到 VS.Net 友好代码的短缺。出于某种原因,我不记得 ConceptNet 之前有公开下载过,尽管我可能记错了,但我对这个项目已经了解了一段时间。其论文一直可以通过 MIT 获取。
解决方案中有两个项目:ConceptNet Demo App 和 ConceptNetUtils。ConceptNetUtils 是 ConceptNet 类库,包含三个类:FoundList、Misc、Search。
ConceptNetUtils.FoundList Holds search result data in an index format. Access: Public Base Classes: Object Members Description protected string[] LineFound //Holds the strings. static public int size = 999;//This can hold up to 999 //data strings. public string this[]//Holds string data. Count() //Returns int count of non "" strings //(populated indexes). Reset() //Resets data to null. public int get_f(int index) //Returns int of the //f metadata in predicate string line(node) public int get_i(int index) //Returns int of the //i metadata in predicate string line(node) |
ConceptNetUtils.Misc Created for Misc Methods Access: Public Base Classes: Object Members Description public string RemoveCategoryString(string R_TYPE) // Returns string without the "K-Lines: " or "Spatial: ", // if All then remove string after. public string XMLGetNode(string path_xmlfilename, string elementname) // Returns string of data in an element node. public string XMLGetAttribute(string path_xmlfilename, string elementname, string attributename) // Returns string of Attribute data in an element node. |
ConceptNetUtils.Search Takes care of Searching ConceptNet text files. Access: Public Base Classes: Object Members Description public bool CreateTextFile(string fullfilename) // Returns true if Creates a text file using the // current FoundList data. public string GetFoundListLine(int index) // Returns string of data in iterator (LineFound[]). public int GetTotalLineCount() // Returns int count of total lines found. public void SearchFor(string fullpathfilename, string SubjectWord, string R_Type, int MAX_RESULTS, bool CreateOutputFile, string fullpathTextFilename) //Searches incoming ConceptNet text file //and fills the FoundList iterator (LineFound[]). //fullpathfilename = Path of ContextNet .txt //fullpathTextFilename = Path of .txt to be created. public static string Predicatefile1; public static string Predicatefile2; public static string Predicatefile3; public static string Predicatefile4; public static string Predicatefile5; public void SearchFor(int index, string SubjectWord, string R_Type, int MAX_RESULTS, bool CreateOutputFile, string fullpathTextFilename) // Searches incoming ConceptNet int index 1 to x // and fills the FoundList iterator. public void XMLSearchForChecked(string path_xmlfilename, string SubjectWord, string R_Type, int MAX_RESULTS, bool CreateOutputFile, string fullpathTextFilename) // Searches for the Attribute: (checked="yes") found in // an XML file and then searches predicate files. public static string GetPredicatePathtoFilename(int index) // Returns full string path to the requested index number. // Index starts at 1. public void XMLLoadFilePaths(string settingsxmlfile) // Sets Predicatefile1 thru Predicatefile5 variables // after loading them from an XML file. public static int getnode_f(string node) public static int getnode_i(string node) public void Sort_f(ArrayList inList, out ArrayList rankedList) public void Sort_i(ArrayList inList, out ArrayList rankedList) public class Compare_f : IComparer public class Compare_i : IComparer |
如何运行演示
1.) 确保您已下载并安装 ConceptNet 2.1。(我将其安装在路径 ...\My Documents\Python Projects\conceptnet2.1\)
2.) 下载并解压缩本文的 .Net 解决方案和项目文件。
3.) 导航到“...\ConceptNet Demo App\bin\Release”位置并运行 ConceptNet Demo App.exe。它将自动打开“设置知识库文件位置”对话框,并且在第一次运行时,您**必须**点击浏览按钮选择一个谓词文件 (ConceptNet 或其他),然后点击确定。后续运行将记住您希望搜索的已勾选谓词文件的位置。
4.) 现在您可以 a) 输入一个词,b) 选择一个关系 (ConceptNet 查看 IsA,然后 PropertyOf),c) 点击“搜索”按钮显示找到的节点。然后您可以通过点击“按 f 排序”或“按 i 排序”来排序它们。
待办事项
--
- 添加 MSBNx COM (或任何其他 BN) 用于创建、评估和衡量贝叶斯网络,并轻松输出到 XML 格式。
- 向类库添加更多方法。
- 创建更多松弛方法。
结论
ConceptNet 2.1 可以成为创建个性化常识知识库网络的工具。希望这个 MS VS.Net 类库项目能提供信息、有用且有趣。
新版本
本文中发布的 0.x 版本将不再更新。我正在使用 Microsoft Visual C# Express 2005 和 .Net 2.0 框架开发一个更新版本,它将作为我正在开发的最新版 ConceptNet C# 类库。它很可能会利用 IronPython 库。如果您有兴趣,这里有一个关于如何使用 IronPython 执行 ConceptNet Mini-Browser (用自然 Python 代码编写) 的简短预览。
我的 wdevs 博客文章,其中包含一些代码。
我只是在有空的时候才进行开发。
参考文献
¹ Liu, H. & Singh, P. (2004) ConceptNet: A Practical Commonsense Reasoning Toolkit. BT Technology Journal, To Appear. Volume 22, forthcoming issue. Kluwer Academic Publishers.
ConceptNet:实用的常识推理工具包,Hugo Liu 和 Push Singh 麻省理工学院媒体实验室
Investigating ConceptNet,Dustin Smith;导师:Stan Thomas 博士。2004 年 12 月
Open Mind Common Sense Project
Hugo Liu 网站
WordNet
Cyc
更新
1/3/06- 在 Form1.cs 中添加了方法,以便在离开文本框时将单词转换为小写。搜索必须使用小写进行。
- ConceptNetUtils.Search.SearchFor 自动将输入的 SubjectWord 转换为小写。
- 修复了组合框的小绘图问题。(如果您遇到此 bug,请给我发电子邮件。)
- 添加了待办事项部分。
- 上传了 ConceptNet Demo App 版本 0.01032006.2rc1 - 上传了 ConceptNetUtils 二进制版本 0.01032006.2b1
- 对于演示,谓词文件的加载现在是 XML 存储并自动化的。它们不再是硬编码的。我添加了 FileOptionsForm.cs 来处理这个问题。我还添加了一个 ImageList。
- 对于 ConceptNetUtils 类库,我添加了 XML 功能。演示创建一个 Settings.xml 文件来保存谓词文件的位置,现在类库可以读取 XML 文件。
- Search.cs: 添加了 SearchFor() 重载、XMLSearchForChecked()、GetPredicatePathtoFilename()、XMLLoadFilePaths()。
- Misc.cs: 添加了 XMLGetNode()、XMLGetAttribute()
- 上传了 ConceptNet Demo App 版本 0.01092006.0rc2 - 上传了 ConceptNetUtils 二进制版本 0.01092006.0b2
- 在“ConceptNet 有何独特之处?”部分添加了关于 ConceptNet 的标准化和松弛过程的详细信息。
- 对于演示,我添加了两个新按钮“按 f 排序”和“按 i 排序”,这演示了 ConceptNet 项目的“松弛过程”。
- 对于 ConceptNetUtils 类库,我添加了一些完成 ConceptNet“松弛过程”所需的方法。我还修复了一些在需要搜索多个谓词文件时出现的问题。
- Search.cs: 添加了 getnode_f()、getnode_i()、Sort_f()、Sort_i()。还添加了两个 IComparer 类来协助排序/提升知识,Compare_f : IComparer, Compare_i : IComparer。
- FoundList.cs: 添加了 get_f()、get_i()。
- 通过电子邮件通知了 MIT 的 ConceptNet 团队有关本文的信息。
- 清空了 \doc 文件夹中来自项目 VS 生成的文档。
- 上传了 ConceptNet Demo App 版本 0.01142006.0rc4 - 上传了 ConceptNetUtils 二进制版本 0.01142006.0b3
- 编辑了引言部分。
- 编辑了 Search.cs XMLSearchForChecked 方法的文档。
- 修改了 ConceptNetUtils.Search.SearchFor() 以仅返回单词本身的结果。例如,搜索“eat”也返回了“theater”等。
- 上传了 ConceptNet Demo App 版本 0.01152006.0rc5 (build 2206.41738) - 上传了 ConceptNetUtils 二进制版本 0.01152006.0b4 (build 2206.41736)
- 编辑了“如何运行演示”部分,以反映演示和类库的新版本。
- 在“需要什么以及在哪里下载…”部分编辑了指向 IronPython-1.0-Beta3 的链接 (原为 Beta 1)。
- 添加了“新版本”部分。0.x 版本已不再开发,并且正在开发一个基于 .Net 2.0 / IronPython 的新 ConceptNetUtils 类库。