示例#1
0
def build_grammar(w):
	"""
	Latejami grammar:

	"""
	g = Grammar("Latejami")

	g["sentence"] = Reference("topic") * OPTIONAL_CLOSURE + Reference("clause") | Reference("vocative-noun-phrase")


	g["topic"] = Reference("topic-particle") + Reference("argument")


	g["topic-particle"] = Reference("heavy-topicalization-particle") | Reference("reference-switching-particle")


	g["clause"] = Reference("disjunct") * KLEENE_CLOSURE + Reference("verb") + Reference("argument") * KLEENE_CLOSURE + Reference("valency-terminator") * OPTIONAL_CLOSURE


	g["argument"] = Reference("expression") | Reference("oblique-argument")


	g["oblique-argument"] = Reference("adverb") | Reference("case-tag") + Reference("expression")


	g["expression"] = Reference("noun-phrase") | Reference("clause")


	g["noun-phrase"] = Reference("noun") + Reference("noun-modifier") * KLEENE_CLOSURE
	g["noun-phrase"] = Reference("open-noun") + Reference("noun-modifier") * KLEENE_CLOSURE + Reference("expression")


	g["noun-modifier"] = Reference("light-modifier") | Reference("heavy-modifier")


	g["light-modifier"] = Reference("adjective")


	g["heavy-modifier"] = Reference("open-adjective") + Reference("expression")

	g["noun"] = WordCategoryFilter("N")
	g["open-noun"] = WordCategoryFilter("N", (CategoryFilter("in", ["P/F-s"]),))
	g["adjective"] = WordCategoryFilter("A")
	g["open-adjective"] = WordCategoryFilter("A", (CategoryFilter("in", ["P/F-s"]),))
	g["verb"] = WordCategoryFilter("V")
	g["adverb"] = WordCategoryFilter("D")
	g["case-tag"] = WordCategoryFilter("D", (CategoryFilter("in", ["P/F-s","P/F-d"]),))
	g["disjunct"] = WordCategoryFilter("D", (CategoryFilter("in", ["P/F-s","P/F-d"]),))
	g["vocative-noun-phrase"] =  WordCategoryFilter("N", (CategoryFilter("in", ["-we"]),))#????
	g["heavy-topicalization-particle"] = WordFilter(build_word(w["xojopa"]))
	g["reference-switching-particle"] = WordFilter(build_word(w["zunjopa"]))
	g["valency-terminator"] = WordFilter(build_word(w["jojope"]))
	g.compile(True)

	return g
示例#2
0
def build_grammar(w):
	"""
	Toki Pona grammar:

	"""
	def add_phrase(name, sep_key):
		"""
		<xs> ::= <x> + (<conj> + <x>)*
		"""
		g[name + "s"] = Reference(name) + Reference(name + "-extension") * KLEENE_CLOSURE
		g[name + "-extension"] = WordFilter(build_word(w[sep_key])) + Reference(name)

	g = Grammar("toki pona")


	g["sentence"] = Reference("sentence-adverb") * OPTIONAL_CLOSURE + Reference("subject") + Reference("predicates")
	g["sentence-adverb"] = Reference("noun-phrase") + WordFilter(build_word(w["la_sep"]))

	g["subject"] = Reference("pronoun-phrase") 
	g["subject"] = Reference("noun-phrases") + WordFilter(build_word(w["li_sep"]))
	add_phrase("noun-phrase", "en_conj")

	g["pronoun-phrase"] = WordFilter(build_word(w["mi_n"])) | WordFilter(build_word(w["sina_n"]))
	g["noun-phrase"] = WordCategoryFilter("n") + Reference("adjectives") * OPTIONAL_CLOSURE
	g["noun-phrase"] = WordCategoryFilter("n") + WordFilter(build_word(w["pi_sep"])) + WordCategoryFilter("n") + Reference("adjectives")

	add_phrase("predicate", "li_sep")
	g["predicate"] = Reference("intransitive-predicate") | Reference("transitive-predicate") | Reference("copulative-predicate")
	
	g["intransitive-predicate"] = WordCategoryFilter("v", ("intr", )) + Reference("adverbs") * OPTIONAL_CLOSURE + Reference("complement") * KLEENE_CLOSURE
	g["transitive-predicate"] = WordCategoryFilter("v", ("tr", )) + Reference("adverbs") * OPTIONAL_CLOSURE + Reference("direct-object") * POSITIVE_CLOSURE + Reference("complement") * KLEENE_CLOSURE
	g["copulative-predicate"] = Reference("noun-phrase") | Reference("adjectives") * OPTIONAL_CLOSURE + Reference("complement") * KLEENE_CLOSURE

	add_phrase("adjective", "en_conj")
	add_phrase("adverb", "en_conj")
	g["direct-object"] = WordFilter(build_word(w["e_sep"])) + Reference("noun-phrase")
	g["adjective"] = WordCategoryFilter("mod")
	g["adverb"] = WordCategoryFilter("mod")
	g["complement"] = WordCategoryFilter("prep") + Reference("noun-phrase") 

	g.compile()
	
	return g