import re


def resolve_cardinality(mark,lex):
	"""
	Find cardinality for Markable based on numerical modifiers or number words

	:param mark: The :class:`.Markable` to resolve agreement for
	:param lex: the :class:`.LexData` object with gazetteer information and model settings
	:return: Cardinality as float, zero if unknown
	"""
	def check_card(mod):
		if mod in lex.numbers:
			return int(lex.numbers[mod][0])
		elif mod.lower() in lex.numbers:
			return int(lex.numbers[mod.lower()][0])
		else:
			thousand_sep = r"\." if lex.filters["thousand_sep"] == "." else lex.filters["thousand_sep"]
			pure_number_candidate = re.sub(thousand_sep,"",mod)

			decimal_sep = lex.filters["decimal_sep"]
			if decimal_sep != ".":
				pure_number_candidate = re.sub(decimal_sep,".",pure_number_candidate)
			if re.match("^(\d+(\.\d+)?|(\.\d+))$",pure_number_candidate) is not None:
				return float(pure_number_candidate)
			else:
				parts = re.match("^(\d+)/(\d+)$",pure_number_candidate)
				if parts is not None: # Fraction with slash division
					numerator = float(parts.groups()[0])
					denominator = float(parts.groups()[1])
					return numerator/denominator

	for m in mark.head.modifiers:
		card = check_card(m.text)
		if card is not None:
			return card
	card = check_card(mark.head.text)
	if card is not None:
		return card
	card = check_card(mark.head.lemma)
	if card is not None:
		return card

	return 0