Пример #1
0
 def __init__(self, s):
     s = s.split(':-')
     left = s[1].split('&')
     right = s[0]
     self.left = []
     self.right = Relation(right)
     for i in left:
         temp = Relation(i)
         self.left.append(temp)
Пример #2
0
class Infer:
    #Prolog
    #VD: husband(Person,Wife):-male(Person);married(Person,Wife)
    #left -> right
    def __init__(self, s):
        s = s.split(':-')
        left = s[1].split('&')
        right = s[0]
        self.left = []
        self.right = Relation(right)
        for i in left:
            temp = Relation(i)
            self.left.append(temp)

    def generate(self, KB):
        #Chua cac gia tri của cac bien ben trai co the thay the vao trong infer
        temp = []
        #Chua ten cac bien ben trai
        temp1 = []
        for i in self.left:
            temp1 += i.getDatas()
            #Luu nhung quan he tuong tu voi i
            temp2 = []
            for j in KB:
                if i.isCompatible(j):
                    temp2.append(j.getDatas())
            temp.append(temp2)

        #Intertools.product(temp) tao mang ket hop cac mang trong temp lai voi nhau
        #VD: temp = [[1,2],[3,4]]
        #Intertools.product tra ve [[1,3],[1,4],[2,3],[2,4]]
        for x in itertools.product(*temp):
            temp2 = []
            for i in x:
                temp2 = temp2 + i
            #Xet xem bo x co phu hop voi temp1 hay khong?
            if check(temp2, temp1):
                data = []
                temp3 = self.right.getDatas()
                for j in temp3:
                    index = temp1.index(j)
                    data.append(temp2[index])
                temp4 = Relation("Relation(Data)")
                temp4.setDatas(data)
                temp4.setName(self.right.getName())
                if temp4 in KB:
                    continue
                return temp4
        return False

    def tostring(self):
        string_left = []
        for i in self.left:
            string_left.append(i.toString())
        string_left = ' ^ '.join(string_left)
        return string_left + ' -> ' + self.right.toString()
 def from_file(cls, filepath):
     relations = []
     lines = open(filepath).readlines()
     for line in lines:
         j = json.loads(line)
         r = Relation(**j)
         r.entity1 = Entity(**r.entity1)
         r.entity2 = Entity(**r.entity2)
         relations.append(r)
     return RelationCollection(relations)
Пример #4
0
 def readLibrary(fl):
     err_str = ''
     war_str = ''
     list_obj = []
     list_rel = []
     ext_obj = []
     ext_rel = []
     dict_ft = defaultdict(list)
     try:
         with open(Object.folder + fl) as f:
             #with object_pairs_hook method, we can check "Error 07" at the same level
             target = json.loads(f.read(), object_pairs_hook=Object.dupe_checking_hook)
         reldict = defaultdict(list)
         err_str, war_str = Object.val_lib(fl, target, target, True, list_obj,
                                           list_rel, reldict, ext_obj, ext_rel, dict_ft)
         #pprint.pprint(reldict)
         #print('\n'*2)
         err_str += Object.cycle_test(reldict, [], '', True)
         err_str += Object.notdefined(list_obj, list_rel, ext_obj, ext_rel, dict_ft)
         if not err_str:
             for key in target.keys():
                 if key == "objects":
                     for objectName in target[key].keys():
                         #We add all the objects contained in "objectName" into global dictionary "Object.
                         #objects" and their name into local "objects" list to give a tree structure
                         obj = Object(objectName, target[key][objectName])
                         #add object into Object.objects
                         Object.addObject(objectName, obj)
                         #add object into Object.objectsLib
                         if not Object.comp:
                             Object.objectsLib[objectName] = obj
                         else:
                             Object.objectsLib2[objectName] = obj
                 elif key == "relations":
                     for relationName in target[key].keys():
                         relation = Relation(relationName, target[key][relationName])
                         Relation.addRelation(relationName, relation)
                         Object.addRelation(relationName, relation)
                         if not Object.comp:
                             Object.relationsLib[relationName] = relation
                         else:
                             Object.relationsLib2[relationName] = relation
     except IOError:
         err_str += 'Error 02: library file is not found!\n'
         list_obj = ['Error 02']
     return [err_str, war_str, list_obj, list_rel]
Пример #5
0
    def start(self):

        ## summarize
        self.sum = Summarize(self.sentences, self.num_cluster)
        self.sum.save(self.collection, self.oid)

        ## time
        self.time = Time(self.sentences, self.sum.clusters)
        self.time.save(self.collection, self.oid)

        ## keyword
        self.keyword = Tf_Idsf(self.sentences)
        self.keyword.save(self.collection, self.oid)

        ## relation

        self.relation = Relation(self.sentences, self.mid)
        self.relation.save(self.collection, self.oid)
Пример #6
0
 def load_relations(self):
     # load relation from file
     relation_data = list(open("../data/RE/relation2id.txt").readlines())
     relation_data = [s.split() for s in relation_data]
     for relation in relation_data:
         r = Relation(relation[0], int(relation[1]))
         self.relations[relation[0]] = r
     for r in self.relations:
         self.relations[r].generate_vector(len(self.relations))
     print("RelationTotal: " + str(len(self.relations)))
Пример #7
0
class Analyzer():
    def __init__(self, mid):

        conn = pymongo.MongoClient('13.209.73.233', 27017)
        db = conn.get_database('test')
        self.collection = db.get_collection('meets')
        self.mid = mid
        self.oid = ObjectId(self.mid)

        ## get talk
        result = self.collection.find({"_id": self.oid}, {
            "_id": False,
            "talk": True,
            "num_cluster": True
        })
        if result[0]:
            talk = sorted(result[0]['talk'], key=lambda x: x[1])
            self.sentences = [Sentence(i, t) for i, t in enumerate(talk)]
            self.num_cluster = result[0]['num_cluster']
            print(self.num_cluster)
            self.error = False
        else:
            self.error = True

    def start(self):

        ## summarize
        self.sum = Summarize(self.sentences, self.num_cluster)
        self.sum.save(self.collection, self.oid)

        ## time
        self.time = Time(self.sentences, self.sum.clusters)
        self.time.save(self.collection, self.oid)

        ## keyword
        self.keyword = Tf_Idsf(self.sentences)
        self.keyword.save(self.collection, self.oid)

        ## relation

        self.relation = Relation(self.sentences, self.mid)
        self.relation.save(self.collection, self.oid)
Пример #8
0
    def generate(self, KB):
        #Chua cac gia tri của cac bien ben trai co the thay the vao trong infer
        temp = []
        #Chua ten cac bien ben trai
        temp1 = []
        for i in self.left:
            temp1 += i.getDatas()
            #Luu nhung quan he tuong tu voi i
            temp2 = []
            for j in KB:
                if i.isCompatible(j):
                    temp2.append(j.getDatas())
            temp.append(temp2)

        #Intertools.product(temp) tao mang ket hop cac mang trong temp lai voi nhau
        #VD: temp = [[1,2],[3,4]]
        #Intertools.product tra ve [[1,3],[1,4],[2,3],[2,4]]
        for x in itertools.product(*temp):
            temp2 = []
            for i in x:
                temp2 = temp2 + i
            #Xet xem bo x co phu hop voi temp1 hay khong?
            if check(temp2, temp1):
                data = []
                temp3 = self.right.getDatas()
                for j in temp3:
                    index = temp1.index(j)
                    data.append(temp2[index])
                temp4 = Relation("Relation(Data)")
                temp4.setDatas(data)
                temp4.setName(self.right.getName())
                if temp4 in KB:
                    continue
                return temp4
        return False
Пример #9
0
    def dbProcessSchemes(self):
        relationList = []
        for table in self.tableNames:
            relObj = Relation()
            relObj.Name = table
            for columns in self.insp.get_columns(table):
                attribute_object = Attribute(columns['name'])
                attribute_object.Type = columns['type']
                attribute_object.Autoincrement = columns['autoincrement']
                attribute_object.Nullable = columns['nullable']
                relObj.Attributes.append(attribute_object)

            #attribute_object = Attribute()
            #attribute_object.Name = columns['name']

            key = Key()
            for att in self.insp.get_primary_keys(table):
                attribute_object = Attribute(att)
                key.Attributes.append(attribute_object)
            relObj.Keys.append(key)

            #relObj.FDs = self.getFunctionalDependencies(relation)
            relationList.append(relObj)
        return relationList
Пример #10
0
    def writeAnswer(self,questions):
        # &: and
        # |: or
        # ~: not
        fo=input('Nhập tên  file output: ')   
        f=open(fo,'w')
        ordinal_number = 0
        for i in questions:
            ordinal_number+=1
            temp=i.split('?-')
            f.write(str(ordinal_number))
            f.write('.')
            # f.write(temp[0])
            f.write('\n')
            temp1 = temp[1].split('?')
            q = temp1[-1]
            f.write('?-')
            f.write(''.join(temp[1]))
            f.write('\n')
            if len(temp1)>1:
                var = temp1[0:-1]
                temp2=temp1[-1].split('&')
                #Luu ket qua cua tung vi tu
                res_temp=[]
                #Luu cac bien
                temp4 = []
                #Giai ket qua cua tung vi tu
                for k in temp2:
                    check_result1 = True
                    temp3=Relation(k)
                    for x in temp3.getDatas():
                        temp4.append(x)
                    #Luu cac ket qua cua 1 vi tu
                    res_temp1=[]
                    for j in self.KB:
                        if temp3.getName() == j.getName():
                            index =0
                            n = len(temp3.getDatas())
                            while index < n:
                                if temp3.getData(index) in var:
                                    index+=1
                                    continue
                                elif temp3.getData(index) == j.getData(index):
                                    index+=1
                                else:
                                    break
                            if index == n:
                                res_temp1.append(j.getDatas())
                    if len(res_temp1) ==0:
                        check_result1 = False
                        break
                    else:
                        res_temp.append(res_temp1)
                if check_result1 == False:
                     f.write("No result!!!")
                     f.write('\n')
                else:
                    if len(res_temp) >1:
                        check_result2 = False
                        #Ket hop nhung ket qua lai voi nhau tao thanh 1 bo
                        for x in itertools.product(*res_temp):
                            temp5= []
                            for y in x:
                                temp5.extend(y)
                            #Bo nao thoa dieu kien thi ghi ket qua bo do
                            if check(temp5,temp4):
                                check_result2 = True
                                for res_var in var:
                                    index = temp4.index(res_var) 
                                    f.write(res_var +' = ' + temp5[index])
                                    f.write('\t')
                                f.write('\n')
                        if check_result2 == False:
                            f.write("No result!!!")
                            f.write('\n')
                    else:
                        for x in res_temp[0]:
                            if check(x,temp4):
                                for res_var in var:
                                    index = temp4.index(res_var)                
                                    f.write(res_var +' = ' + x[index])
                                    f.write('\t')
                                f.write('\n')
            else:
                s = split_string(q)
                for i in range(len(s)):
                    if s[i] != '&' and s[i] != '|' and s[i] != '[' and s[i] != ']':
                        temp1 = Relation(s[i])
                        res_temp = False
                        for j in self.KB:
                            if temp1 ==j:
                                res_temp=True
                        if res_temp == True:
                            s[i]='1'
                        else:
                            s[i]='0'
                res = eval(''.join(s))
                res_write = ''
                if res == 1:
                    res_write = 'True'
                else:
                    res_write = 'False'
                f.write(res_write)
                f.write('\n')

        print("Xu ly query xong!!!")
        f.close()
Пример #11
0
from Program import Program
from Relation import Relation
from Infer import Infer
#Doc file input
fp = input('Nhập tên  file input: ')
f = open(fp, 'r')
f1 = f.read().splitlines()
KB = []
I = []
questions = []
flag = 0
for x in f1:
    if x == '*':
        flag += 1
        continue
    if flag == 0:
        temp = Relation(x)
        KB.append(temp)
    elif flag == 1:
        temp = Infer(x)
        I.append(temp)
    else:
        questions.append(x)
f.close()

main = Program(KB, I)
main.run()
#main.printKB()
main.writeAnswer(questions)
Пример #12
0
Cntrs = Countries()

Cntrs.add(cntr1)
Cntrs.add(cntr2)
Cntrs.add(cntr3)

Cts = Cities()

Cts.add(ct1)
Cts.add(ct2)
Cts.add(ct3)
Cts.add(ct4)
Cts.add(ct5)
Cts.add(ct6)

ER = Relation()

ER.add(0, 0, Cntrs, Cts)
ER.add(0, 1, Cntrs, Cts)
ER.add(0, 2, Cntrs, Cts)
ER.add(1, 3, Cntrs, Cts)
ER.add(2, 4, Cntrs, Cts)
ER.add(2, 5, Cntrs, Cts)

with open('Relation.pickle', 'wb') as f:
    pickle.dump(ER, f)

with open('Cities.pickle', 'wb') as f:
    pickle.dump(Countries, f)

with open('Countries.pickle', 'wb') as f:
Пример #13
0
    exit(0)
try:
    test = int(sys.argv[1])
except ValueError:
    print "Usage : python test.py TEST_NUMBER"
    exit(0)

# create an SQLite test database
DB_NAME = 'test_db'
create_database(DB_NAME)
# get attributes from database
emp = SQLiteRelation(DB_NAME, 'emp')
dept = SQLiteRelation(DB_NAME, 'dept')

# manually create attributes, SQL can't be generated but errors will be checked
man_rel = Relation([Attribute('name', 'TEXT'), Attribute('age', 'INTEGER')])
if test == 0:
    print str_attributes(man_rel.getAttributes())

# test SQLiteRelation
if test == 1:
    rel = emp
if test == 2:
    rel = dept

# Test SelectAttribute
if test == 10:
    # error : attribute does not exists
    rel = SelectAttribute('ename', 'nonexistant', emp)
if test == 11:
    # error : not comparable attributes
Пример #14
0
import copy
from prettytable import PrettyTable
from GoodShit import GoodShit
from Relation import Relation

# Relacje - przyklad dla zadania ze slajdow

R1 = Relation(
    'R1', 1000, {
        'rows_identifiers': ['SIZE', 'VAL'],
        'columns_identifiers': ['Property', 'A', 'B']
    })
R1.populate_table([[4, 4], [200, 50]])

R2 = Relation(
    'R2', 1000, {
        'rows_identifiers': ['SIZE', 'VAL'],
        'columns_identifiers': ['Property', 'A', 'B']
    })
R2.populate_table([[4, 4], [40, 100]])

R3 = Relation(
    'R3', 2000, {
        'rows_identifiers': ['SIZE', 'VAL'],
        'columns_identifiers': ['Property', 'B', 'D']
    })
R3.populate_table([[4, 4], [400, 100]])

R4 = Relation(
    'R4', 1000, {
        'rows_identifiers': ['SIZE', 'VAL'],
Пример #15
0
def loadRelationFile(hin, relationFileName, startEntityType, endEntityType):
    # Judge whether the entity type exists in the HIN
    if startEntityType not in hin['EntityTypes']:
        hin['EntityTypes'][startEntityType] = dict()
    if endEntityType not in hin['EntityTypes']:
        hin['EntityTypes'][endEntityType] = dict()

    # Initialize relations list
    startTypeToEndType = startEntityType + '-' + endEntityType
    endTypeToStartType = endEntityType + '-' + startEntityType
    hin['RelationTypes'][startTypeToEndType] = []
    hin['RelationTypes'][endTypeToStartType] = []
    hinStartToEndList = hin['RelationTypes'][startTypeToEndType]
    hinEndToStartList = hin['RelationTypes'][endTypeToStartType]

    hinRelationList = hin['Relations']

    # Start reading data file
    with open(DATASET_DIR + relationFileName, 'r') as relationFile:

        while True:
            relationLine = relationFile.readline()
            if not relationLine:
                break

            relationLine = relationLine.strip()
            relation = relationLine.split(' ')

            # Data sensitive -> type ...
            # Load the start entity and the end entity
            startEntityId = int(relation[0])
            endEntityId = relation[1]
            if endEntityId.isdigit():
                endEntityId = int(endEntityId)
            weight = None

            # The third column is regarded as the weight
            if len(relation) > 2:
                weightStr = relation[2]
                # Judge whether the type of the weight should be float or int
                if '.' in weightStr:
                    weight = float(weightStr)
                else:
                    weight = int(weightStr)

            # Entity location
            tmpStartEntityInfo, tmpStartEntityIndex = locateEntity(
                hin, startEntityId, startEntityType)
            tmpEndEntityInfo, tmpEndEntityIndex = locateEntity(
                hin, endEntityId, endEntityType)
            tmpStartEntity = tmpStartEntityInfo.entity
            tmpEndEntity = tmpEndEntityInfo.entity

            # Construct a relation and its reverse one
            tmpRelation = Relation(tmpStartEntity, tmpEndEntity, weight)
            hinRelationList.append(tmpRelation)
            tmpRelationIndex = len(hinRelationList) - 1
            tmpReverseRelation = Relation(tmpEndEntity, tmpStartEntity)
            hinRelationList.append(tmpReverseRelation)
            tmpReverseRelationIndex = len(hinRelationList) - 1

            # Add the relations in entity and HIN
            tmpStartEntityInfo.addRelation(tmpRelation, tmpRelationIndex,
                                           False)
            tmpEndEntityInfo.addRelation(tmpRelation, tmpRelationIndex, True)
            hinStartToEndList.append(tmpRelationIndex)

            tmpStartEntityInfo.addRelation(tmpReverseRelation,
                                           tmpReverseRelationIndex, True)
            tmpEndEntityInfo.addRelation(tmpReverseRelation,
                                         tmpReverseRelationIndex, False)
            hinEndToStartList.append(tmpReverseRelationIndex)

        relationFile.close()