import io


def readmodifierrules(filename):
	"""Read a file containing heuristic rules for marking modifiers.

	Example line: ``S *-MOD``, which means that for an S
	constituent, any child with the MOD function tag is a modifier.
	A default rule can be specified by using * as the first label, which
	always matches (in addition to another matching rule, if any).
	If none of the rules matches, a non-terminal is assumed to be a complement.
	"""
	modifierrules = {}
	with io.open(filename, encoding='utf8') as inp:
		for line in inp:
			line = line.strip().upper()
			if line and not line.startswith("%"):
				label, modifiers = line.split(None, 1)
				if label in modifierrules:
					raise ValueError('duplicate rule for %r (each label'
							' should occur at most once in the file)' % label)
				modifierrules[label] = modifiers.split()
	return modifierrules