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_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_bootstrap_query_no_entity(query_factory): """"Tests bootstrap output for a query without entities""" query_text = "cancel the timer" query = query_factory.create_query(query_text) confidence = { "domains": { "times_and_dates": 0.95, "espionage": 0.05 }, "intents": { "stop_timer": 0.9, "start_timer": 0.07, "cut_blue_wire": 0.03 }, "entities": [], "roles": [], } processed_query = ProcessedQuery( query, domain="times_and_dates", intent="stop_timer", entities=[], confidence=confidence, ) bootstrap_data = markup.bootstrap_query_row(processed_query, show_confidence=True) expected_data = { "query": "cancel the timer", "domain": "times_and_dates", "domain_conf": 0.95, "intent": "stop_timer", "intent_conf": 0.9, "entity_conf": 1.0, "role_conf": 1.0, } assert bootstrap_data == expected_data
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_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_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_bootstrap_query_no_entity(query_factory): """"Tests bootstrap output for a query without entities""" query_text = 'cancel the timer' query = query_factory.create_query(query_text) confidence = { 'domains': { 'times_and_dates': 0.95, 'espionage': 0.05 }, 'intents': { 'stop_timer': 0.9, 'start_timer': 0.07, 'cut_blue_wire': 0.03 }, 'entities': [], 'roles': [] } processed_query = ProcessedQuery(query, domain='times_and_dates', intent='stop_timer', entities=[], confidence=confidence) bootstrap_data = markup.bootstrap_query_row(processed_query, show_confidence=True) expected_data = { 'query': 'cancel the timer', 'domain': 'times_and_dates', 'domain_conf': 0.95, 'intent': 'stop_timer', 'intent_conf': 0.9, 'entity_conf': 1.0, 'role_conf': 1.0 } assert bootstrap_data == expected_data
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_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