Ejemplo n.º 1
0
def compile ( syms, clss , code , nwy=0 ):

    """
    create cognitive or generative semantic procedure from code

    arguments:
        syms  - symbol table
        clss  - type of procedure: 'c'=cognitive, 'g'=generative
        code  - list of semantic commands
        nwy   - type of grammar rule

    returns:
        cognitive or generative procedure on success, None otherwise
    """

    inps = ellyDefinitionReader.EllyDefinitionReader(code)
    if   clss == 'c':
        cp = cognitiveProcedure.CognitiveProcedure(syms,inps,nwy)
        return cp if cp.logic != None else None
    elif clss == 'g':
        gp = generativeProcedure.GenerativeProcedure(syms,inps)
        return gp if gp.logic != None else None
    else:
        print ( 'bad semantic procedure class' , file=sys.stderr )
        return None
Ejemplo n.º 2
0
    def _genp(self, ls):
        """
        create a generative procedure

        arguments:
            self  -
            ls    - list of command strings

        returns:
            GenerativeProcedure
        """

        din = ellyDefinitionReader.EllyDefinitionReader(ls)
        return generativeProcedure.GenerativeProcedure(self.context.syms, din)
Ejemplo n.º 3
0
    def __init__(self, stb, gtb, ptb):
        """
        create parse tree generator with grammar and syntax type patterns

        arguments:
            self  -
            stb   - symbol table
            gtb   - grammar
            ptb   - patterns
        """

        #       print "at ParseTreeBottomUp"
        inp = ellyDefinitionReader.EllyDefinitionReader(['obtain'])
        self.litg = generativeProcedure.GenerativeProcedure(stb, inp)
        inp = ellyDefinitionReader.EllyDefinitionReader([])
        self.litc = cognitiveProcedure.CognitiveProcedure(stb, inp)
        self.ntyp = stb.getSyntaxTypeCount()
        self.gtb = gtb
        self.ptb = ptb
        self.ctx = None  # set by ParseTree, if at all
        super(ParseTreeBottomUp, self).__init__()
        #       print "back in ParseTreeBottomUp"
        self._pls = 0
Ejemplo n.º 4
0
    def __init__(self, dta):
        """
        initialization of vocabulary object from retrieved record

        arguments:
            self  -
            dta   - what DB support returns

        throws:
            FormatFailure on error
        """

        self._ln = 0
        rec = dta[1]  # data record found for search key
        #       print ( 'voc rec=' , rec , file=sys.stderr )
        r = rec.split('=:')  # split off term in data record
        if len(r) <= 1: return  # the '=:' is mandatory
        d = r[1].strip().split(' ')  # definition is right of '=:'
        #       print ( 'VEntry: define as' , d , file=sys.stderr )
        if len(d) < 4: return  # it should have at least 4 parts
        ur = r[0].strip()  # term left of '=:'
        self.chs = list(ur)  # save it
        self.cat = int(d.pop(0))  # syntactic category
        #       print ( '    full term=' , ''.join(self.chs) , file=sys.stderr )
        sy = d.pop(0)
        nb = len(sy) * 4
        self.syf = ellyBits.EllyBits(nb)  # allocate bits
        self.syf.reinit(sy)  # set syntactic features
        sm = d.pop(0)
        nb = len(sm) * 4
        self.smf = ellyBits.EllyBits(nb)  # allocate bits
        self.smf.reinit(sm)  # set semantic  features
        self.bia = int(d.pop(0))  # save initial plausibility
        if len(d) > 0:  # any concept?
            self.con = d.pop(0).upper()  # if so, save it
        else:
            self.con = '-'

#       print ( '    translation=' , d , file=sys.stderr )

        if len(d) == 0:  # no further definition?
            self.gen = obtnp  # if so, then use default procedure
            self._nt = 0  #   i.e. no translation
        elif d[0][0] == '=':  # simple translation?
            dfs = ' '.join(d)  # just in case translation had spaces
            #           print ( 'def ext=' , dfs , file=sys.stderr )
            pls = ['append ' + dfs[1:]]
            inpts = ellyDefinitionReader.EllyDefinitionReader(pls)
            self.gen = generativeProcedure.GenerativeProcedure(None, inpts)
            self._nt = 1
        elif d[0][0] == '(':  # get predefined procedure
            inpts = ellyDefinitionReader.EllyDefinitionReader([d[0]])
            self.gen = generativeProcedure.GenerativeProcedure(None, inpts)
            self._nt = 0
        else:  # otherwise, set for selection of translation
            #           print ( 'multi selection, d=' , d , file=sys.stderr )
            cm = 'pick LANG ('  # construct instruction to select
            for p in d:
                if p[-1] == ',':
                    p = p[:-1]
                cm += p + '#'  # build selection clauses
            cm += ')'
            gens[0] = cm  # replace action
            #           print ( 'cm=' , cm )
            #           print ( 'gens=' , gens )
            inpts = ellyDefinitionReader.EllyDefinitionReader(gens)
            self.gen = generativeProcedure.GenerativeProcedure(None, inpts)
            if self.gen == None:
                print('vocabulary generative semantic failure',
                      file=sys.stderr)
                print('gens=', gens, file=sys.stderr)
                raise ellyException.FormatFailure
#           print ( 'vocabulary gen.logic' )
#           generativeDefiner.showCode(self.gen.logic)
            self._nt = len(d)

        self._ln = len(self.chs)
Ejemplo n.º 5
0
"""
vocabulary element from external table lookup
"""

import sys
import ellyBits
import ellyException
import ellyDefinitionReader
import generativeProcedure
import unicodedata

gens = ['*']  # define generative procedure for translation
geno = ['obtain']  # default generative procedure

inptx = ellyDefinitionReader.EllyDefinitionReader(geno)
obtnp = generativeProcedure.GenerativeProcedure(None, inptx)


class VocabularyElement(object):
    """
    vocabulary term plus definition

    attributes:
        chs   - list of chars in term
        cat   - syntactic category
        syf   - syntactic flags
        smf   - semantic  flags
        bia   - initial plausibility score
        con   - associated concept
        gen   - generative procedure