思路:
从DBpedia下载实体数据、含歧义的实体数据。
使用python对下载的数据做一些预处理,主要是提取实体、提取有歧义的实体的映射关系、提取有歧义的实体的上下文。
long_abstracts_preprocessing_entity(file_contents_examples)如下:
Animalia (book)
long_abstracts_preprocessing(file_contents_examples)如下:
Animalia (software)=>atlas:1 rand:1 shrugged:1 list:1 characters:1
Animalia (book)=>gold:1 copies:1 illustrated:1 edition:3 tenth:1 children:1 special:1 1986:1 anniversary:3 graeme:1 book:1 embossed:1 25th:1 2012:1 animalia:1 sold:1 base:1 numbered:1 1996:2 originally:1 signed:1 jacket:1 published:2
disambiguations_preprocessing(file_contents_examples)如下:
Animalia=>Animalia (law)<=Animalia (software)
使用LingPipe做实体识别。
使用Lucene对所有有歧义的实体的映射关系建立索引(index1)。
使用Lucene对所有有歧义的实体的上下文建立索引(index2)。
构建网页,获取用户输入,并传入后台处理。处理思路:
使用LingPipe识别出输入中的所有实体(比如,Alien),对每一个实体作如下处理:
从index1中查询该实体是否属于有歧义的实体,如果不是,则直接作为返回结果;如果是:
将index1中提供的所有可能的有歧义的实体(比如,Alien (law)、Alien (software))提取出来,对每一个这样的有歧义的实体:
从index2中查询出该实体的上下文信息(比如,Alien (law)=>gold:1 copies:1 illustrated:1 edition:3 tenth:1):
判断该有歧义的实体的上下文和用户输入的内容(即,LingPipe识别出的实体的上下文)进行比较,计算相似度,返回得分最高的那个有歧义的实体作为LingPipe识别出来的实体最可能的候选。(即,对于Alien,如果Alien (law)的上下文与Alien (software)的上下文相比,离用户输入内容更相近,则返回Alien (law),注意,同时返回原实体Alien。)
在网页界面,获取到后台的返回值后,作如下处理:
如果该返回值只有一部分内容,表明该实体无歧义,直接用replace替换原文中用户的输入:
showData=showData.replace(returnDatas[i], "<a target='_blank' href='https://en.wikipedia.org/wiki/"+returnDatas[i]+"'>"+returnDatas[i]+"</a>");
如果该返回值包含两部分,表明识别出的实体有歧义,第一部分为原实体,第二部分为最可能的候选实体,则replace替换规则如下:
showData=showData.replace(temp[0], "<a target='_blank' href='https://en.wikipedia.org/wiki/"+temp[1]+"'>"+temp[1]+"</a>");
替换时有个假设:任意实体不是另一个实体的前缀!否则会出现出现replace错误,这个我想大家都能想明白。
从DBpedia下载实体数据、含歧义的实体数据。
使用python对下载的数据做一些预处理,主要是提取实体、提取有歧义的实体的映射关系、提取有歧义的实体的上下文。
long_abstracts_preprocessing_entity(file_contents_examples)如下:
Animalia (book)
long_abstracts_preprocessing(file_contents_examples)如下:
Animalia(software)=>atlas:1 rand:1 shrugged:1 list:1 characters:1
Animalia (book)=>gold:1 copies:1 illustrated:1 edition:3 tenth:1 children:1 special:1 1986:1 anniversary:3 graeme:1 book:1 embossed:1 25th:1 2012:1 animalia:1 sold:1 base:1 numbered:1 1996:2 originally:1 signed:1 jacket:1 published:2
disambiguations_preprocessing(file_contents_examples)如下:
Animalia=>Animalia(law)<=Animalia(software)
使用LingPipe做实体识别。
使用Lucene对所有有歧义的实体的映射关系建立索引(index1)。
使用Lucene对所有有歧义的实体的上下文建立索引(index2)。
构建网页,获取用户输入,并传入后台处理。处理思路:
使用LingPipe识别出输入中的所有实体(比如,Alien),对每一个实体作如下处理:
从index1中查询该实体是否属于有歧义的实体,如果不是,则直接作为返回结果;如果是:
将index1中提供的所有可能的有歧义的实体(比如,Alien (law)、Alien (software))提取出来,对每一个这样的有歧义的实体:
从index2中查询出该实体的上下文信息(比如,Alien (law)=>gold:1 copies:1 illustrated:1 edition:3 tenth:1):
判断该有歧义的实体的上下文和用户输入的内容(即,LingPipe识别出的实体的上下文)进行比较,计算相似度,返回得分最高的那个有歧义的实体作为LingPipe识别出来的实体最可能的候选。(即,对于Alien,如果Alien (law)的上下文与Alien (software)的上下文相比,离用户输入内容更相近,则返回Alien (law),注意,同时返回原实体Alien。)
在网页界面,获取到后台的返回值后,作如下处理:
如果该返回值只有一部分内容,表明该实体无歧义,直接用replace替换原文中用户的输入:
showData=showData.replace(returnDatas[i], "<a target='_blank' href='https://en.wikipedia.org/wiki/"+returnDatas[i]+"'>"+returnDatas[i]+"</a>");
如果该返回值包含两部分,表明识别出的实体有歧义,第一部分为原实体,第二部分为最可能的候选实体,则replace替换规则如下:
showData=showData.replace(temp[0], "<a target='_blank' href='https://en.wikipedia.org/wiki/"+temp[1]+"'>"+temp[1]+"</a>");
替换时有个假设:任意实体不是另一个实体的前缀!否则会出现出现replace错误,这个我想大家都能想明白。
下几篇博客讲具体实现。
参考文献:
[1] Mendes, Pablo N, Jakob, Max, Garc&#, et al. DBpedia spotlight: Shedding light on the web of documents[C]// Proceedings of the 7th International Conference on Semantic Systems. ACM, 2011:1-8.
[2] Han X, Sun L. A Generative Entity-Mention Model for Linking Entities with Knowledge Base.[J]. Proceeding of Acl, 2011:945-954.
[4] http://alias-i.com/lingpipe/demos/tutorial/ne/read-me.html
[5] http://wiki.dbpedia.org/Downloads2014
[6] http://www.oschina.net/p/jieba(结巴分词)