Ejemplo n.º 1
0
	def do_form(self, lemma, categories, words = ()):
		"""
		Generate the specified inflected form for the lemma, preserving the given words.
		When a word form is provided, it is returned; when not, it is generated applying the appropriate inflection form.

		@param lemma: The lemma to inflect.
		@type lemma: Lemma
		@param categories: The categories of the word to generate.
		@type categories: tuple of str/CategoryFilter
		@param words: The inflected forms (usually a list of irregular forms).
		@type words: sequence of Word
		@return: The inflected word form for the lemma.
		@rtype: Word
		@raise InflectionError: If the inflection form can not accept the lemma respecting mandatory steps.
		@raise TransformSyntaxError: If the substitution pattern in some step can not be compiled.
		"""
		word = None
		#Find is there is a default form
		for w in words:
			if CategoryFilter.test(categories, w.categories):
				word = w
				break
		if word is None:
			#Get the form == dict of transforms
			form = self.__forms[categories]
			s = None
			#Iterate over transforms
			for transform in form:
				s = transform(lemma, words)
				if s is not None:
					break
			if s is None:
				raise InflectionError("Inflection can not generate %s for lemma '%s'" % (Utilities.tuple_str(categories), `lemma`))
			word = Word(s, lemma, categories)
		return word
Ejemplo n.º 2
0
	def __call__(self, lemma, words = ()):
		"""
		Apply the transform to the lemma, preserving the given words.
		When a word form is provided, it is returned; when not, it is generated applying the appropriate inflection form.
		If the transform does not accept the lemma or violates any mandatory step, nothing is returned.

		@param lemma: The lemma to inflect.
		@type lemma: Lemma
		@param words: The inflected forms (usually a list of irregular forms).
		@type words: sequence of Word
		@return: The inflected form (string) for the lemma.
		@rtype: str
		@raise TransformSyntaxError: If the substitution pattern in some step can not be compiled.
		"""
		if not CategoryFilter.test(self.lemma_categories, lemma.categories):
			return None
		if self.based_on == BASED_ON_ENTRY_FORM:
			s = lemma.entry_form
		else:
			s = None
			for w in words:
				if CategoryFilter.test(self.based_on, w.categories):
					s = w.form
					break
			if not s:
				w = self.__parent.do_form(lemma, self.based_on, words)
				s = w.form
		if not s:
			return None
		if DEFECTIVE == s:
			return DEFECTIVE #propagation
		if not self.__cco.search(s):
			return None
		for r, cre, substitution, mandatory in self.__steps:
			if cre.search(s):
				try:
					s = cre.sub(substitution, s)
				except:
					raise TransformSyntaxError("Invalid form %s for %s" % (`substitution`, `r`))
			elif mandatory:
				return None
		return s
Ejemplo n.º 3
0
	def accept(self, lemma):
		"""
		Verify if the inflection accepts a lemma.

		@param lemma: The lemma to verify.
		@type lemma: Lemma
		@return: True if the inflection can accept the lemma.
		@rtype: bool
		"""
		cco = self.__cco
		if self.p_o_s == lemma.p_o_s and (cco is None or cco.search(lemma.entry_form)) and CategoryFilter.test(self.lemma_categories, lemma.categories):
			return True
		else:
			return False