def test_conf(): from sagas.nlu.anal import build_anal_tree, Doc, AnalNode from sagas.nlu.anal_corpus import model_info f = build_anal_tree('Ördek filin üzerinde.', 'tr', 'stanza') # f.draw() assert 'AnalNode_tr' == type(f).__name__ assert 'Doc' == type(f.doc).__name__
def test_zh(): from sagas.nlu.anal import build_anal_tree, Doc, AnalNode f = build_anal_tree('附近有什么好吃的饭馆', 'zh', 'ltp') f.draw() assert (f / 'obj').deprel == 'obj' assert ['饭馆', '饭馆' ] == [f.resolve_rel("obj").text, f.resolve_rels("obj*")[0].text]
def test_and_or(): f = build_anal_tree('list some restaurants', 'en', 'stanza') f.draw() target = f.model().target print(target.text, target.types) assert target.match('InstitutePlace|场所') assert target.match('location: reside|住下; location: eat|吃') assert not target.match('+location: reside|住下; location: eat|吃')
def test_match_absent_part(): f = build_anal_tree('Nós estudamos.', 'pt', 'stanza') f.draw() r = match(f, behave_(_, _1 << 'study', _2 << _, _), lambda arg, v1, v2: [v1.text, v2.text], behave_(_, _1 << 'study', _, _), lambda arg, v1: [v1.text], behave_(_, _1 << 'learn', _2 << _, _), lambda arg, v1, v2: [v1.text, v2.text], _, None) assert ['estudamos'] == r
def test_match_embed_expr(): f = build_anal_tree('We expect them to change their minds', 'en', 'stanza') f.draw() r = match( f, behave_(_, _1 << 'expect|期望', _, _, rel_('xcomp') == behave_(_, _2 << 'change|变', 'mind')), lambda arg, v1, v2: [v1.text, v2.text], _, None) assert ['expect', 'change'] == r
def test_restaurants(sents, lang, engine): f = build_anal_tree(sents, lang, engine) f.draw() target = f.model().target agents = match_agents(target) assert len(agents) == 1 rs = agents[0](f) pprint(rs) assert len(rs) > 1
def descrip(self, sents, lang, engine=None): """ $ python -m sagas.nlu.anal_corpus descrip 'Karpet di kantor saya abu-abu.' id $ sid 'Celana ini bisa diperbesar.' :param sents: :param lang: :param engine: :return: """ from sagas.nlu.anal import build_anal_tree, Doc, AnalNode from sagas.conf.conf import cf f = build_anal_tree(sents, lang, cf.engine(lang)) f.draw() model = f.model() model_info(model)
def test_behave_matcher(): f = build_anal_tree('We expect them to change their minds', 'en', 'stanza') f.draw() model = f.rels('xcomp')[0].model() model_info(model) assert (f/'nsubj').match(_) assert (f/'obj').match('pos:pron') assert (f / 'xcomp').match(behave_(_, 'change|变', 'mind')) # assert (f/'xcomp').match(behave_(_, 'change|变', 'mind') | # behave_(_, 'change|变', 'mental|精神')) assert f.match(behave_(_, 'expect|期望', _, _)) assert not f.match(behave_(_, 'love', _, _)) assert f.match(behave_(_, 'expect|期望', _, _, rel_('xcomp')==behave_(_, 'change|变', 'mind'))) assert f==behave_(_, 'expect|期望', _, _)
def test_match_expr(): f = build_anal_tree('Note the output is a string', 'en', 'stanza') f.draw() r=match(f, behave_(_, 'perception|感知', _, _), lambda arg: 'perception', behave_(_, 'perception|感知', desc_('result|结果', _), _), lambda arg: arg.text, _, None ) assert 'perception'==r r = match(f, behave_(_, 'unknown', _, _), lambda arg: 'unknown', # behave_(_, 'perception|感知', desc_('result|结果', _), _), # lambda arg: [arg.behave.text], behave_(_, 'perception|感知', _1<<desc_('result|结果', _), _), lambda arg, v1: [arg.behave.text, v1.target.text], _, None ) assert ['Note', 'output'] == r # assert ['Note'] == r r = match(f/'ccomp'/'nsubj', 'pos:noun', lambda arg: arg.text, _, None ) assert 'output' == r r = match(f / 'ccomp' / 'nsubj', 'pos:pron', lambda arg: arg.text, _, None ) assert r is None r = match(f, behave_(_, 'perception|感知', _1<<_, _), lambda arg, v1: v1.text, _, None ) assert 'string' == r r = match(f, behave_(_, _1<<'perception|感知', _2 << _, _), lambda arg, v1, v2: [v1.text, v2.text], _, None ) assert ['Note', 'string'] == r
def do(self, sents, lang='en'): """ $ python -m sagas.modules.life.agents do 'list some restaurants' en :param sents: :param lang: :return: """ f = build_anal_tree(sents, lang, cf.engine(lang)) tc.emp('cyan', '❏', f.doc.sents) f.draw() target = f.model().target tc.emp('red', '☞', target.text, target.types) for m in global_meta_ls(): print('-' * 25) succ=match_agent(target, m, verbose=True) if succ: r = m(f) if r: pprint(r) else: print('result -> _')
def test_match_expr(): f = build_anal_tree('Note the output is a string', 'en', 'stanza') f.draw() r = match(f, behave_(_, _1 << 'perception|感知', _2 << _, _), lambda arg, v1, v2: [v1.text, v2.text], _, None) assert ['Note', 'string'] == r r = match( f, behave_(_, _1 << 'unknown', _2 << _, _), lambda *args: 'unknown', # behave_(_, 'perception|感知', _1 << desc_('result|结果', _), _), # lambda arg, v1: [arg.behave.text, v1.target.text], behave_(_, _1 << 'perception|感知', desc_('result|结果', _), _), lambda arg, v1: [arg.behave.text, v1.text], behave_(_, _1 << 'unknown2', _2 << _, _), lambda *args: 'unknown2', _, None) assert ['Note', 'Note'] == r assert 0 == len(_1.reqs)
def digest_verb(sents, lang, engine='stanza'): f = build_anal_tree(sents, lang, engine) words = findall_by_attr(f, name='upos', value='VERB') if words: print(sents, len(words)) rs = [] for w in words: rs.append(proc_verb_subs(w)) def proc_text(t): if lang in ('ko'): return get_contrast(t, lang) return t succ = any(rs) cl = 'blue' if succ else 'white' tc.emp( cl, RenderTree(f, style=AsciiStyle()).by_attr( lambda n: f"{n.dependency_relation}: {proc_text(n.text)} {n.upos}")) return succ return False
def test_term(sents, lang, engine): f = build_anal_tree(sents, lang, engine) f.draw() assert f.is_term('ref') pprint(f.links[0].as_json()) assert f.links[0].name == 'ProductType'
def test_ner_rasa(): # run: simple/rasa-serv.sh from sagas.nlu.anal import build_anal_tree, Doc, AnalNode f = build_anal_tree('i would like to find an expensive restaurant', 'en', 'stanza') assert f.get_by_index(7).ner.value == 'hi'
def test_ja(): from sagas.nlu.anal import build_anal_tree, Doc, AnalNode f = build_anal_tree('コーヒーを ミルク付きで お願い します 。', 'ja', 'knp') f.draw() assert ['珈琲', '珈琲'] == [f.resolve_rel("obj").lemma, f.resolve_rels("obj*")[0].lemma]
def test_as_desc(): from sagas.nlu.anal import build_anal_tree, Doc, AnalNode f = build_anal_tree('Nuestro horario es de nueve a cinco.', 'es', 'stanza') desc = f.as_desc() assert (desc.subj.text, desc.subj_spec, desc.aux.lemma, desc.desc.as_num()) \ == ('horario', 'schedule', 'ser', 9)
def test_desc_matcher(): f = build_anal_tree('Note the output is a string', 'en', 'stanza') f.draw() assert f == behave_(_, 'perception|感知', _, _) assert f == behave_(_, 'perception|感知', desc_('result|结果', _), _) assert not f == behave_(_, 'perception|感知', desc_('food', _), _)
def test_term(sents, lang, engine): f = build_anal_tree(sents, lang, engine) f.draw() # target = f.model().target assert (f / 'nsubj').is_term('typ')