import copy
import json
from random import random
import gensim
from graphviz import Digraph
from gensim.models import keyedvectors
from Graph import Node, Edge
import math
import numpy as np
class GraphForEntityV0_4_9():
def __init__(self,wv_from_text,sentence_dict,all_entity):
self.sentenceDict=sentence_dict
self.all_entity=all_entity
self.Graph = [] # 图中的边集
self.nodeDict = dict() # 图中的点集
self.v_v_Co_Dict = dict() # 动词之间相似度,查完存于此表中,下一次直接查询该字典
self.T_T_Sim_Dict = dict() # 类型之间相似度,查完存于此表中,下一次直接查询该字典
self.wv_from_text = wv_from_text # 腾讯预训练语料
self.triple_list=[] # 获得三元组的列表
self.entity_type_list=[] # 获得实体类型对
"""
所有的实体类别
"""
f = open(r".\model3_file\filtered_types_new.json", "r", encoding="utf-8") # 经过滤后的选用的实体类别
json_file_dict = json.load(f)
self.all_type = list(json_file_dict.keys())
self.type_order_dict=json_file_dict
f.close()
"""
每个实体的所有类别
"""
f = open(r".\model3_file\get_key_class_all_new.json", "r", encoding="utf-8") # 每个实体根据xlore给出的标签
file = f.read()
self.all_entity_type = json.loads(file)
f.close()
"""
每个实体的所有类别相似度
"""
f = open(r".\model3_file\typeSimForFilteredTypes.json", "r", encoding="utf-8") # 用PMI计算的类型相似度
file = f.read()
self.all_type_sim = json.loads(file)
f.close()
def get_type_list_order(self):
entity_type_list=sorted(self.entity_type_list,key=lambda x:x[2],reverse=True)
return entity_type_list
# 根据实体类别生成动词与实体的边并加入图中
def find_edge(self,clean_word,flag,rangeEntityList,seedNode,weight=1):
n_clean_word = clean_word + flag
rangeEntityList.append(n_clean_word)
if n_clean_word not in self.nodeDict:
entityNode = Node(n_clean_word, 1, 'entity')
self.nodeDict[n_clean_word] = entityNode
else:
entityNode = self.nodeDict[n_clean_word]
xNode = entityNode
entityEdge = Edge(seedNode, entityNode, weight)
if not self.graphHasEdge(self.Graph, entityEdge): # 判断该边有没有出现在图中
self.Graph.append(entityEdge) # 若不在,在图的边集中加入该边
else: # 如果该边已经存在,则该边的权重+1
entityEdge = self.graphHasEdge(self.Graph, entityEdge) # 从Graph中找到这条边
entityEdge.weight += 1 # 权重+1
return xNode
def getEntityAndRelationBySeeds(self,seed):
"""
seed: 种子动词的名称
"""
weight = 1
rangeEntityList = [] # 初始化实体层tail节点列表
domainEntityList = [] # 初始化实体层head节点列表
allEntityList = self.all_entity
if seed =='包括':
print('有问题')
if seed in self.nodeDict: # 如果种子已经在nodeDict中
seedNode = self.nodeDict[seed] # 直接取出seedNode
else:
seedNode = Node(seed,1,"relation") # 否则新建一个seed节点,Node(节点名,score,节点类型),score设为1,节点类型设为"relation"
self.nodeDict[seed] = seedNode
for sentID in self.sentenceDict:
sentence = self.sentenceDict[sentID] # 遍历每个句子
for item in sentence:
word = item[0]
location = word.find("_")
Numlocation = word.find("#")
orig_word = word[Numlocation + 1:location] # 得到每个item的汉字
if orig_word == seed: # 如果该词与seed一样,且是个动词,因为动词里可能有’#‘
headNode = None # 初始化头实体
tailNode = None # 初始化尾实体
headFirstVerb = False # 当在寻找头实体时,遇见了动词
tailFirstVerb = False # 当在寻找尾实体时,遇见了动词
# 找尾实体
find_n = False
find_entity=False
find_Ans=False
n_clean_word=''
for aIndex in range(sentence.index(item) + 1 , len(sentence)): # 再次遍历在sentence中该动词后的item
aItem = sentence[aIndex]
aWord = aItem[0]
aLocation = aWord.find("_")
aNumlocation = aWord.find("#")
clean_word = aWord[aNumlocation + 1:aLocation]
# 找到标点符号则终止尾实体查找
if '$' in aWord:
break
# 找到两个动词以上则不找尾实体
if sentence.index(item)+2<len(sentence):
if '_v' in sentence[sentence.index(item)+1][0] and '_v' in sentence[sentence.index(item)+2][0]:
tailFirstVerb = True
# 如果没找到实体,优先找名词
if tailFirstVerb==False and '_n' in aWord and aItem[1]>item[1] and aNumlocation == -1:
if aItem[1] - item[1] - len(orig_word) <= 5 and len(clean_word)>1:
find_n=True
n_clean_word=clean_word
# 如果找到方位实体词
if tailFirstVerb==False and '_Ans' in aWord and aItem[1]>item[1]:
Ans_clean_word=clean_word
find_Ans=True
# 如果找到实体词
if tailFirstVerb == False and (clean_word in allEntityList) and aItem[1] > item[1] and aNumlocation != -1:
if len(clean_word)>1 and aItem[1] - item[1] - len(orig_word) <= 5: # 设置两个实体距离不能超过10
find_entity=True
entity_clean_word=clean_word
break# 只选最近的实体
# 如果到最后一个字没有找到这个实体,则将名词作为尾实体
if find_Ans is True:
print('选择 方位词 tail')
tailNode=self.find_edge(Ans_clean_word,'_tail',rangeEntityList,seedNode)
if find_entity is True and find_Ans is False:
print('选择 实体 tail')
tailNode=self.find_edge(entity_clean_word,'_tail',rangeEntityList,seedNode)
if find_n is True and find_entity is False and find_Ans is False:
print('选择名词实体tail')
tailNode=self.find_edge(n_clean_word,'_tail',rangeEntityList,seedNode)
# 找头实体
find_subject=False
find_nh=False
find_entity_head=False
for aIndex in range(sentence.index(item) - 1,-1,-1): # 再次遍历在sentence中该动词前的item
aItem = sentence[aIndex]
aWord = aItem[0]
aLocation = aWord.find("_")
aNumlocation = aWord.find("#")
clean_word = aWord[aNumlocation + 1:aLocation]
if '$' in aWord:
break
if sentence.index(item) - 2>0:
if '_v' in sentence[sentence.index(item) - 1][0] and '_v' in s
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
知识图谱是一种结构化的知识表达形式,它以图形的方式组织和存储了大量实体(如人、地点、事件等)及其相互关系。在知识图谱中,实体作为节点,实体之间的各种语义关联则通过边进行连接,形成了一个庞大的数据网络。 知识图谱的核心价值在于其能够精确、直观地表示复杂世界中的知识,并支持高效的知识查询与推理。例如,在搜索引擎中,知识图谱可以提升搜索结果的相关性和准确性,为用户提供直接的答案而非仅仅是网页链接。同时,知识图谱还能支撑高级的人工智能应用,比如问答系统、推荐系统、决策支持等领域。 构建知识图谱的过程通常包括数据抽取、知识融合、实体识别、关系抽取等多个步骤,涉及到自然语言处理、机器学习、数据库技术等多种技术手段。知识图谱的不断完善有助于实现从海量信息中挖掘深层次、有价值的知识,从而推动人工智能向着更加理解人类世界的智慧方向发展。 总之,知识图谱是一个大规模、多领域、多源异构知识集成的载体,是实现智能化信息系统的基础工具和关键基础设施,对于提升信息检索质量、推动智能应用研发具有重要作用。
资源推荐
资源详情
资源评论
收起资源包目录
北京旅游知识图谱程序.zip (5个子文件)
SJT-code
entity2class.py 2KB
graph_model3_new.py 44KB
graph_model3_allfile.py 43KB
e_v_jiagu_new.py 9KB
model3_program
graph_model3_v2.py 46KB
共 5 条
- 1
资源评论
JJJ69
- 粉丝: 6232
- 资源: 5778
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功