def test_resolve(self): word = topic([ieml('wa.')]) p = path('r0') elems = resolve(word, p) self.assertSetEqual(elems, {ieml('wa.')}) worda = topic([ieml('wu.')]) wordm = topic([ieml('we.')]) s = fact([(word, worda, wordm)]) p = path('sa:r') elems = resolve(s, p) self.assertSetEqual(elems, {ieml('wu.')}) p = path('sa0+s0+sm0') elems = resolve(s, p) self.assertSetEqual(elems, {word, wordm, worda}) t = text([s, word]) p = path('t') elems = resolve(t, p) self.assertSetEqual(elems, {s, word}) p = path('t1') elems = resolve(t, p) self.assertSetEqual(elems, {s})
def setUp(self): self.morpheme_a = [ term("E:A:T:."), term("E:.S:.wa.-"), term("E:.-S:.o.-t.-'") ] self.morpheme_b = [term("a.i.-"), term("i.i.-")] self.word_a = topic(self.morpheme_a, self.morpheme_b) self.word_b = topic( [term("E:A:T:."), term("E:.-S:.o.-t.-'"), term("E:.S:.wa.-")], [term("a.i.-"), term("i.i.-")])
def test_topics_reordering(self): morpheme_a = [ term("E:A:T:."), term("E:.-S:.o.-t.-'"), term("E:.S:.wa.-") ] morpheme_b = [ term("E:A:T:."), term("E:.S:.wa.-"), term("E:.-S:.o.-t.-'") ] self.assertTrue(topic(morpheme_a) == topic(morpheme_b))
def get_topics_list(): #this list is already sorted terms_list = [term("E:A:T:."), term("E:.S:.wa.-"), term("E:.-S:.o.-t.-'"), term("a.i.-"), term("i.i.-"), term("u.M:M:.-")] # a small yield to check the word before returning it :-° for t in terms_list: word_obj = topic([t]) yield word_obj
def test_topics_hashing(self): """Verifies words can be used as keys in a hashmap""" new_word = topic( [term("E:A:T:."), term("E:.-S:.o.-t.-'"), term("E:.S:.wa.-")]) word_hashmap = {new_word: 1, self.word_a: 2} self.assertTrue(self.word_b in word_hashmap)
def test_deference(self): rand = RandomPoolIEMLObjectGenerator() w0 = rand.topic() self.assertEqual(w0['r0'], w0.root[0]) self.assertEqual(w0['r'], w0.root) w0 = topic([w0['r0']]) self.assertEqual(w0['f'], w0.flexing)
def test_rules(self): rules0 = [(path('r0'), ieml('wa.'))] obj = resolve_ieml_object(*zip(*rules0)) self.assertEqual(obj, topic([ieml('wa.')])) rules1 = [(path('r1'), ieml('wa.')), (path('r'), ieml('I:')), (path('f0'), ieml('we.'))] obj = resolve_ieml_object(*zip(*rules1)) word1 = topic([ieml('I:'), ieml('wa.')], [ieml('we.')]) self.assertEqual(obj, word1) self.assertEqual(resolve_ieml_object(enumerate_paths(obj)), obj) r = RandomPoolIEMLObjectGenerator(level=Text) t = r.text() self.assertEqual(t, resolve_ieml_object(enumerate_paths(t))) rules = [(path('r1'), ieml('wa.')), (path('r'), ieml('I:')), (path('f0'), ieml('we.'))] obj = resolve_ieml_object(*zip(*rules)) self.assertEqual(obj, topic([ieml('I:'), ieml('wa.')], [ieml('we.')]))
def test_invalid_creation(self): def test(rules, expected=None): if expected: try: usl(rules) except IEMLObjectResolutionError as e: self.assertListEqual(e.errors, expected) else: self.fail() else: with self.assertRaises(IEMLObjectResolutionError): usl(rules) # missing node definition on sm0 test([('s:r', ieml('[we.]')), ('sa0:r', ieml('[wa.]'))], [('s0m0', "Missing node definition.")]) # empty rules test([], [('', "Missing node definition.")]) # multiple def for a node test([('r0', ieml('[wa.]')), ('r0', ieml('[we.]'))], [( 'r0', 'Multiple definition, multiple ieml object provided for the same node.' )]) # missing index on text test([('t:r', ieml('[we.]')), ('t2:r', ieml('[wa.]'))], [('', "Index missing on text definition.")]) # missing index on word test([('r2', ieml('[we.]')), ('r', ieml('[wa.]'))], [('', "Index missing on topic definition.")]) test([('s:r', ieml('[wa.]')), ('sm:r', ieml('[we.]')), ('sa1:r', ieml('[wu.]'))], [('s0a0', 'Missing node definition.')]) # incompatible path test([('t:r', ieml('[wa.]')), ('s:f', ieml('[we.]'))], [('', 'No definition, no type inferred on rules list.')]) # mulitple errors test([("t0:s:f0", ieml('[wa.]')), ("t0:sa:r", ieml('[a.]')), ('t2:r', ieml('[we.]')), ("t0:sm1", topic([ieml('[wu.]')]))], [('t0:s0', 'No root for the topic node.'), ('t0:s0m0', 'Missing node definition.'), ('t1', 'Missing node definition.')])
def __init__(self, children, literals=None): _children = [ topic([c]) if isinstance(c, Word) else c for c in children ] _children = list( chain([c for c in _children if not isinstance(c, Text)], *(c.children for c in _children if isinstance(c, Text)))) self.children = sorted(set(_children)) dictionary_version = self.children[0].dictionary_version if any(e.dictionary_version != dictionary_version for e in self.children): raise InvalidIEMLObjectArgument( Fact, "Incompatible dictionary version in the list of usls") super().__init__(dictionary_version, literals=literals)
from ieml.dictionary import term from ieml.grammar import topic, fact, usl root = topic([term("i.i.-"), # fabriquer term("a.i.-")], # vendre [term("E:S:.o.-"), # vouloir futur term("E:S:.wa.-"), # 1ere personne pluriel term("E:A:T:.")]) # beaucoup objects = [ { 'usl': usl(root), 'tags': { 'FR': "Nous avons l'intention de fabriquer et de vendre beaucoup", 'EN': "We intend to manufacture and sell a lot" }, 'keywords': { 'FR': [], 'EN': [] } }, { 'usl': usl(fact([( root, topic([term("t.i.-s.i.-'u.T:.-U:.-'wo.-',B:.-',_M:.-',_;")]), # véhicule a roue sans conducteur topic([term("E:E:T:.")]) # COD ), ( root, topic([term("S:.-'B:.-'n.-S:.U:.-',")]), # Europe topic([term("E:T:.f.-")]) # dans
def _resolve_ctx(rules): """ Resolve the context of the rules (the type of this element), and building the ieml element. :param rules: :return: """ if not rules: raise ResolveError("Missing node definition.") # if rules == [(None, e)] --> e if len(rules) == 1 and rules[0][0] is None: return rules[0][1] if any(r[0] is None for r in rules): raise ResolveError( "Multiple definition, multiple ieml object provided for the same node." ) if any(not isinstance(r[0], Path) for r in rules): raise ResolveError("Must have only path instance.") # resolve all the possible types for this element r0 = rules[0] types = _inferred_types(*r0) for r in rules[1:]: types = types.intersection(_inferred_types(*r)) if not types: raise ResolveError("No definition, no type inferred on rules list.") if len(types) > 1: raise ResolveError( "Multiple definition, multiple type inferred on rules list.") type = next(types.__iter__()) if type == Topic: error, deps = _build_deps_topic(rules) if error: return flexing = None if deps['f']: flexing = deps['f'] if not deps['r']: raise ResolveError("No root for the topic node.") return topic(deps['r'], flexing) if type == Text: error, deps = _build_deps_text(rules) if error: return return text(deps) if type in (Theory, Fact): error, deps = _build_deps_tree_graph(rules) if error: return if type == Fact: clauses = [] for s, a, m in deps: clauses.append((s, a, m)) return fact(clauses) else: clauses = [] for s, a, m in deps: clauses.append((s, a, m)) return theory(clauses) raise ResolveError("Invalid type inferred %s" % type.__name__)
from ieml.dictionary import term from ieml.grammar import topic, fact, usl root = topic( [ term("i.i.-"), # fabriquer term("a.i.-") ], # vendre [ term("E:S:.o.-"), # vouloir futur term("E:S:.wa.-"), # 1ere personne pluriel term("E:A:T:.") ]) # beaucoup objects = [ { 'usl': usl(root), 'tags': { 'FR': "Nous avons l'intention de fabriquer et de vendre beaucoup", 'EN': "We intend to manufacture and sell a lot" }, 'keywords': { 'FR': [], 'EN': [] } }, { 'usl': usl( fact([ (
def test_create_topic(self): a = topic([usl('[wa.]'), usl('[we.]')]) b = topic(reversed([usl('[wa.]'), usl('[we.]')])) self.assertEqual(a, b) self.assertEqual(str(a), str(b))
def test_promotion(self): self.assertIsInstance(usl('[A:]'), Word) self.assertIsInstance(topic(['[A:]']), Topic) self.assertIsInstance(term('[A:]'), Term)
def test_topics_with_different_substance_comparison(self): word_a, word_b = topic(self.morpheme_a), topic(self.morpheme_b) # true because term("E:A:T:.") < term("a.i.-") self.assertTrue(word_a < word_b)
def test_replace(self): u = topic([usl('[M:]')]) u2 = replace_paths(u, {'r0': '[S:]'}) self.assertEqual(u2, topic([usl('[S:]')]))
def _resolve_ctx(rules): """ Resolve the context of the rules (the type of this element), and building the ieml element. :param rules: :return: """ if not rules: raise ResolveError("Missing node definition.") # if rules == [(None, e)] --> e if len(rules) == 1 and rules[0][0] is None: return rules[0][1] if any(r[0] is None for r in rules): raise ResolveError("Multiple definition, multiple ieml object provided for the same node.") if any(not isinstance(r[0], Path) for r in rules): raise ResolveError("Must have only path instance.") # resolve all the possible types for this element r0 = rules[0] types = _inferred_types(*r0) for r in rules[1:]: types = types.intersection(_inferred_types(*r)) if not types: raise ResolveError("No definition, no type inferred on rules list.") if len(types) > 1: raise ResolveError("Multiple definition, multiple type inferred on rules list.") type = next(types.__iter__()) if type == Topic: error, deps = _build_deps_topic(rules) if error: return flexing = None if deps['f']: flexing = deps['f'] if not deps['r']: raise ResolveError("No root for the topic node.") return topic(deps['r'], flexing) if type == Text: error, deps = _build_deps_text(rules) if error: return return text(deps) if type in (Theory, Fact): error, deps = _build_deps_tree_graph(rules) if error: return if type == Fact: clauses = [] for s, a, m in deps: clauses.append((s, a, m)) return fact(clauses) else: clauses = [] for s, a, m in deps: clauses.append((s, a, m)) return theory(clauses) raise ResolveError("Invalid type inferred %s"%type.__name__)