Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_load_dump_groups(query_factory):
"""Tests that load_query and dump_query are reversible"""
text = (
"Order [{one|quantity} {large|size} {Tesora|product} with "
"[{medium|size} {cream|option}|option]|product] from "
"[{Philz|store} in {Downtown Sunnyvale|location}|store]"
)
processed_query = markup.load_query(text, query_factory)
markup_text = markup.dump_query(processed_query)
assert text == markup_text
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_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
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
)
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
)
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
)
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_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
query_tree = {}
for query in queries:
if domain and query.domain != domain:
continue
if intent and query.intent != intent:
continue
if query.domain not in query_tree:
query_tree[query.domain] = {}
if query.intent not in query_tree[query.domain]:
query_tree[query.domain][query.intent] = []
if raw:
query_tree[query.domain][query.intent].append(markup.dump_query(query))
else:
query_tree[query.domain][query.intent].append(query)
return query_tree