Exemple #1
0
    def _phase3 ( self , phr , fbs ):

        """
        create a goal at next position for the right branch of each
        2-branch splitting syntax rule having a left branch going to
        the type of the current phrase

        arguments:
            self  -
            phr   - current phrase
            fbs   - its compounded feature bits
        """

        rls = self.gtb.splits[phr.krnl.typx]
        po = phr.krnl.posn
        if po < 0 : po = 0                       # position check needed for ... phrase type
        gb = self.gbits[po]
        np = self.wordno + 1
#       print ( '> PHASE 3' , phr )
#       print ( '> PHASE 3 at' , po , '=' , len(rls) , 'rules, gb=' , gb.hexadecimal() )
        for r in rls:
            nt = r.styp
#           print ( 'type=' , nt , 'dm=' , self.gtb.mat.dm[nt].hexadecimal() )
            if self.gtb.mat.derivable(nt,gb):    # rule is applicable at current position?
#               print ( 'left test=' , r.ltfet )
                if ellyBits.check(fbs,r.ltfet):  # phrase has required features for rule?
                    g = self.makeGoal(r,phr)     # allocate new goal
#                   print ( 'at ' + str(np) + ',' , g )
                    if np == len(self.goal):     # check for end of goal array
                        self.addGoalPositions()  # add new positions when needed
                    self.goal[np].append(g)      # add it to next position
                    self.gbits[np].set(g.cat)    # and set bit for type of goal
Exemple #2
0
    def _phase3 ( self , phr , fbs ):

        """
        create a goal at next position for the right branch of each
        2-branch splitting syntax rule having a left branch going to
        the type of the current phrase

        arguments:
            self  -
            phr   - current phrase
            fbs   - its compounded feature bits
        """

        rls = self.gtb.splits[phr.krnl.typx]
        po = phr.krnl.posn
        if po < 0 : po = 0                       # position check needed for ... phrase type
        gb = self.gbits[po]
        np = self.wordno + 1
#       print '> PHASE 3' , phr
#       print '> PHASE 3 at' , po , '=' , len(rls) , 'rules, gb=' , gb.hexadecimal()
        for r in rls:
            nt = r.styp
#           print 'type=' , nt , 'dm=' , self.gtb.mat.dm[nt].hexadecimal()
            if self.gtb.mat.derivable(nt,gb):    # rule is applicable at current position?
#               print 'left test=' , r.ltfet
                if ellyBits.check(fbs,r.ltfet):  # phrase has required features for rule?
                    g = self.makeGoal(r,phr)     # allocate new goal
#                   print 'at ' + str(np) + ',' , g
                    if np == len(self.goal):     # check for end of goal array
                        self.addGoalPositions()  # add new positions when needed
                    self.goal[np].append(g)      # add it to next position
                    self.gbits[np].set(g.cat)    # and set bit for type of goal
Exemple #3
0
    def _phase2(self, phr, fbs):
        """
        create phrase for each 1-branch syntax rule going to
        the type of the current phrase

        arguments:
            self  -
            phr   - current phrase
            fbs   - its compounded feature bits

        exceptions:
            ParseOverflow
        """

        po = phr.krnl.posn
        gb = self.gbits[po]
        rls = self.gtb.extens[phr.krnl.typx]
        #       print '> PHASE 2 at' , po , '=' , len(rls) , 'rules, gb=' , gb.hexadecimal()
        for r in rls:
            nt = r.styp
            #           print 'type=' , nt , 'dm=' , self.gtb.mat.dm[nt].hexadecimal()
            if self.gtb.mat.derivable(
                    nt, gb):  # rule applicable at current position?
                #               print 'phr fet=' , fbs
                #               print 'rul fet=' , r.utfet
                if ellyBits.check(
                        fbs,
                        r.utfet):  # phrase has required features for rule?
                    phn = self.makePhrase(
                        po, r)  # make new phrase if checks succeed
                    if phn == None:
                        break
                    phn.krnl.lftd = phr  # current phrase is part of new one
                    phn.ntok = phr.ntok  # set token count for new phrase
                    phn.lens = phr.lens  # set token count for new phrase
                    #                   print 'phn=' , phn
                    #                   print 'phr=' , phr
                    self._score(phn)  # compute bias score
                    rF = phn.krnl.synf.test(0)
                    lF = phn.krnl.synf.test(1)
                    if lF or rF:  # inherit features from current phrase?
                        #                       print 'lF or rF'
                        phn.krnl.synf.combine(phr.krnl.synf)
                        if lF:
                            phn.krnl.synf.unset(0)
                        else:
                            phn.krnl.synf.unset(1)
#                       print 'TO' , str(phn.krnl.synf)
                    phn.krnl.synf.reset(
                        r.sftr)  # reset selected inherited bits
                    #                   print 'phn=' , phn
                    self.enqueue(phn)  # save new phrase to ramify (phase 2)
Exemple #4
0
    def _phase1(self, phr, fbs):
        """
        check for goals at current position for current phrase type
        and create new phrase for each match

        arguments:
            self  -
            phr   - current phrase
            fbs   - its compounded feature bits

        exceptions:
            ParseOverflow
        """

        po = phr.krnl.posn
        gls = self.goal[po]
        #       print '> PHASE 1 at' , po , '=' , len(gls) , 'goals'
        for g in gls:
            #           print 'goal of' , g.cat
            if phr.krnl.typx == g.cat:
                r = g.rul
                #               print 'r=' , r.seqn , r.rtfet
                if ellyBits.check(fbs, r.rtfet):
                    phx = g.lph  # phrase that generated pertinent goal
                    phn = self.makePhrase(phx.krnl.posn,
                                          r)  # new phrase to satisfy goal
                    if phn == None:
                        break
                    phn.krnl.lftd = phx  # goal phrase is left part of new one
                    phn.krnl.rhtd = phr  # current phrase is right part
                    phn.ntok = phx.ntok + phr.ntok  # set token count for new phrase
                    phn.lens = phx.lens + phr.lens  # and char count
                    self._score(phn)  # compute bias score
                    rF = phn.krnl.synf.test(0)
                    lF = phn.krnl.synf.test(1)
                    if lF:  # inherit features from previous phrase?
                        #                       print '*l set' , str(phx.krnl.synf)
                        phn.krnl.synf.combine(phx.krnl.synf)
                        phn.krnl.synf.unset(0)
#                       print 'TO' , str(phn.krnl.synf)
                    if rF:  # inherit features from ramified phrase?
                        #                       print '*r set' , str(phx.krnl.synf)
                        phn.krnl.synf.combine(phr.krnl.synf)
                        phn.krnl.synf.unset(1)
#                       print 'TO' , str(phn.krnl.synf)
                    phn.krnl.synf.reset(
                        r.sftr)  # reset selected inherited bits
                    #                   print 'phr=' , phr , 'phn=' , phn
                    self.enqueue(phn)  # save new phrase to ramify (phase 1)
Exemple #5
0
    def _phase2 ( self , phr , fbs ):

        """
        create phrase for each 1-branch syntax rule going to
        the type of the current phrase

        arguments:
            self  -
            phr   - current phrase
            fbs   - its compounded feature bits

        exceptions:
            ParseOverflow
        """

        po = phr.krnl.posn
        gb = self.gbits[po]
        rls = self.gtb.extens[phr.krnl.typx]
#       print '> PHASE 2 at' , po , '=' , len(rls) , 'rules, gb=' , gb.hexadecimal()
        for r in rls:
            nt = r.styp
#           print 'type=' , nt , 'dm=' , self.gtb.mat.dm[nt].hexadecimal()
            if self.gtb.mat.derivable(nt,gb):          # rule applicable at current position?
#               print 'phr fet=' , fbs
#               print 'rul fet=' , r.utfet
                if ellyBits.check(fbs,r.utfet):        # phrase has required features for rule?
                    phn = self.makePhrase(po,r)        # make new phrase if checks succeed
                    if phn == None:
                        break
                    phn.krnl.lftd = phr                # current phrase is part of new one
                    phn.ntok = phr.ntok                # set token count for new phrase
#                   print 'phn=' , phn
#                   print 'phr=' , phr
                    self._score(phn)                   # compute bias score
                    rF = phn.krnl.synf.test(0)
                    lF = phn.krnl.synf.test(1)
                    if lF or rF:                       # inherit features from current phrase?
#                       print 'lF or rF'
                        phn.krnl.synf.combine(phr.krnl.synf)
                        if lF:
                            phn.krnl.synf.unset(0)
                        else:
                            phn.krnl.synf.unset(1)
#                       print 'TO' , str(phn.krnl.synf)
                    phn.krnl.synf.reset(r.sftr)        # reset selected inherited bits
#                   print 'phn=' , phn
                    self.enqueue(phn)                  # save new phrase for ramification
Exemple #6
0
    def _phase1 ( self , phr , fbs ):

        """
        check for goals at current position for current phrase type
        and create new phrase for each match

        arguments:
            self  -
            phr   - current phrase
            fbs   - its compounded feature bits

        exceptions:
            ParseOverflow
        """

        po = phr.krnl.posn
        gls = self.goal[po]
#       print '> PHASE 1 at' , po , '=' , len(gls) , 'goals'
        for g in gls:
#           print 'goal of' , g.cat
            if phr.krnl.typx == g.cat:
                r = g.rul
                if ellyBits.check(fbs,r.rtfet):
                    phx = g.lph                             # phrase that generated pertinent goal
                    phn = self.makePhrase(phx.krnl.posn,r)  # new phrase to satisfy goal
                    if phn == None:
                        break
                    phn.krnl.lftd = phx                     # goal phrase is left part of new one
                    phn.krnl.rhtd = phr                     # current phrase is right part
                    phn.ntok = phx.ntok + phr.ntok          # set token count for new phrase
                    self._score(phn)                        # compute bias score
                    rF = phn.krnl.synf.test(0)
                    lF = phn.krnl.synf.test(1)
                    if lF:                                  # inherit features from previous phrase?
#                       print '*l set' , str(phx.krnl.synf)
                        phn.krnl.synf.combine(phx.krnl.synf)
                        phn.krnl.synf.unset(0)
#                       print 'TO' , str(phn.krnl.synf)
                    if rF:                                  # inherit features from ramified phrase?
#                       print '*r set' , str(phx.krnl.synf)
                        phn.krnl.synf.combine(phr.krnl.synf)
                        phn.krnl.synf.unset(1)
#                       print 'TO' , str(phn.krnl.synf)
                    phn.krnl.synf.reset(r.sftr)             # reset selected inherited bits
#                   print 'phr=' , phr , 'phn=' , phn
                    self.enqueue(phn)                       # save new phrase for ramification
Exemple #7
0
    def run ( self , cntx , phrs , pnam=None ):

        """
        execute a generative procedure in context

        arguments:
            self  -
            cntx  - interpretive context
            phrs  - phrase to which procedure is attached
            pnam  - procedure name to associate with call

        returns:
            True on success, False otherwise
        """

        if self.logic == None:
            return True         # trivial success
        if phrs == None:
            print >> sys.stderr , 'null phrase:' , self
            return False

#       print 'run semantics for phr' , phrs.krnl.seqn , 'rule=' , phrs.krnl.rule.seqn

        cntx.pushStack()        # ready for any local variables
#       print 'pnam=' , pnam
        if pnam != None:        # save any procedure name for TRACE
            cntx.defineLocalVariable(PNAM,pnam)
        code = Code(self.logic) # get code of procedure to run

#       generativeDefiner.showCode(self.logic)

        status = True           # success flag to be return

        while status:

            op = code.next()    # iterate on operations in code
#           print 'op=' , op , 'code @' + str(code.index)

            if op < 0: break    # op codes are non-negative numeric

            if   op == semanticCommand.Gnoop:  # no op
                pass
            elif op == semanticCommand.Gretn:  # successful return
#               print 'return'
                break
            elif op == semanticCommand.Gfail:  # unsuccessful return
                print >> sys.stderr , 'generative semantic FAIL'
                status = False
            elif op == semanticCommand.Gleft:  # go to procedure for left descendant
                if phrs.krnl.lftd == None:
                    print >> sys.stderr , 'no left descendant for:' , phrs
                    status = False
                else:
#                   print 'to left descendant'
                    status = phrs.krnl.lftd.krnl.rule.gens.doRun(cntx,phrs.krnl.lftd)
            elif op == semanticCommand.Grght:  # go to procedure for right descendant, or left
#               print 'descent from' , phrs
                if phrs.krnl.rhtd == None:
                    if phrs.krnl.lftd == None:
                        print >> sys.stderr , 'no descendants for:' , phrs
                        status = False
#                   print 'to left descendant instead'
                    status = phrs.krnl.lftd.krnl.rule.gens.doRun(cntx,phrs.krnl.lftd)
                else:
#                   print 'to right descendant'
                    status = phrs.krnl.rhtd.krnl.rule.gens.doRun(cntx,phrs.krnl.rhtd)
            elif op == semanticCommand.Gblnk:  # insert space into output buffer
                cntx.insertCharsIntoBuffer(u' ')
            elif op == semanticCommand.Glnfd:  # insert linefeed into output buffer
                cntx.insertCharsIntoBuffer(u'\n ')
            elif op == semanticCommand.Gsplt:  # split off new buffer for output
                cntx.splitBuffer()
#               print "split",
#               cntx.printStatus()
            elif op == semanticCommand.Gback:  # go back to previous buffer
                cntx.backBuffer()
#               print "back",
#               cntx.printStatus()
            elif op == semanticCommand.Gmrge:  # merge buffers into current one
                cntx.mergeBuffers()
#               print "merge",
#               cntx.printStatus()
            elif op == semanticCommand.Gchng:  # merge with substitution
                ts = code.next()
                ss = code.next()
                cntx.mergeBuffersWithReplacement(ts,ss)
            elif ( op == semanticCommand.Gchck or
                   op == semanticCommand.Gnchk ):
                sens = (op == semanticCommand.Gchck)
                ids = code.next()
                ref = code.next()
                val = cntx.getLocalVariable(ids)
#               print "chk",ids,"[",ref,"]","[",val,"]"
#               print (val in ref),sens
                if (val in ref) == sens:
#                   print "match"
                    code.next()
                    continue
#               print "no match"
                code.skip()                    # match fails
            elif op == semanticCommand.Gchkf:
                refr = code.next()
                dblb = phrs.krnl.semf.compound()
                if ellyBits.check(dblb,refr):
                    code.next()
                    continue
                code.skip()                    # match fails
            elif op == semanticCommand.Gskip:  # unconditional branch
                code.skip()
            elif op == semanticCommand.Gvar:   # define new local variable
                var = code.next()
                val = code.next()
#               print "var",var,"[",val,"]"
                cntx.defineLocalVariable(var,val)
            elif op == semanticCommand.Gpeek:
                var = code.next()
                sns = code.next()
                val = cntx.peekIntoBuffer(nxtb=sns)
                cntx.setLocalVariable(var,val)
            elif ( op == semanticCommand.Gset  or
                   op == semanticCommand.Gextl or
                   op == semanticCommand.Gextr ):
                var = code.next()              # set a local variable
                if op == semanticCommand.Gset:
                    val = code.next()                                # from string
                elif op == semanticCommand.Gextl:
                    val = cntx.extractCharsFromBuffer(code.next(),1) # from buffer chars
                else:
                    val = cntx.extractCharsFromBuffer(code.next(),0) # from buffer chars
#               print 'val=' , val
                cntx.setLocalVariable(var,val)
            elif ( op == semanticCommand.Ginsr or
                   op == semanticCommand.Ginsn ):
                var = code.next()              # insert value local variable into buffer
                s = cntx.getLocalVariable(var)
                if s != '':
                    if op == semanticCommand.Ginsn:
                        cntx.insertCharsIntoBuffer(s,1)
                    else:
                        cntx.insertCharsIntoBuffer(s,0)
            elif op == semanticCommand.Gshft:  # shift text between buffers
                cntx.moveCharsBufferToBuffer(code.next())
            elif op == semanticCommand.Gdele:  # delete text from buffers
                cntx.deleteCharsFromBuffer(code.next())
            elif op == semanticCommand.Gdelt:  # delete to subsequence
                s = code.next()
                t = code.next()
                if t > 0:
                    cntx.deleteCharsInBufferTo(s)
                else:
                    cntx.deleteCharsInBufferFrom(s)
            elif op == semanticCommand.Gstor:  # save deletion to variable
                var = code.next()              # set a local variable from string
                nde = code.next()              # deletion count
#               print 'var=',var,'nde=',nde
                val = cntx.getDeletion()       # last deleted sequence
#               print 'val=',val
                if abs(nde) >= len(val):
                    val = [ ]                  # set to empty sequence
                elif nde > 0:
                    val = val[:-nde]           # drop final chars
                elif nde < 0:
                    val = val[-nde:]           # drop initial chars
                cntx.setLocalVariable(var,val) # set variable to deleted string
            elif op == semanticCommand.Gfnd:   # find sequence in buffer
                cntx.findCharsInBuffer(code.next(),code.next())
            elif op == semanticCommand.Gpick:  # use local variable to select text
                var = code.next()              #   to insert into buffer
                s = cntx.getLocalVariable(var)
#               print 'pick with',s
                dct = code.next()
#               print 'in',dct
                if not s in dct:
                    if '' in dct:
                        s = ''
                    else:
                        continue
                cntx.insertCharsIntoBuffer(dct[s])
            elif op == semanticCommand.Gappd:  # append explicit text to buffer
                s = code.next()
                cntx.insertCharsIntoBuffer(s)
            elif op == semanticCommand.Gget:   # assign global value to local variable
                var = code.next()
                gvn = code.next()
                cntx.setLocalVariable(var,cntx.getGlobalVariable(gvn))
            elif op == semanticCommand.Gput:   # assign value of local variable to global
                var = code.next()
                gvn = code.next()
                cntx.setGlobalVariable(gvn,cntx.getLocalVariable(var))
            elif op == semanticCommand.Gassg:  # assign one local variable to another
                dst = code.next()              # note order difference here!
                src = code.next()
                cntx.setLocalVariable(dst,cntx.getLocalVariable(src))
            elif op == semanticCommand.Gque:   # queue up or count down iteration
                dst = code.next()              # note order difference here!
                src = code.next()
                cno = code.next()              # > 0 for QUEUE, < 0 for UNQUEUE
                val = cntx.getLocalVariable(src)
                if len(val) == 0:              # src is empty string?
                    if cno == 0: continue      # if so, do nothing for QUEUE
                    it = ''                    # otherwise set dst to ''
                elif cno <= 0:
#                   print 'dst=',dst
                    it = cntx.getLocalVariable(dst) + val # QUEUE concatenates
                else:
                    if cno > len(val):         # UNQUEUE pops up to n chars for dst
                        cno = len(val)
                    it = val[:cno]
                    cntx.setLocalVariable(src,val[cno:])
                cntx.setLocalVariable(dst,it)  # new value for dst
#               print 'op=',op,'dst=',it
            elif op in [semanticCommand.Gunio,semanticCommand.Gintr,semanticCommand.Gcomp]:
                dst = code.next()                  # note order difference here!
                src = code.next()
#               print 'dst=',dst,'src=',src
                dv = cntx.getLocalVariable(dst)
                sv = cntx.getLocalVariable(src)
#               print 'op=',op,'dv=',dv,'sv=',sv
                lstd = [ ] if len(dv) == 0 else dv.split(LSTJ)
                lsts = [ ] if len(sv) == 0 else sv.split(LSTJ)
                ls   = [ ]
                if op == semanticCommand.Gunio:    # union of two list values
                    for x in lsts:
                        if not x in lstd:
                            lstd.append(x)
                    ls = lstd
                elif op == semanticCommand.Gintr:  # intersection of two list values
                    for x in lsts:
                        if x in lstd:
                            ls.append(x)
                else:                              # complement of list value
                    for x in lstd:
                        if not x in lsts:
                            ls.append(x)

                js = LSTJ.join(ls)
#               print 'ls=',ls,'js=',js
                cntx.setLocalVariable(dst,js)

            elif op == semanticCommand.Gobtn:  # obtain string for current parse position
                tok = cntx.getNthTokenInListing(phrs.krnl.posn)
#               print 'obtain: ' , tok.toUnicode()
                cntx.insertCharsIntoBuffer(tok.toUnicode())
            elif op == semanticCommand.Gcapt:  # capitalize first char in buffer
                c = cntx.extractCharsFromBuffer()
                if c != '':
                    cntx.insertCharsIntoBuffer(c.upper(),1)
            elif op == semanticCommand.Gucpt:  # capitalize first char in buffer
                c = cntx.extractCharsFromBuffer()
                if c != '':
                    cntx.insertCharsIntoBuffer(c.lower(),1)
            elif op == semanticCommand.Gtrce:  # show phrase info to trace execution
                cat = cntx.syms.getSyntaxTypeName(phrs.krnl.typx)
                brn = 1 if isinstance(phrs.krnl.rule,grammarRule.ExtendingRule) else 2
                pnam = cntx.getLocalVariable(PNAM)
                fm = u'TRACE @{0:d} type={1:s} rule={2:d} ({3:d}-br)'
                fs = fm.format(phrs.krnl.posn,cat,phrs.krnl.rule.seqn,brn)
                sys.stderr.write(fs)
                sys.stderr.flush()
                cntx.printStatus(pnam)
            elif op == semanticCommand.Gshow:   # show local variable
                vr = code.next()
                ms = code.next()
                st = despace(cntx.getLocalVariable(vr))
                fm = u'SHOW @phr {0:d} : [{1:s}] VAR {2:s}= [{3:s}]'
                fs = fm.format(phrs.krnl.seqn,ms,vr,st)
                sys.stderr.write(fs)
                sys.stderr.write('\n')
                sys.stderr.flush()
            elif op == semanticCommand.Gview:   # show current and new buffers partially
                nc = code.next()
                cbl , nbl = cntx.viewBufferDivide(nc)
                fm = u'VIEW @phr {0:d} : {1:s} | {2:s}\n'
                st = fm.format(phrs.krnl.seqn,str(cbl),str(nbl))
                sys.stderr.write(st)
                sys.stderr.flush()
            elif op == semanticCommand.Gproc:  # semantic subprocedure
                name = code.next()
#               print >> sys.stderr , 'run procedure (' + name + ')'
                if name == '': continue        # null procedure is no operation`
                proc = cntx.getProcedure(name)
                if proc == None:
                    print >> sys.stderr , 'unknown subprocedure name' , name
                    status = False
                else:
                    status = proc.run(cntx,phrs,name)
            else:
                print >> sys.stderr , GenerativeProcedure._error , 'op=' , op
                status = False

        cntx.popStack()  # undefine local variables for procedure
        return status
    def score(self, cntx, phrs):
        """
        compute plausibility score

        arguments:
            self  -
            cntx  - interpretive context
            phrs  - associated phrase

        returns:
            integer plausibility score
        """

        if self.logic == None: return 0
        #       print 'cognitive scoring: phrs=' , phrs

        trce = False  # enable diagnostic tracing
        clno = 0  # clause index
        psum = 0  # to accumulate plausibility score

        for cls in self.logic:  # go through all clauses

            clno += 1
            #           print 'clno=' , clno
            for p in cls[0]:  # go through all predicates of clause
                op = p[0]
                if op == semanticCommand.Ctrc:
                    print >> sys.stderr, ''
                    print >> sys.stderr, '  at phrase', phrs.krnl.seqn, ': rule=', phrs.krnl.rule.seqn,
                    print >> sys.stderr, 'with current bias= ', phrs.krnl.rule.bias
                    print >> sys.stderr, '  l:', phrs.krnl.lftd
                    print >> sys.stderr, '  r:', phrs.krnl.rhtd
                    trce = True
                    break
                elif op == semanticCommand.Clftf or op == semanticCommand.Crhtf:
                    # test features of descendants
                    if phrs.krnl.lftd == None: break
                    dph = (phrs.krnl.lftd if op == semanticCommand.Clftf
                           or phrs.krnl.rhtd == None else phrs.krnl.rhtd)
                    bts = dph.krnl.semf.compound()
                    if not ellyBits.check(p[1], bts): break
                elif op == semanticCommand.Clftc or op == semanticCommand.Crhtc:
                    # check concepts of descendants
                    if phrs.krnl.lftd == None: break
                    if cntx == None:
                        print >> sys.stderr, '  no context for conceptual hierarchy'
                        break
                    dph = (phrs.krnl.lftd if op == semanticCommand.Clftc
                           or phrs.krnl.rhtd == None else phrs.krnl.rhtd)
                    cnc = dph.krnl.cncp
                    cx = p[1]  # concepts to check against
                    mxw = -1
                    mxc = None
                    for c in cx:
                        w = cntx.wghtg.hiery.isA(cnc, c)
                        if mxw < w:
                            mxw = w
                            mxc = c
                    if mxw < 0: break
                    cntx.wghtg.noteConcept(mxc)
                else:
                    # unknown command
                    print >> sys.stderr, 'bad cog sem action=', op
                    break

            else:  # execute actions of clause if ALL predicates satisfied
                if trce:
                    ncls = len(self.logic)
                    print >> sys.stderr, '  cog sem at clause', clno, 'of', ncls
#                   print >> sys.stderr , '   =' , cls[1]
                for a in cls[1]:  # get next action
                    op = a[0]
                    if op == semanticCommand.Cadd:  # add to score?
                        psum += a[1]
                    elif op == semanticCommand.Csetf:  # set   semantic features?
                        phrs.krnl.semf.combine(a[1])
                    elif op == semanticCommand.Crstf:  # reset semantic features?
                        phrs.krnl.semf.reset(a[1])
                    elif op == semanticCommand.Csetc:  # set concepts?
                        phrs.krnl.cncp = a[1]
                    elif phrs.krnl.lftd == None:
                        pass
                    elif op == semanticCommand.Clhr:  # inherit from left descendant?
                        phrs.krnl.semf.combine(phrs.krnl.lftd.krnl.semf)
                        phrs.krnl.cncp = phrs.krnl.lftd.krnl.cncp
                    elif op == semanticCommand.Crhr:  # inherit from right?
                        dsc = phrs.krnl.rhtd if phrs.krnl.rhtd != None else phrs.krnl.lftd
                        phrs.krnl.semf.combine(dsc.krnl.semf)
                        phrs.krnl.cncp = dsc.krnl.cncp
#                   if trce:
#                       print >> sys.stderr , '->' , phr.krnl.semf

                break  # ignore subsequent clauses on taking action

        inc = 0  # compute conceptual contribution
        rwy = phrs.krnl.rule.nmrg
        #       print >> sys.stderr , 'conceptual plausibility'
        if rwy == 2:  # 2-branch splitting rule?
            #           print '2-branch!'
            inc = cntx.wghtg.relateConceptPair(phrs.krnl.lftd.krnl.cncp,
                                               phrs.krnl.rhtd.krnl.cncp)
            #           print >> sys.stderr , phrs.krnl.lftd.krnl.cncp , ':' , phrs.krnl.rhtd.krnl.cncp , '=' , inc , '!'
            if inc > 1:
                phrs.krnl.ctxc = cntx.wghtg.getIntersection()
#           print >> sys.stderr , '2-way bias incr=' , inc
        elif phrs.krnl.lftd != None:  # 1-branch extending rule?
            #           print >> sys.stderr , '1-branch!'
            dst = cntx.wghtg.interpretConcept(phrs.krnl.lftd.krnl.cncp)
            if dst > 0:
                phrs.krnl.ctxc = cntx.wghtg.getIntersection()
                inc = 1
#           print >> sys.stderr , '1-way bias incr=' , inc
        if inc > 0: psum += inc  # only positive increments contribute
        #       print >> sys.stderr , 'phrase' , phrs.krnl.seqn, 'intersect=' , phrs.krnl.ctxc

        if trce:
            print >> sys.stderr, '  incremental scoring=', psum,
            print >> sys.stderr, 'sem[' + phrs.krnl.semf.hexadecimal() + ']'
        return psum
Exemple #9
0
    def run ( self , code , cntx , phrs , pnam=None ):

        """
        execute a generative procedure in context

        arguments:
            self  -
            code  - actual generative semantics to run
            cntx  - interpretive context
            phrs  - phrase to which procedure is attached
            pnam  - procedure name to associate with call

        returns:
            True on success, False otherwise
        """

        if code == None:
            return True         # trivial success
        if phrs == None:
            print >> sys.stderr , 'null phrase:' , self
            return False
#       print 'run code =' , code
#       if len(code.lstg) > 4:
#           generativeDefiner.showCode(code.lstg)

#       print 'run semantics for phr' , phrs.krnl.seqn , 'rule=' , phrs.krnl.rule.seqn

        cntx.pushStack()        # ready for any local variables
#       print 'pnam=' , pnam
        if pnam != None:        # save any procedure name for TRACE
            cntx.defineLocalVariable(PNAM,pnam)

        status = True           # success flag to be return

        while status:

            op = code.next()    # iterate on operations in code
#           print 'op=' , op , 'code @' + str(code.index)

            if op < 0: break    # op codes are non-negative numeric

            if   op == semanticCommand.Gnoop:  # no op
                pass
            elif op == semanticCommand.Gretn:  # successful return
#               print 'return'
                break
            elif op == semanticCommand.Gfail:  # unsuccessful return
                print >> sys.stderr , 'generative semantic FAIL'
                status = False
            elif op == semanticCommand.Gleft:  # go to procedure for left descendant
                if phrs.krnl.lftd == None:
                    print >> sys.stderr , 'no left descendant for:' , phrs
                    status = False
                else:
#                   print 'to left descendant'
                    status = phrs.krnl.lftd.krnl.rule.gens.doRun(cntx,phrs.krnl.lftd)
            elif op == semanticCommand.Grght:  # go to procedure for right descendant, or left
#               print 'descent from' , phrs
                if phrs.krnl.rhtd == None:
                    if phrs.krnl.lftd == None:
                        print >> sys.stderr , 'no descendants for:' , phrs
                        status = False
#                   print 'to left descendant instead'
                    status = phrs.krnl.lftd.krnl.rule.gens.doRun(cntx,phrs.krnl.lftd)
                else:
#                   print 'to right descendant'
                    status = phrs.krnl.rhtd.krnl.rule.gens.doRun(cntx,phrs.krnl.rhtd)
            elif op == semanticCommand.Gblnk:  # insert space into output buffer
                cntx.insertCharsIntoBuffer(u' ')
            elif op == semanticCommand.Glnfd:  # insert linefeed into output buffer
                cntx.insertCharsIntoBuffer(u'\n ')
            elif op == semanticCommand.Gsplt:  # split off new buffer for output
                cntx.splitBuffer()
#               print "split",
#               cntx.printStatus()
            elif op == semanticCommand.Gback:  # go back to previous buffer
                cntx.backBuffer()
#               print "back",
#               cntx.printStatus()
            elif op == semanticCommand.Gmrge:  # merge buffers into current one
                cntx.mergeBuffer()
#               print "merge",
#               cntx.printStatus()
            elif op == semanticCommand.Gchng:  # merge with substitution
                ts = code.next()
                ss = code.next()
                cntx.mergeBufferWithReplacement(ts,ss)
            elif ( op == semanticCommand.Gchck or
                   op == semanticCommand.Gnchk ):
                sens = (op == semanticCommand.Gchck)
                ids = code.next()
                ref = code.next()
                val = cntx.getLocalVariable(ids)
#               print "chk",ids,"[",ref,"]","[",val,"]"
#               print (val in ref),sens
                if (val in ref) == sens:
#                   print "match"
                    code.next()
                    continue
#               print "no match"
                code.skip()                    # match fails
            elif op == semanticCommand.Gchkf:
                refr = code.next()
                dblb = phrs.krnl.semf.compound()
                if ellyBits.check(dblb,refr):
                    code.next()
                    continue
                code.skip()                    # match fails
            elif op == semanticCommand.Gskip:  # unconditional branch
                code.skip()
            elif op == semanticCommand.Gvar:   # define new local variable
                var = code.next()
                val = code.next()
#               print "var",var,"[",val,"]"
                cntx.defineLocalVariable(var,val)
            elif op == semanticCommand.Gpeek:
                var = code.next()
                sns = code.next()
                val = cntx.peekIntoBuffer(nxtb=sns)
                cntx.setLocalVariable(var,val)
            elif ( op == semanticCommand.Gset  or
                   op == semanticCommand.Gextl or
                   op == semanticCommand.Gextr ):
                var = code.next()              # set a local variable
                if op == semanticCommand.Gset:
                    val = code.next()                                  # from string
                elif op == semanticCommand.Gextl:                      # source is current buffer
                    val = cntx.extractCharsFromBuffer(code.next(),m=0) #   get buffer chars
                else:                                                  # source is next    buffer
                    val = cntx.extractCharsFromBuffer(code.next(),m=1) #   get buffer chars
#               print 'val=' , val
                cntx.setLocalVariable(var,val)
            elif ( op == semanticCommand.Ginsr or
                   op == semanticCommand.Ginsn ):
                var = code.next()              # insert value local variable into buffer
                s = cntx.getLocalVariable(var)
                if s != '':
                    if op == semanticCommand.Ginsn:
                        cntx.insertCharsIntoBuffer(s,m=1)
                    else:
                        cntx.insertCharsIntoBuffer(s,m=0)
            elif op == semanticCommand.Gshft:  # shift text between buffers
                cntx.moveCharsBufferToBuffer(code.next())
            elif op == semanticCommand.Gdele:  # delete text from buffers
                cntx.deleteCharsFromBuffer(code.next())
            elif op == semanticCommand.Gdelt:  # delete to subsequence
                s = code.next()
                t = code.next()
                if t > 0:
                    cntx.deleteCharsInBufferTo(s)
                else:
                    cntx.deleteCharsInBufferFrom(s)
            elif op == semanticCommand.Gstor:  # save deletion to variable
                var = code.next()              # set a local variable from string
                nde = code.next()              # deletion count
#               print 'var=',var,'nde=',nde
                val = cntx.getDeletion()       # last deleted sequence
#               print 'val=',val
                if abs(nde) >= len(val):
                    val = [ ]                  # set to empty sequence
                elif nde > 0:
                    val = val[:-nde]           # drop final chars
                elif nde < 0:
                    val = val[-nde:]           # drop initial chars
                cntx.setLocalVariable(var,val) # set variable to deleted string
            elif op == semanticCommand.Gfnd:   # find sequence in buffer
                cntx.findCharsInBuffer(code.next(),code.next())
            elif op == semanticCommand.Galgn:  # align buffer to start of line
                sens = code.next()             # direction of alignment
                cntx.findCharsInBuffer('\n',sens)
                if sens:
                    if cntx.peekIntoBuffer(False) == '\n':
                        cntx.moveCharsBufferToBuffer(1)  # move over expected space after \n
                else:
                    if cntx.peekIntoBuffer(True) == '\n':
                        cntx.moveCharsBufferToBuffer(2)  # move \n + space to current buffer
            elif op == semanticCommand.Gpick:  # use local variable to select text
                var = code.next()              #   to insert into buffer
                s = cntx.getLocalVariable(var)
#               print 'pick with',s
                dct = code.next()
#               print 'in',dct
                if not s in dct:
                    if '' in dct:
                        s = ''
                    else:
                        continue
                cntx.insertCharsIntoBuffer(dct[s])
            elif op == semanticCommand.Gappd:  # append explicit text to current buffer
                s = code.next()
                cntx.insertCharsIntoBuffer(s)
            elif op == semanticCommand.Gget:   # assign global value to local variable
                var = code.next()
                gvn = code.next()
                cntx.setLocalVariable(var,cntx.getGlobalVariable(gvn))
            elif op == semanticCommand.Gput:   # assign value of local variable to global
                var = code.next()
                gvn = code.next()
                cntx.setGlobalVariable(gvn,cntx.getLocalVariable(var))
            elif op == semanticCommand.Gassg:  # assign one local variable to another
                dst = code.next()              # note order difference here!
                src = code.next()
                cntx.setLocalVariable(dst,cntx.getLocalVariable(src))
            elif op == semanticCommand.Gque:   # queue up or count down iteration
                dst = code.next()              # note order difference here!
                src = code.next()
                cno = code.next()              # > 0 for QUEUE, < 0 for UNQUEUE
                val = cntx.getLocalVariable(src)
                if len(val) == 0:              # src is empty string?
                    if cno == 0: continue      # if so, do nothing for QUEUE
                    it = ''                    # otherwise set dst to ''
                elif cno <= 0:
#                   print 'dst=',dst
                    it = cntx.getLocalVariable(dst) + val # QUEUE concatenates
                else:
                    if cno > len(val):         # UNQUEUE pops up to n chars for dst
                        cno = len(val)
                    it = val[:cno]
                    cntx.setLocalVariable(src,val[cno:])
                cntx.setLocalVariable(dst,it)  # new value for dst
#               print 'op=',op,'dst=',it
            elif op in [semanticCommand.Gunio,semanticCommand.Gintr,semanticCommand.Gcomp]:
                dst = code.next()                  # note order difference here!
                src = code.next()
#               print 'dst=',dst,'src=',src
                dv = cntx.getLocalVariable(dst)
                sv = cntx.getLocalVariable(src)
#               print 'op=',op,'dv=',dv,'sv=',sv
                lstd = [ ] if len(dv) == 0 else dv.split(LSTJ)
                lsts = [ ] if len(sv) == 0 else sv.split(LSTJ)
                ls   = [ ]
                if op == semanticCommand.Gunio:    # union of two list values
                    for x in lsts:
                        if not x in lstd:
                            lstd.append(x)
                    ls = lstd
                elif op == semanticCommand.Gintr:  # intersection of two list values
                    for x in lsts:
                        if x in lstd:
                            ls.append(x)
                else:                              # complement of list value
                    for x in lstd:
                        if not x in lsts:
                            ls.append(x)

                js = LSTJ.join(ls)
#               print 'ls=',ls,'js=',js
                cntx.setLocalVariable(dst,js)

            elif op == semanticCommand.Gobtn:  # obtain string for current parse position
                tok = cntx.getNthTokenInListing(phrs.krnl.posn)
#               print 'obtain: ' , tok.toUnicode()
                cntx.insertCharsIntoBuffer(tok.toUnicode())
            elif op == semanticCommand.Gcapt:  #   capitalize first char in next buffer
                c = cntx.extractCharsFromBuffer(m=1)
                if c != '':
                    cntx.insertCharsIntoBuffer(c.upper(),m=1)
            elif op == semanticCommand.Gucpt:  # uncapitalize first char in next buffer
                c = cntx.extractCharsFromBuffer(m=1)
                if c != '':
                    cntx.insertCharsIntoBuffer(c.lower(),m=1)
            elif op == semanticCommand.Gtrce:  # show phrase info to trace execution
                cat = cntx.syms.getSyntaxTypeName(phrs.krnl.typx)
                brn = 1 if isinstance(phrs.krnl.rule,grammarRule.ExtendingRule) else 2
                pnam = cntx.getLocalVariable(PNAM)
                fm = u'TRACE @{0:d} type={1:s} rule={2:d} ({3:d}-br)'
                fs = fm.format(phrs.krnl.posn,cat,phrs.krnl.rule.seqn,brn)
                sys.stderr.write(fs)
                sys.stderr.flush()
                cntx.printStatus(pnam)
            elif op == semanticCommand.Gshow:   # show local variable
                vr = code.next()
                ms = code.next()
                st = despace(cntx.getLocalVariable(vr))
                fm = u'SHOW @phr {0:d} : [{1:s}] VAR {2:s}= [{3:s}]'
                fs = fm.format(phrs.krnl.seqn,ms,vr,st)
                sys.stderr.write(fs)
                sys.stderr.write('\n')
                sys.stderr.flush()
            elif op == semanticCommand.Gview:   # show current and new buffers partially
                nc = code.next()
                cbl , nbl = cntx.viewBufferDivide(nc)
                cbn = cntx.getCurrentBuffer()
                fm = u'VIEW ={0:2d} @phr {1:d} : {2:s} | {3:s}\n'
                st = fm.format(cbn,phrs.krnl.seqn,str(cbl),str(nbl))
                sys.stderr.write(st)
                sys.stderr.flush()
            elif op == semanticCommand.Gproc:  # semantic subprocedure
                name = code.next()
#               print >> sys.stderr , 'run procedure (' + name + ')' , 'in' , phrs
                if name == '': continue        # null procedure is no operation`
                proc = cntx.getProcedure(name) # generative semantics object
                if proc == None:
                    print >> sys.stderr , 'unknown subprocedure name' , name
                    status = False
                else:
                    status = proc.run(Code(proc.logic),cntx,phrs,name)
            else:
                print >> sys.stderr , '** unknown generative semantic op=' , op , 'in' , phrs
                status = False

        cntx.popStack()  # undefine local variables for procedure
        return status
Exemple #10
0
    def score(self, cntx, phrs):
        """
        compute plausibility score

        arguments:
            self  -
            cntx  - interpretive context
            phrs  - associated phrase

        returns:
            integer plausibility score
        """

        if self.logic == None: return 0
        #       print ( 'cognitive scoring: phrs=' , phrs )

        trce = False  # enable diagnostic tracing
        clno = 0  # clause index
        psum = 0  # to accumulate plausibility score

        for cls in self.logic:  # go through all clauses

            clno += 1
            #           print ( 'clno=' , clno )
            for p in cls[0]:  # go through all predicates of clause
                op = p[0]
                #               print ( 'cog sem op=' , op )
                if op == semanticCommand.Ctrc:
                    print('  at phrase',
                          phrs.krnl.seqn,
                          ': rule=',
                          phrs.krnl.rule.seqn,
                          end=' ',
                          file=sys.stderr)
                    print('with current bias= ',
                          phrs.krnl.rule.bias,
                          file=sys.stderr)
                    print('  l:', phrs.krnl.lftd, file=sys.stderr)
                    print('  r:', phrs.krnl.rhtd, file=sys.stderr)
                    print(' ',
                          phrs.ntok,
                          'token(s) spanned',
                          end=' ',
                          file=sys.stderr)
                    print('@' + str(phrs.krnl.posn), file=sys.stderr)
                    print(file=sys.stderr)
                    trce = True
                    break
                elif op == semanticCommand.Clftf or op == semanticCommand.Crhtf:
                    # test features of descendants
                    #                   print ( 'op=' , p , file=sys.stderr )
                    if phrs.krnl.lftd == None: break
                    #                   print ('lftd=' , phrs.krnl.lftd.krnl.seqn , file=sys.stderr )
                    dph = (phrs.krnl.lftd if op == semanticCommand.Clftf
                           or phrs.krnl.rhtd == None else phrs.krnl.rhtd)
                    #                   print ( 'dph=' , dph , file=sys.stderr )
                    bts = dph.krnl.semf.compound()
                    #                   print ( 'bts=' , bts , file=sys.stderr )
                    #                   print ( 'cnd=' , p[1] , file=sys.stderr )
                    if not ellyBits.check(p[1], bts): break
                elif op == semanticCommand.Clftc or op == semanticCommand.Crhtc:
                    # check concepts of descendants
                    if phrs.krnl.lftd == None: break
                    if cntx == None:
                        print('  no context for conceptual hierarchy',
                              file=sys.stderr)
                        break
                    dph = (phrs.krnl.lftd if op == semanticCommand.Clftc
                           or phrs.krnl.rhtd == None else phrs.krnl.rhtd)
                    cnc = dph.krnl.cncp
                    cx = p[1]  # concepts to check against
                    mxw = -1
                    mxc = None
                    for c in cx:
                        w = cntx.wghtg.hiery.isA(cnc, c)
                        if mxw < w:
                            mxw = w
                            mxc = c
                    if mxw < 0: break
                    cntx.wghtg.noteConcept(mxc)
                elif op == semanticCommand.Cngt or op == semanticCommand.Cnlt:
                    # check token count of evaluated phrase
                    nt = phrs.ntok
                    nm = p[1]
                    if op == semanticCommand.Cngt:
                        if nt <= nm: break
                    else:
                        if nt >= nm: break
                elif op == semanticCommand.Cpgt or op == semanticCommand.Cplt:
                    # check position of evaluated phrase
                    po = phrs.krnl.posn
                    nm = p[1]
                    #                   print ( 'po=' , po , 'nm=' ,nm )
                    if op == semanticCommand.Cpgt:
                        if po <= nm: break
                    else:
                        if po >= nm: break
#                   print ( 'no break' )
                elif op == semanticCommand.Ccgt or op == semanticCommand.Cclt:
                    # check token count of evaluated phrase
                    nc = phrs.lens
                    nm = p[1]
                    if op == semanticCommand.Ccgt:
                        if nc <= nm: break
                    else:
                        if nc >= nm: break
                else:
                    # unknown command
                    print('bad cog sem action=', op, file=sys.stderr)
                    break

            else:  # execute actions of clause if ALL predicates satisfied
                if trce:
                    ncls = len(self.logic)
                    print('  cog sem at clause',
                          clno,
                          'of',
                          ncls,
                          file=sys.stderr)
                    #                   print ( '   =' , cls[1] , file=sys.stderr )
                    print(file=sys.stderr)
                for a in cls[1]:  # get next action
                    op = a[0]
                    if op == semanticCommand.Cadd:  # add to score?
                        psum += a[1]
                    elif op == semanticCommand.Csetf:  # set   semantic features?
                        phrs.krnl.semf.combine(a[1])
                    elif op == semanticCommand.Crstf:  # reset semantic features?
                        phrs.krnl.semf.reset(a[1])
                    elif op == semanticCommand.Csetc:  # set concepts?
                        phrs.krnl.cncp = a[1]
                    elif phrs.krnl.lftd == None:
                        pass
                    elif op == semanticCommand.Clhr:  # inherit from left descendant?
                        phrs.krnl.semf.combine(phrs.krnl.lftd.krnl.semf)
                        phrs.krnl.cncp = phrs.krnl.lftd.krnl.cncp
                    elif op == semanticCommand.Crhr:  # inherit from right?
                        dsc = phrs.krnl.rhtd if phrs.krnl.rhtd != None else phrs.krnl.lftd
                        phrs.krnl.semf.combine(dsc.krnl.semf)
                        phrs.krnl.cncp = dsc.krnl.cncp
#                   if trce:
#                       print ( '->' , phr.krnl.semf , file=sys.stderr )

                break  # ignore subsequent clauses on taking action

        inc = 0  # compute conceptual contribution
        rwy = phrs.krnl.rule.nmrg
        #       print ( 'conceptual plausibility' , file=sys.stderr )
        if rwy == 2:  # 2-branch splitting rule?
            #           print ( '2-branch!' )
            inc = cntx.wghtg.relateConceptPair(phrs.krnl.lftd.krnl.cncp,
                                               phrs.krnl.rhtd.krnl.cncp)
            #           print ( phrs.krnl.lftd.krnl.cncp , ':' , phrs.krnl.rhtd.krnl.cncp , '=' , inc , '!' , file=sys.stderr )
            if inc > 1:
                phrs.krnl.ctxc = cntx.wghtg.getIntersection()
#           print ( '2-way bias incr=' , inc , file=sys.stderr )
        elif phrs.krnl.lftd != None:  # 1-branch extending rule?
            #           print ( '1-branch!' , file=sys.stderr )
            dst = cntx.wghtg.interpretConcept(phrs.krnl.lftd.krnl.cncp)
            if dst > 0:
                phrs.krnl.ctxc = cntx.wghtg.getIntersection()
                inc = 1
#           print ( '1-way bias incr=' , inc , file=sys.stderr )
        if inc > 0: psum += inc  # only positive increments contribute
        #       print ( 'phrase' , phrs.krnl.seqn, 'intersect=' , phrs.krnl.ctxc , file=sys.stderr )

        if trce:
            print('  raw plausibility=', phrs.krnl.bias, file=sys.stderr)
            print('  adjustment=', psum, end=' ', file=sys.stderr)
            print('sem[' + phrs.krnl.semf.hexadecimal() + ']', file=sys.stderr)
            print(file=sys.stderr)
        return psum
    def score ( self , cntx , phrs ):

        """
        compute plausibility score

        arguments:
            self  -
            cntx  - interpretive context
            phrs  - associated phrase

        returns:
            integer plausibility score
        """

        if self.logic == None: return 0
#       print 'cognitive scoring'

        psum = 0               # to accumulate plausibility score

        for cls in self.logic: # go through all clauses

            for p in cls[0]:   # go through all predicates of clause
                op = p[0]
                if op == semanticCommand.Clftf or op == semanticCommand.Crhtf:
                    # test features of descendants
                    dph = phrs.lftd if op == semanticCommand.Clftf else phrs.rhtd
                    bts = dph.semf.compound()
                    if not ellyBits.check(p[1],bts): break
                elif op == semanticCommand.Clftc or op == semanticCommand.Crhtc:
                    # check concepts of descendants
                    dph = phrs.lftd if op == semanticCommand.Clftc else phrs.rhtd
                    cnc = dph.cons
                    cx = p[1]  # concepts to check against
                    mxw = -1
                    mxc = None
                    for c in cx:
                        w = cntx.wghtg.hier.isA(cnc,c)
                        if mxw < w:
                            mxw = w
                            mxc = c
                    if mxw < 0: break
                    cntx.wghtg.noteConcept(mxc)
                else:
                    # unknown command
                    print >> sys.stderr , 'bad cog sem action=' , op
                    break

            else:             # execute actions of clause if ALL predicates satisfied
                for a in cls[1]:                      # get mext action
                    op = a[0]
                    if op == semanticCommand.Cadd:    # add to score?
                        psum += a[1]
                    elif op == semanticCommand.Clhr:  # inherit from left descendant?
                        phrs.semf.combine(phrs.lftd.semf)
                        phrs.cncp = phrs.lftd.cncp
                    elif op == semanticCommand.Crhr:  # inherit from right?
                        phrs.semf.combine(phrs.rhtd.semf)
                        phrs.cncp = phrs.rhtd.cncp
                    elif op == semanticCommand.Csetf: # set semantic features?
                        phrs.semf.combine(a[1])
                    elif op == semanticCommand.Csetc: # set concepts?
                        phrs.cncp = a[1]

                break  # ignore subsequent clauses on taking action

        inc = 0                  # compute conceptual contribution
        rwy = phrs.rule.nmrg
#       print >> sys.stderr , 'conceptual plausibility'
        if rwy == 2:        # 2-branch splitting rule?
#           print '2-branch!'
            inc = cntx.wghtg.relateConceptPair(phrs.lftd.cncp,phrs.rhtd.cncp)
#           print >> sys.stderr , phrs.lftd.cncp , ':' , phrs.rhtd.cncp , '=' , inc , '!'
            if inc > 1:
                phrs.ctxc = cntx.wghtg.getIntersection()
#           print >> sys.stderr , '2-way bias incr=' , inc
        elif phrs.lftd != None:  # 1-branch extending rule?
#           print >> sys.stderr , '1-branch!'
            dst = cntx.wghtg.interpretConcept(phrs.lftd.cncp)
            if dst > 0:
                phrs.ctxc = cntx.wghtg.getIntersection()
                inc = 1
#           print >> sys.stderr , '1-way bias incr=' , inc
        if inc > 0: psum += inc  # only positive increments contribute
#       print >> sys.stderr , 'phrase' , phrs.seqn, 'intersect=' , phrs.ctxc

        return psum