Example #1
0
def events2jeni(uuid,confidence_analysis=0.3):
    res=[]
    try:
        exp=Experience.query.filter(Experience.uuid==uuid).one()
    except NoResultFound:
        return json.dumps({
                    'status': 'error',
                    'message': 'Not experience found'
        })
    
    try:
        es=Event.query.filter(Event.experience_id==exp.id).order_by(Event.order.asc()).all()
        loc=None
        locs=[]
        preds=[]
        for e in es:
            i=e.infos[0]
            pred_=json.loads(i.json)
            print(pred_,e.type.name)
            preds.append(pred_)
            print("--------------->",res)
            # Figure out place 
            if e.type.name=='action':
                if loc==None and pred_['type']=='start':
                    res.append([])
                    loc=golfred.kb_getlocgspace(g,pred_['command']['args'][0])
                    locs.append(loc)
                elif loc and pred_['type']=="move":
                    loc_=golfred.kb_getlocgspace(g,pred_['command']['args'][0])
                    if not loc.eq(loc_):
                        loc=loc_
                        locs.append(loc)
                        res.append([])
                elif loc and pred_['type']=="fail":
                    if pred_['command']['name']=='move':
                        res[-1].pop()
                        locs.pop()
                        loc=locs[-1]
                elif loc and pred_['type']=='finish':
                    res.append([])

            # Generate text
            # START or new spatial reference
            if e.type.name=='action' and pred_['type']=='start':
                c=None
                semantics = golfred.kb_generatesemantics(g,pred_['command']['name'])
                loc__=golfred.kb_getglabel(g,pred_['command']['args'][0])
                f=semantics.format(str(loc__))
                res[-1].append((f,c))
                continue
             
            if e.type.name=='action' and pred_['type']=='finish':
                #t,c=golfred.kb_drawtemplate(g,'connect',res[-1],C="text")
                #res[-1].append((t,c))
                #t,c=golfred.kb_drawtemplate(g,'finish',res[-1])
                #loc__=golfred.kb_getlocname(g,pred_['command']['args'][0])
                #res[-1].append((t.format(**{'loc':loc__}),c))
                semantics = golfred.kb_generatesemantics(g,pred_['command']['name'])
                loc__=golfred.kb_getglabel(g,pred_['command']['args'][0])
                f=semantics.format(str(loc__))
                res[-1].append((f,c))
               
                continue
            if e.type.name=='action' and pred_['type']=="fail":
                print("------> hello")
                #t,c=golfred.kb_drawtemplate(g,'fail',res[-1])
                #res[-1].append((t,c))
                if e.type.name=='action':
                    #t,c=golfred.kb_drawtemplate(g,preds[-2]['command']['name'],res[-1])
                    semantics = golfred.kb_generatesemantics(g,pred_['command']['name'])
                    semantics+= " d:fail(e2)"
                    miss_loc=golfred.kb_getlocgspace(g,preds[-2]['command']['args'][0])
                    f=semantics.format(*[miss_loc])
                    res[-1].append((f,c))
       
            if len(res)>0 and len(res[-1])==0:
                #t,c=golfred.kb_drawtemplate(g,'connect',res[-1],C="text")
                #res[-1].append((t,c))
                if e.type.name=='action':
                    semantics=golfred.kb_generatesemantics(g,pred_['command']['name'])
                    loc__=golfred.kb_getlocgspace(g,pred_['command']['args'][0])
                    f=semantics.format(str(loc__))
                    res[-1].append((f,c))
 
                    #loc__=golfred.kb_getlocname(g,loc)
                    #if t:
                    #    f=t.format(*[loc__])+' to '+golfred.kb_getlocname(g,preds[-1]['command']['args'][0])
                    #    res[-1].append((f,c))
                continue 
                       
                #res[-1].append(("but could't",None))
                continue 
            if e.type.name=='action' and pred_['type']=="move":
                #res[-1].append(("then",None))
                c=None
                if e.type.name=='action':
                    semantics = golfred.kb_generatesemantics(g,pred_['command']['name'])
                    loc__=golfred.kb_getglabel(g,pred_['command']['args'][0])
                    f=semantics.format(str(loc__))
                    res[-1].append((f,c))
                continue

            if e.type.name=='action' and pred_['type']=="manipulation":
                if e.type.name=='action':
                    semantics = golfred.kb_generatesemantics(g,pred_['command']['name'])
                    f=semantics.format(*pred_['command']['args'])
                    res[-1][-1]=res[-1][-1][0].replace(u'e2',u'e1'),None
                    res[-1][-1]=res[-1][-1][0]+' z:before(e1 e2) ',None
                    res[-1][-1]=res[-1][-1][0]+f,None
                continue 

            if len(res)>0 and e.type.name=='visual':
                for i in e.infos:
                    pred_=json.loads(i.json)
         
                    t,c=golfred.kb_drawtemplate(g,i.type.name,res[-1])
                    if t:
                        ana=False
                        #if i.type.name=='analysis' and i.type.name=="read":
                        #    ana=True
                        #    if pred_['description']['captions'][0]['confidence']>confidence_analysis:
                        #    else: 
                        #        continue
                    
                        #if i.type.name=='analysis':
                        #    ana=True
                        #    if pred_['description']['captions'][0]['confidence']>confidence_analysis:
                        #        f=t.format(*[pred_['description']['captions'][0]['text']])
                        #    else: 
                        #        continue
                        if i.type.name=="read":
                            text=cleanning(pred_['text'])
                            semantics="ik:i(j) i:Experiencer(e2 j) a:visual(u) u:{0}({1}) i:Stimulus(e2 u) i:see(e2)".format(
                                    text,
                                    abbreviation(text))
                            res[-1].append((semantics,c))
            else:
                pass
        #print(res)   
        f=open("jeni/"+uuid+'.jeni','w')
        c=0
        for segment in res:
            for semantics,n in segment:
                print("id_{0}".format(c),file=f)
                print("semantics:[{0}]".format(semantics),file=f)
                print("",file=f)
                c+=1
        f.close()
        phrases=golfred.jeni(uuid)
        print(phrases)
        return json.dumps([[x for x,y in b] for  b in res])
    except NoResultFound:
        return json.dumps({
                    'status': 'error',
                    'message': 'Not events found'

        })
Example #2
0
def events2structure(uuid,confidence_analysis=0.3):
    res=[]
    try:
        exp=Experience.query.filter(Experience.uuid==uuid).one()
    except NoResultFound:
        return json.dumps({
                    'status': 'error',
                    'message': 'Not experience found'
        })
    
    try:
        es=Event.query.filter(Event.experience_id==exp.id).order_by(Event.order.asc()).all()
        loc=None
        locs=[]
        preds=[]
        for e in es:
            i=e.infos[0]
            pred_=json.loads(i.json)
            preds.append(pred_)
            # Figure out place 
            if e.type.name=='action':
                if loc==None and pred_['type']=='start':
                    res.append([])
                    loc=golfred.kb_getlocspace(g,pred_['command']['args'][0])
                    locs.append(loc)
                elif loc and pred_['type']=="move":
                    loc_=golfred.kb_getlocspace(g,pred_['command']['args'][0])
                    if not loc.eq(loc_):
                        loc=loc_
                        locs.append(loc)
                        res.append([])
                elif loc and pred_['type']=="fail":
                    if pred_['command']['name']=='move':
                        res[-1].pop()
                        locs.pop()
                        loc=locs[-1]
                elif loc and pred_['type']=='finish':
                    res.append([])

            # Generate text
            # START or new spatial reference
            if e.type.name=='action' and pred_['type']=='start':
                t,c=golfred.kb_drawtemplate(g,'start',res[-1])
                loc__=golfred.kb_getlocname(g,loc)
                res[-1].append((t.format(**{'loc':loc__}),c))
                continue
            if e.type.name=='action' and pred_['type']=='finish':
                t,c=golfred.kb_drawtemplate(g,'connect',res[-1],C="text")
                res[-1].append((t,c))
                t,c=golfred.kb_drawtemplate(g,'finish',res[-1])
                loc__=golfred.kb_getlocname(g,pred_['command']['args'][0])
                res[-1].append((t.format(**{'loc':loc__}),c))
                continue
       
            elif len(res)>0 and len(res[-1])==0:
                t,c=golfred.kb_drawtemplate(g,'connect',res[-1],C="text")
                res[-1].append((t,c))
                if e.type.name=='action':
                    t,c=golfred.kb_drawtemplate(g,pred_['command']['name'],res[-1])
                    loc__=golfred.kb_getlocname(g,loc)
                    if t:
                        f=t.format(*[loc__])+' to '+golfred.kb_getlocname(g,preds[-1]['command']['args'][0])
                        res[-1].append((f,c))
                continue 
            if e.type.name=='action' and pred_['type']=="fail":
                t,c=golfred.kb_drawtemplate(g,'fail',res[-1])
                res[-1].append((t,c))
                if e.type.name=='action':
                    t,c=golfred.kb_drawtemplate(g,preds[-2]['command']['name'],res[-1])
                    miss_loc=golfred.kb_getlocspace(g,preds[-2]['command']['args'][0])
                    loc__=golfred.kb_getlocname(g,miss_loc)
                    if t:
                        f=t.format(*[loc__])
                        res[-1].append((f,c))
                        
                res[-1].append(("but could't",None))
                continue 
            if e.type.name=='action' and pred_['type']=="move":
                res[-1].append(("then",None))
                if e.type.name=='action':
                    t,c=golfred.kb_drawtemplate(g,pred_['command']['name'],res[-1])
                    if t:
                        loc__=golfred.kb_getlocname(g,pred_['command']['args'][0])
                        f=t.format(str(loc__))
                        res[-1].append((f,c))
                continue 


            if e.type.name=='action' and pred_['type']=="manipulation":
                if e.type.name=='action':
                    t,c=golfred.kb_drawtemplate(g,pred_['command']['name'],res[-1])
                    if t:
                        f=t.format(*pred_['command']['args'])
                        res[-1].append((f,c))
                continue 

            if len(res)>0 and e.type.name=='visual':
                for i in e.infos:
                    pred_=json.loads(i.json)
         
                    t,c=golfred.kb_drawtemplate(g,i.type.name,res[-1])
                    if t:
                        ana=False
                        if i.type.name=='analysis':
                            ana=True
                            if pred_['description']['captions'][0]['confidence']>confidence_analysis:
                                f=t.format(*[pred_['description']['captions'][0]['text']])
                            else: 
                                continue
                        elif i.type.name=="read":
                            if not ana:
                                f="there "+t.format(pred_['text'])
                            else:
                                f=t.format(pred_['text'])
                        res[-1].append((f,c))
            else:
                pass
        print(res)   
        return json.dumps([[x for x,y in b] for  b in res])
    except NoResultFound:
        return json.dumps({
                    'status': 'error',
                    'message': 'Not events found'

        })