コード例 #1
0
ファイル: patternTable.py プロジェクト: belkhir-nacim/pyelly
    def __init__ ( self , syms , dfls ):

        """
        initialization

        arguments:
            self  -
            syms  - Elly grammatical symbol table
            dfls  - definition elements in list

        exceptions:
            FormatFailure on error
        """

#       print 'dfls=' , dfls
        if len(dfls) != 3:                            # must have 3 elements
            raise ellyException.FormatFailure
        else:
            if dfls[0] == '\\0':
                self.patn = u'\x00'                       # special nul pattern
            else:
                self.patn = ellyWildcard.convert(dfls[0]) # encode Elly pattern
            if dfls[0] != '$':
                if self.patn == None or ellyWildcard.minMatch(self.patn) == 0:
                    print >> sys.stderr , '** bad link pattern:' , dfls[0]
                    raise ellyException.FormatFailure
            self.catg = None                          # defaults
            self.synf = None                          #
            sss = dfls[1].lower()                     # cannot be Unicode!
#           print 'sss=' , sss
            if sss != '-':                            # allow for no category
                syx = syntaxSpecification.SyntaxSpecification(syms,sss)
                if syx != None:
                    self.catg = syx.catg              # syntactic category
                    self.synf = syx.synf.positive     # syntactic features

            try:
                n = int(dfls[2])                      # next state for link
            except:
                raise ellyException.FormatFailure     # unrecognizable number

            if n < 0:                                 # final transition?
                if self.patn == u'\x00':
                    raise ellyException.FormatFailure # final state not allowed here
                pe = self.patn[-1]                    # if so, get last pattern element
                if ( pe != ellyWildcard.cALL and      # final pattern must end with * or $
                     pe != ellyWildcard.cEND ):
                    self.patn += ellyWildcard.cEND    # default is $
                    print >> sys.stderr , '** default $ added to pattern' , dfls

            self.nxts = n                             # specify next state
コード例 #2
0
    def __init__ ( self , syms , dfls ):

        """
        initialization

        arguments:
            self  -
            syms  - Elly grammatical symbol table
            dfls  - definition elements in list

        exceptions:
            FormatFailure on error
        """

#       print 'dfls=' , dfls
        ne = len(dfls)
#       print 'ne=' , ne
        if 3 > ne or ne > 5:                              # must have 3 to 5 elements
            raise ellyException.FormatFailure
        else:
            if dfls[0] == '\\0':
                self.patn = u'\x00'                       # special nul pattern
            elif ellyWildcard.numSpaces(list(dfls[0])) > 0:
                print >> sys.stderr , '** link pattern includes space:' , dfls[0]
                raise ellyException.FormatFailure
            else:
                self.patn = ellyWildcard.convert(dfls[0]) # encode Elly pattern
            if dfls[0] != '$':
                if self.patn == None or ellyWildcard.minMatch(self.patn) == 0:
                    print >> sys.stderr , '** bad link pattern:' , dfls[0]
                    raise ellyException.FormatFailure
#               print 'appended patn=' , list(self.patn) , '=' , len(self.patn)

            lastat = dfls[-1]
            self.catg = None                          # defaults
            self.synf = None                          #
            self.semf = None                          #
            self.bias = 0                             #
            sss = dfls[1].lower()                     # assumed not to be Unicode
#           print 'sss=' , sss
            if sss != '-':                            # allow for no category
                syx = syntaxSpecification.SyntaxSpecification(syms,sss)
                if syx != None:
                    if lastat != '-1':                    # not a stop state for matching
                        raise ellyException.FormatFailure # cannot have syntax here
                    self.catg = syx.catg              # syntactic category
                    self.synf = syx.synf.positive     # syntactic features

            if ne > 3:
                if lastat != '-1':                    # not a stop state for matching
                    raise ellyException.FormatFailure # cannot have semantics here
                sss = None if dfls[2] == '-' else dfls[2].lower()
            else:
                sss = None
#           print 'semantic features=' , sss
            sem = featureSpecification.FeatureSpecification(syms,sss,True)
            self.semf = sem.positive                  # get semantic features
#           print 'semf=' , self.semf

            if ne > 4:
                try:
                    self.bias = int(dfls[3])
                except ValueError:
                    raise ellyException.FormatFailure # unrecognizable bias

            try:
                n = int(lastat)                       # next state for link
            except ValueError:
                raise ellyException.FormatFailure     # unrecognizable number

#           print 'transition=' , n

            if n < 0:                                 # final transition?
                if self.patn == u'\x00':
                    raise ellyException.FormatFailure # final state not allowed here
                pe = self.patn[-1]                    # if so, get last pattern element
                if ( pe != ellyWildcard.cALL and      # final pattern must end with * or $
                     pe != ellyWildcard.cEND ):
                    self.patn += ellyWildcard.cEND    # default is $
                    print >> sys.stderr , '** final $ added to pattern' , list(self.patn)

            self.nxts = n                             # specify next state
コード例 #3
0
    def __init__(self, syms, dfls):
        """
        initialization

        arguments:
            self  -
            syms  - Elly grammatical symbol table
            dfls  - definition elements in list

        exceptions:
            FormatFailure on error
        """

        #       print 'dfls=' , dfls
        ne = len(dfls)
        #       print 'ne=' , ne
        if 3 > ne or ne > 5:  # must have 3 to 5 elements
            raise ellyException.FormatFailure
        else:
            if dfls[0] == '\\0':
                self.patn = u'\x00'  # special nul pattern
            elif ellyWildcard.numSpaces(list(dfls[0])) > 0:
                print >> sys.stderr, '** link pattern includes space:', dfls[0]
                raise ellyException.FormatFailure
            else:
                #               print 'do conversion'
                self.patn = ellyWildcard.convert(
                    dfls[0])  # encode Elly pattern
#           print 'patn=' , self.patn
            if dfls[0] != '$':
                if self.patn == None or ellyWildcard.minMatch(self.patn) == 0:
                    print >> sys.stderr, '** bad link pattern:', dfls[0]
                    raise ellyException.FormatFailure
#               print 'appended patn=' , list(self.patn) , '=' , len(self.patn)

            lastat = dfls[-1]
            self.catg = None  # defaults
            self.synf = None  #
            self.semf = None  #
            self.bias = 0  #
            sss = dfls[1].lower()  # assumed not to be Unicode
            #           print 'sss=' , sss
            if sss != '-':  # allow for no category
                syx = syntaxSpecification.SyntaxSpecification(syms, sss)
                if syx != None:
                    if not lastat in ['-1', '-2'
                                      ]:  # not a stop state for matching
                        raise ellyException.FormatFailure  # cannot have syntax here
                    self.catg = syx.catg  # syntactic category
                    self.synf = syx.synf.positive  # syntactic features

            if ne > 3:
                if lastat != '-1':  # not a stop state for matching
                    raise ellyException.FormatFailure  # cannot have semantics here
                sss = None if dfls[2] == '-' else dfls[2].lower()
            else:
                sss = None
#           print 'semantic features=' , sss
            sem = featureSpecification.FeatureSpecification(syms, sss, True)
            self.semf = sem.positive  # get semantic features
            #           print 'semf=' , self.semf

            if ne > 4:
                try:
                    self.bias = int(dfls[3])
                except ValueError:
                    raise ellyException.FormatFailure  # unrecognizable bias

            try:
                n = int(lastat)  # next state for link
            except ValueError:
                raise ellyException.FormatFailure  # unrecognizable number

#           print 'transition=' , n

            if n < 0:  # final transition?
                if self.patn == u'\x00':
                    raise ellyException.FormatFailure  # final state not allowed here
                if n == -1:
                    pe = self.patn[-1]  # if so, get last pattern element
                    if (pe != ellyWildcard.cALL
                            and  # final pattern must end with * or $
                            pe != ellyWildcard.cEND):
                        self.patn += ellyWildcard.cEND  # default is $
                        print >> sys.stderr, '** final $ added to pattern', list(
                            self.patn)

            self.nxts = n  # specify next state