Exemple #1
0
 def perform_regex_tests(self, sublex):
     testResult = 0
     for rtKey in sorted(self.regexTests):
         result = lexeme.check_for_regex(sublex, self.regexTests[rtKey][0],
                                         self.errorHandler)
         testResult = testResult * 2 + int(result)
     return testResult
Exemple #2
0
 def apply(self, wf):
     if wf.stem != self.stem and wf.lemma != self.lemma:
         return None
     for rxTest in self.searchFields:
         if not lexeme.check_for_regex(wf, rxTest, errorHandler=self.errorHandler,
                                       checkWordform=True):
             return None
     wfNew = copy.deepcopy(wf)
     wfNew.otherData += self.addFields
     return wfNew
Exemple #3
0
    def fork_regex(self, sublex):
        """
        Write a regex-free version of self to the grammar if needed.
        Return the name of the paradigm.
        """
        if self.regexTests is None:
            self.build_regex_tests()
        if len(self.regexTests) == 0:
            if self.name not in grammar.Grammar.paradigms:
                grammar.Grammar.paradigms[self.name] = copy.deepcopy(self)
            return self.name

        testResult = self.perform_regex_tests(sublex)
        newParaName = self.name + '=' + str(testResult)
        if newParaName in grammar.Grammar.paradigms:
            return newParaName

        # If there is no such paradigm, make it:
        newPara = copy.deepcopy(self)
        testResult = 0
        flex2remove = set()
        for rtKey in sorted(newPara.regexTests):
            result = lexeme.check_for_regex(sublex,
                                            newPara.regexTests[rtKey][0],
                                            self.errorHandler)
            if not result:
                flex2remove |= newPara.regexTests[rtKey][1]
            testResult = testResult * 2 + int(result)
        for iFlex in sorted(flex2remove, reverse=True):
            newPara.flex.pop(iFlex)
        newPara.name = newParaName
        newPara.regexTests = {}
        for flex in newPara.flex:
            flex.regexTests = []
        grammar.Grammar.paradigms[newParaName] = newPara
        return newParaName