def test_nested_and_query_entity_equality(): """Tests NestedEntity and QueryEntity equality operations""" entity_a = NestedEntity( ("Entity", "Entity", "entity"), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity("text", "type", "role", "value", "display"), ) entity_b = QueryEntity( ("Entity", "Entity", "entity"), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity("text", "type", "role", "value", "display"), ) assert entity_a == entity_b assert not entity_a != entity_b entity_a = NestedEntity( ("Entity123", "Entity", "entity"), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity("text", "type", "role", "value", "display"), ) entity_b = QueryEntity( ("Entity", "Entity", "entity"), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity("text", "type", "role", "value", "display"), ) assert not entity_a == entity_b assert entity_a != entity_b
def test_dump_group_with_role(query_factory): """Tests dumping a query with an entity group with role type""" query_text = "a large latte with nonfat milk please" query = query_factory.create_query(query_text) size = QueryEntity.from_query(query, Span(2, 6), entity_type="size") option = QueryEntity.from_query(query, Span(19, 29), entity_type="option", role="beverage") product = QueryEntity.from_query( query, Span(8, 12), entity_type="dish-type", role="beverage", children=(size, option), ) processed_query = ProcessedQuery(query, entities=[size, product, option]) markup_text = ("a [{large|size} {latte|dish-type|beverage} with " "{nonfat milk|option|beverage}|dish-type] please") entity_text = ("a {large|size} {latte|dish-type|beverage} with " "{nonfat milk|option|beverage} please") group_text = "a [large latte with nonfat milk|dish-type] please" assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_group=True) == entity_text assert (markup.dump_query(processed_query, no_entity=True, no_role=True) == group_text) assert (markup.dump_query(processed_query, no_group=True, no_entity=True, no_role=True) == query_text)
def test_query_entity_equality(): """Tests query entity equality""" entity_a = QueryEntity(('Entity', 'Entity', 'entity'), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity('text', 'type', 'role', 'value', 'display')) entity_b = QueryEntity(('Entity', 'Entity', 'entity'), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity('text', 'type', 'role', 'value', 'display')) assert entity_a == entity_b
def test_dump_entities(query_factory): """Tests dumping a basic query with two entities""" query_text = 'When does the Elm Street store close on Monday?' query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query(query, Span(14, 23), entity_type='store_name'), QueryEntity.from_query(query, Span(40, 45), entity_type='sys_time') ] processed_query = ProcessedQuery(query, entities=entities) markup_text = 'When does the {Elm Street|store_name} store close on {Monday|sys_time}?' assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_entity=True) == query_text
def test_dump_multi_nested(query_factory): """Tests dumping a query with multiple nested system entities""" query_text = "show me houses between 600,000 and 1,000,000 dollars" query = query_factory.create_query(query_text) lower = NestedEntity.from_query(query, Span(8, 14), parent_offset=15, entity_type="sys_number") upper = NestedEntity.from_query(query, Span(20, 28), parent_offset=15, entity_type="sys_number") raw_entity = Entity( "between 600,000 dollars and 1,000,000", "price", value={"children": [lower, upper]}, ) entities = [QueryEntity.from_query(query, Span(15, 51), entity=raw_entity)] processed_query = ProcessedQuery(query, entities=entities) markup_text = ("show me houses {between {600,000|sys_number} and " "{1,000,000|sys_number} dollars|price}") assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_group=True) == markup_text assert markup.dump_query(processed_query, no_entity=True) == query_text
def test_query_entity_equality(): """Tests query entity equality""" entity_a = QueryEntity( ("Entity", "Entity", "entity"), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity("text", "type", "role", "value", "display"), ) entity_b = QueryEntity( ("Entity", "Entity", "entity"), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity("text", "type", "role", "value", "display"), ) assert entity_a == entity_b
def test_dump_group_nested_2(query_factory): """Tests dumping a query with nested entity groups""" query_text = "Can I get one curry sauce with my rice ball with house salad" query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query( query, Span(10, 12), entity_type="sys_number", role="quantity" ), QueryEntity.from_query(query, Span(14, 24), entity_type="option"), QueryEntity.from_query(query, Span(34, 59), entity_type="dish"), ] entities[1] = entities[1].with_children((entities[0],)) entities[2] = entities[2].with_children((entities[1],)) processed_query = ProcessedQuery(query, entities=entities) markup_text = ( "Can I get [[{one|sys_number|quantity} {curry sauce|option}|option] " "with my {rice ball with house salad|dish}|dish]" ) entity_text = ( "Can I get {one|sys_number|quantity} {curry sauce|option} " "with my {rice ball with house salad|dish}" ) role_text = ( "Can I get {one|quantity} curry sauce " "with my rice ball with house salad" ) group_text = ( "Can I get [[one curry sauce|option] " "with my rice ball with house salad|dish]" ) assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_group=True) == entity_text assert ( markup.dump_query(processed_query, no_group=True, no_entity=True) == role_text ) assert ( markup.dump_query(processed_query, no_entity=True, no_role=True) == group_text ) assert ( markup.dump_query(processed_query, no_group=True, no_entity=True, no_role=True) == query_text )
def test_dump_groups(query_factory): """Tests dumping a query with multiple top level entity groups""" query_text = ( "Order one large Tesora with medium cream from Philz in Downtown Sunnyvale" ) query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query(query, Span(6, 8), entity_type="quantity"), QueryEntity.from_query(query, Span(10, 14), entity_type="size"), QueryEntity.from_query(query, Span(16, 21), entity_type="product"), QueryEntity.from_query(query, Span(28, 33), entity_type="size"), QueryEntity.from_query(query, Span(35, 39), entity_type="option"), QueryEntity.from_query(query, Span(46, 50), entity_type="store"), QueryEntity.from_query(query, Span(55, 72), entity_type="location"), ] entities[4] = entities[4].with_children((entities[3], )) entities[2] = entities[2].with_children( (entities[0], entities[1], entities[4])) entities[5] = entities[5].with_children((entities[6], )) processed_query = ProcessedQuery(query, entities=entities) markup_text = ("Order [{one|quantity} {large|size} {Tesora|product} with " "[{medium|size} {cream|option}|option]|product] from " "[{Philz|store} in {Downtown Sunnyvale|location}|store]") assert markup.dump_query(processed_query) == markup_text
def test_dump_entity(query_factory): """Tests dumping a basic query with an entity""" query_text = "When does the Elm Street store close?" query = query_factory.create_query(query_text) entities = [QueryEntity.from_query(query, Span(14, 23), entity_type="store_name")] processed_query = ProcessedQuery(query, entities=entities) markup_text = "When does the {Elm Street|store_name} store close?" assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_entity=True) == query_text
def test_create_entity_from_query(query_factory): """Tests the QueryEntity generated has the correct character indices based on raw query""" query = query_factory.create_query("!!Connect me with Paul's meeting room please.") norm_span = Span(16, 19) entity = QueryEntity.from_query( query, normalized_span=norm_span, entity_type="test_type" ) assert entity.span.start == 18 assert entity.span.end == 21
def test_bootstrap_query_with_entities(query_factory): query_text = "Can I get one curry sauce with my rice ball with house salad" query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query( query, Span(10, 12), entity_type="sys_number", role="quantity" ), QueryEntity.from_query(query, Span(14, 24), entity_type="option"), QueryEntity.from_query(query, Span(34, 59), entity_type="dish"), ] entities[1] = entities[1].with_children((entities[0],)) entities[2] = entities[2].with_children((entities[1],)) confidence = { "domains": {"food": 0.95, "music": 0.05}, "intents": {"get_comestibles": 0.99, "reorder": 0.01}, "entities": [{"sys_number": 0.9}, {"option": 0.99}, {"dish": 0.65}], "roles": [{"quantity": 0.8, "quality": 0.2}, None, None], } processed_query = ProcessedQuery( query, domain="food", intent="get_comestibles", entities=entities, confidence=confidence, ) bootstrap_data = markup.bootstrap_query_row(processed_query, show_confidence=True) expected_data = { "query": ( "Can I get [[{one|sys_number|quantity} {curry sauce|option}|option] " "with my {rice ball with house salad|dish}|dish]" ), "domain": "food", "domain_conf": 0.95, "intent": "get_comestibles", "intent_conf": 0.99, "entity_conf": 0.65, "role_conf": 0.8, } assert bootstrap_data == expected_data
def test_nested_and_query_entity_equality(): """Tests NestedEntity and QueryEntity equality operations""" entity_a = NestedEntity(('Entity', 'Entity', 'entity'), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity('text', 'type', 'role', 'value', 'display')) entity_b = QueryEntity(('Entity', 'Entity', 'entity'), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity('text', 'type', 'role', 'value', 'display')) assert entity_a == entity_b assert not entity_a != entity_b entity_a = NestedEntity(('Entity123', 'Entity', 'entity'), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity('text', 'type', 'role', 'value', 'display')) entity_b = QueryEntity(('Entity', 'Entity', 'entity'), (Span(0, 5), ) * 3, (Span(0, 0), ) * 3, Entity('text', 'type', 'role', 'value', 'display')) assert not entity_a == entity_b assert entity_a != entity_b
def test_dump_group(query_factory): """Tests dumping a query with an entity group""" query_text = 'a large latte with nonfat milk please' query = query_factory.create_query(query_text) size = QueryEntity.from_query(query, Span(2, 6), entity_type='size') option = QueryEntity.from_query(query, Span(19, 29), entity_type='option') product = QueryEntity.from_query(query, Span(8, 12), entity_type='product', children=(size, option)) processed_query = ProcessedQuery(query, entities=[size, product, option]) markup_text = "a [{large|size} {latte|product} with {nonfat milk|option}|product] please" entity_text = "a {large|size} {latte|product} with {nonfat milk|option} please" group_text = "a [large latte with nonfat milk|product] please" assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_group=True) == entity_text assert markup.dump_query(processed_query, no_entity=True) == group_text assert markup.dump_query(processed_query, no_group=True, no_entity=True) == query_text
def test_dump_group_nested_2(query_factory): """Tests dumping a query with nested entity groups""" query_text = 'Can I get one curry sauce with my rice ball with house salad' query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query(query, Span(10, 12), entity_type='sys_number', role='quantity'), QueryEntity.from_query(query, Span(14, 24), entity_type='option'), QueryEntity.from_query(query, Span(34, 59), entity_type='dish') ] entities[1] = entities[1].with_children((entities[0], )) entities[2] = entities[2].with_children((entities[1], )) processed_query = ProcessedQuery(query, entities=entities) markup_text = ( 'Can I get [[{one|sys_number|quantity} {curry sauce|option}|option] ' 'with my {rice ball with house salad|dish}|dish]') entity_text = ('Can I get {one|sys_number|quantity} {curry sauce|option} ' 'with my {rice ball with house salad|dish}') role_text = ('Can I get {one|quantity} curry sauce ' 'with my rice ball with house salad') group_text = ('Can I get [[one curry sauce|option] ' 'with my rice ball with house salad|dish]') assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_group=True) == entity_text assert markup.dump_query(processed_query, no_group=True, no_entity=True) == role_text assert markup.dump_query(processed_query, no_entity=True, no_role=True) == group_text assert markup.dump_query(processed_query, no_group=True, no_entity=True, no_role=True) == query_text
def test_dump_role(query_factory): """Tests dumping a basic query with an entity with a role""" query_text = 'What stores are open between 3 and 5' query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query(query, Span(29, 29), entity_type='sys_time', role='open_hours'), QueryEntity.from_query(query, Span(35, 35), entity_type='sys_time', role='close_hours') ] processed_query = ProcessedQuery(query, entities=entities) markup_text = ('What stores are open between {3|sys_time|open_hours} and ' '{5|sys_time|close_hours}') entity_text = 'What stores are open between {3|sys_time} and {5|sys_time}' assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_role=True) == entity_text assert markup.dump_query(processed_query, no_role=True, no_entity=True) == query_text
def test_dump_role(query_factory): """Tests dumping a basic query with an entity with a role""" query_text = "What stores are open between 3 and 5" query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query(query, Span(29, 29), entity_type="sys_time", role="open_hours"), QueryEntity.from_query(query, Span(35, 35), entity_type="sys_time", role="close_hours"), ] processed_query = ProcessedQuery(query, entities=entities) markup_text = ("What stores are open between {3|sys_time|open_hours} and " "{5|sys_time|close_hours}") entity_text = "What stores are open between {3|sys_time} and {5|sys_time}" assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_role=True) == entity_text assert (markup.dump_query(processed_query, no_role=True, no_entity=True) == query_text)
def test_load_special_chars_3(query_factory): """Tests loading a query with special characters""" text = "is {s.o.b.|show} gonna be {{on at 8 p.m.|sys_time}|range}?" processed_query = markup.load_query(text, query_factory) entities = processed_query.entities expected_entity = QueryEntity.from_query(processed_query.query, Span(3, 8), entity_type="show") assert entities[0] == expected_entity assert entities[1].entity.type == "range" assert entities[1].span == Span(19, 30) assert "children" in entities[1].entity.value assert entities[1].entity.value["children"][0].entity.type == "sys_time"
def test_dump_nested(query_factory): """Tests dumping a query with a nested system entity""" query_text = "show me houses under 600,000 dollars" query = query_factory.create_query(query_text) nested = NestedEntity.from_query( query, Span(0, 6), parent_offset=21, entity_type="sys_number" ) raw_entity = Entity("600,000 dollars", "price", value={"children": [nested]}) entities = [QueryEntity.from_query(query, Span(21, 35), entity=raw_entity)] processed_query = ProcessedQuery(query, entities=entities) markup_text = "show me houses under {{600,000|sys_number} dollars|price}" assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_group=True) == markup_text assert markup.dump_query(processed_query, no_entity=True) == query_text
def test_load_special_chars_4(query_factory): """Tests loading a query with special characters""" text = 'is {s.o.b.|show} ,, gonna be on at {{8 p.m.|sys_time}|range}?' processed_query = markup.load_query(text, query_factory) entities = processed_query.entities expected_entity = QueryEntity.from_query(processed_query.query, Span(3, 8), entity_type='show') assert entities[0] == expected_entity assert entities[1].entity.type == 'range' assert entities[1].span == Span(28, 33) assert 'children' in entities[1].entity.value assert entities[1].entity.value['children'][0].entity.type == 'sys_time'
def test_dump_group_nested(query_factory): """Tests dumping a query with nested entity groups""" query_text = "Order one large Tesora with medium cream and medium sugar" query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query(query, Span(6, 8), entity_type="quantity"), QueryEntity.from_query(query, Span(10, 14), entity_type="size"), QueryEntity.from_query(query, Span(16, 21), entity_type="product"), QueryEntity.from_query(query, Span(28, 33), entity_type="size"), QueryEntity.from_query(query, Span(35, 39), entity_type="option"), QueryEntity.from_query(query, Span(45, 50), entity_type="size"), QueryEntity.from_query(query, Span(52, 56), entity_type="option"), ] entities[4] = entities[4].with_children((entities[3],)) entities[6] = entities[6].with_children((entities[5],)) entities[2] = entities[2].with_children( (entities[0], entities[1], entities[4], entities[6]) ) processed_query = ProcessedQuery(query, entities=entities) markup_text = ( "Order [{one|quantity} {large|size} {Tesora|product} with [{medium|size} " "{cream|option}|option] and [{medium|size} {sugar|option}|option]|product]" ) entity_text = ( "Order {one|quantity} {large|size} {Tesora|product} with {medium|size} " "{cream|option} and {medium|size} {sugar|option}" ) group_text = ( "Order [one large Tesora with [medium " "cream|option] and [medium sugar|option]|product]" ) assert markup.dump_query(processed_query) == markup_text assert markup.dump_query(processed_query, no_group=True) == entity_text assert markup.dump_query(processed_query, no_entity=True) == group_text assert ( markup.dump_query(processed_query, no_group=True, no_entity=True) == query_text )
def test_bootstrap_query_with_entities(query_factory): query_text = 'Can I get one curry sauce with my rice ball with house salad' query = query_factory.create_query(query_text) entities = [ QueryEntity.from_query(query, Span(10, 12), entity_type='sys_number', role='quantity'), QueryEntity.from_query(query, Span(14, 24), entity_type='option'), QueryEntity.from_query(query, Span(34, 59), entity_type='dish') ] entities[1] = entities[1].with_children((entities[0], )) entities[2] = entities[2].with_children((entities[1], )) confidence = { 'domains': { 'food': 0.95, 'music': 0.05 }, 'intents': { 'get_comestibles': 0.99, 'reorder': 0.01 }, 'entities': [{ 'sys_number': 0.9 }, { 'option': 0.99 }, { 'dish': 0.65 }], 'roles': [{ 'quantity': 0.8, 'quality': 0.2 }, None, None] } processed_query = ProcessedQuery(query, domain='food', intent='get_comestibles', entities=entities, confidence=confidence) bootstrap_data = markup.bootstrap_query_row(processed_query, show_confidence=True) expected_data = { 'query': ('Can I get [[{one|sys_number|quantity} {curry sauce|option}|option] ' 'with my {rice ball with house salad|dish}|dish]'), 'domain': 'food', 'domain_conf': 0.95, 'intent': 'get_comestibles', 'intent_conf': 0.99, 'entity_conf': 0.65, 'role_conf': 0.8 } assert bootstrap_data == expected_data