Exemplo n.º 1
0
    def file2graph(self, kg: KG, filePath, format='nt'):
        g = rdflib.Graph()
        g.parse(filePath, format=format)

        relation_triples, attribute_triples = set(), set()

        # 循环遍历图谱中每一个三元组
        for subj, pred, obj in g:
            # 空图谱检查
            if (subj, pred, obj) not in g:
                raise Exception("It better be!")

            if isinstance(obj, rdflib.term.URIRef):
                triple = (str(subj).split('/')[-1], str(pred).split('/')[-1],
                          str(obj).split('/')[-1])
                # triple = (str(subj).split('/')[-1], str(pred).split('/')[-1], str(obj).split('/')[-1])
                relation_triples.add(triple)
            elif isinstance(obj, rdflib.term.Literal):
                triple = (str(subj).split('/')[-1], str(pred).split('/')[-1],
                          str(obj).split('/')[-1])
                # triple = (str(subj).split('/')[-1], str(pred).split('/')[-1], str(obj).split('/')[-1])
                attribute_triples.add(triple)

        kg.add_relations(relation_triples)
        kg.add_attributes(attribute_triples)
Exemplo n.º 2
0
 def __init__(self):
     self.__score = 0
     self.__metaPaths = []
     self.__graph = KG()
     self.__Aconstraint = set()
Exemplo n.º 3
0
class MeatGraph(object):
    def __init__(self):
        self.__score = 0
        self.__metaPaths = []
        self.__graph = KG()
        self.__Aconstraint = set()

    def __lt__(self, other):
        return self.score < other.score

    def __gt__(self, other):
        return self.score > other.score

    def __ge__(self, other):
        return self.score > other.score

    def __le__(self, other):
        return self.score <= other.score

    def __hash__(self):
        return hash(self.__score)

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__score == other.score
        else:
            return False

    @property
    def metaPaths(self):
        return self.__metaPaths

    @property
    def graph(self):
        return self.__graph

    def addMetaPath(self, metaPath):
        if isinstance(metaPath, MetaPath):
            self.__metaPaths.append(metaPath)
            self.score = self.merge(self.__metaPaths)
        elif isinstance(metaPath, MeatGraph):
            for path in metaPath.metaPaths:
                self.__metaPaths.append(path)
            self.score = self.merge(self.__metaPaths)
        else:
            print('It is not a MeatGraph/MetaPath object')

    def merge(self, paths):
        allP = set()
        for p in paths:
            allP |= set(path2Triples(p))
            if len(p) > 1:
                for t in set(path2Triples(p)):
                    self.__graph.add_relations({
                        (t.head.Class, t.relation.Class, t.tail.Class)
                    })

        pathProb = 1
        for T in allP:
            pathProb *= T.prob

        return pathProb

    @property
    def score(self):
        return self.__score

    @score.setter
    def score(self, score):
        self.__score = score

    @property
    def Aconstraint(self):
        return self.__Aconstraint

    # cst: (entity, attribution, value)
    def addAconstraint(self, cst: tuple):
        self.__Aconstraint.add(cst)
Exemplo n.º 4
0

# r1 = ReadRDFFile()
# kg = KG()
# # r1.file2graph(kg, "D:/ws/aa/KeywordSearch/dataSet/CTIKG/CyberSecurity.nt")
# r1.file2graph(kg, "D:/ws/aa/KeywordSearch/dataSet/CTIKG/CTIKG.nt")
#
#
# # Key = NTNav(kg, ["WannaCry","Attack Pattern", "platform"])
# print("entities_set" + str(sys.getsizeof(kg.entities_set)))
# print("relations_set" + str(sys.getsizeof(kg.relations_set)))
# print("rev_hr_dict" + str(sys.getsizeof(kg.rev_hr_dict)))
# print("rev_rt_dict" + str(sys.getsizeof(kg.rev_rt_dict)))
# Key = NTNav(kg, ["WannaCry","Attack Pattern"])
# # Key = NTNav(kg, ["BlackEnergy","system","Vulnerability"])
# # Key = NTNav(kg, ["BlackEnergy","ApplicablePlatform","Weakness"])
# Key.search()



kg = KG()
r = ReadRDFFile()
r.file2graph(kg, "D:/ws/aa/KeywordSearch/dataSet/Yago4/yago-wd-class.nt")
r.file2graph(kg, "D:/ws/aa/KeywordSearch/dataSet/Yago4/yago-wd-full-types.nt")
r.file2graph(kg, "D:/ws/aa/KeywordSearch/dataSet/Yago4/yago-wd-facts.nt")
# print("entities_set" + str(sys.getsizeof(kg.entities_set)))
# print("relations_set" + str(sys.getsizeof(kg.relations_set)))
# print("rev_hr_dict" + str(sys.getsizeof(kg.rev_hr_dict)))
# print("rev_rt_dict" + str(sys.getsizeof(kg.rev_rt_dict)))
Key = NTNav(kg,["Amit Singhal","Google"])
Key.search()