Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@
# "Filter Usage" is a chart) or an open "anything else" ("I'll replace Order Breakdown in the same
# position"). "With it" names the chart, so "replace the date filter with it" still places one.
_CHART_NOUNS = r"charts?|visualizations?|insights?"
# "Confirm and I'll propose placing it at the end" places the chart as much as "place it" does.
_PLACING = rf"{_PLACEMENT_VERBS}|replacing|placing|putting|swapping|adding|inserting|moving"
_PLACED_CHART_RE = re.compile(
rf"\b(?:{_PLACEMENT_VERBS})\s+(?:\S+\s+)?(?:it|them)\b"
rf"|\b(?:{_PLACEMENT_VERBS})\s+(?:\S+\s+){{0,5}}?(?:{_CHART_NOUNS})\b"
rf"\b(?:{_PLACING})\s+(?:\S+\s+)?(?:it|them)\b"
rf"|\b(?:{_PLACING})\s+(?:\S+\s+){{0,5}}?(?:{_CHART_NOUNS})\b"
rf"|\b(?:{_PLACEMENT_VERBS})\b[^,;]*?\bwith\s+(?:it|them|(?:this|that|the)\s+(?:new\s+)?(?:{_CHART_NOUNS}))\b",
re.IGNORECASE,
)
Expand All @@ -107,12 +109,75 @@
rf"(?:{_SHOULD_I}|{_WANT_ME_TO}|{_IF_YOU_WANT_ME_TO})(?:{_PLACEMENT_VERBS})\b",
re.IGNORECASE,
)
# A word to say before a placement ('Say "place it" and I'll add it', "Type “yes” and I will put it
# below"): a go-ahead or placement word, at most a pronoun or "ahead" after it, between a matching pair of
# straight or curly quotes. Read on the raw reply and turned into _SAY_TOKEN before the quoted literals are
# masked, so both quote styles read alike; a literal that asks for more ("yes, but smaller", "Section A",
# "ok it's fine") stays a literal and is masked.
_SAY_TOKEN = "saygoaheadword"
_SAY_WORDS = rf"yes|ok(?:ay)?|{_PLACEMENT_VERBS}|{_APPROVAL_VERBS}"
_SAY_TAIL = r"(?:\s+(?:it|them|this|that|ahead|now))?\s*[.!]?\s*"
_SAY_LITERAL = "|".join(
rf"{opening}\s*({_SAY_WORDS}){_SAY_TAIL}{closing}" for opening, closing in ('""', "“”", "‘’", "''")
)
_SAY_WORD_RE = re.compile(rf"\b(say|type)\s+(?:{_SAY_LITERAL})(?!\w)", re.IGNORECASE)
# Each word to say that the reply ties to what it will then do ('say "add" and I'll add it as a new row',
# also ', or "add" and I'll …') is one option. Two options are a choice unless every word only goes ahead:
# 'Say "yes" and I'll replace Order breakdown with it, or say "add" and I'll add it as a new row' asks
# which, 'Say "yes" and I'll place it. Or just say "place it" and I'll add it there' does not. A word with
# no "and I'll" after it ('To move it later, just type "move"') offers nothing now.
_SAY_OFFER_RE = re.compile(
rf"(?:\b(?:say|type)\s+|\bor\s+)(?:{_SAY_LITERAL})(?!\w),?\s+and\s+i(?:['’]ll|\s+will)\b", re.IGNORECASE
)
_SAY_GO_AHEAD_WORDS = frozenset({"yes", "ok", "okay", "place", "proceed", "apply", "go ahead"})
# A reply that says before a "where" that it already placed the chart ("I replaced Order Breakdown with
# it") asks for no place to go on with. The claim must place the chart itself: "I added a comparison line
# to it" changed the chart, and "I've added it to the preview below" shows it.
_CLAIMS_PLACED_RE = re.compile(
r"\bi(?:['’]ve|\s+have)?\s+(?:already\s+)?(?:placed|added|put|moved|inserted)\s+"
rf"(?:it|them|(?:the|this|that)\s+(?:\S+\s+){{0,2}}?(?:{_CHART_NOUNS}))\b"
r"(?!\s+(?:in|into|to|on)\s+(?:the|a)\s+preview)"
r"|\bi(?:['’]ve|\s+have)?\s+(?:already\s+)?(?:replaced|swapped)\s+[^.;]{1,40}?\s+with\s+(?:it|them)\b",
re.IGNORECASE,
)
# An ask for a place on a condition about the spot ("if that spot is not right", "if you'd rather have it
# elsewhere") asks nothing yet; "if it helps" or "if you like" is a courtesy.
_IF_ELSEWHERE_RE = re.compile(
r"\bif\s+(?:that|this|the)\s+(?:spot|place|position|location)\b|\bif\s+you(?:['’]d|\s+would)\s+rather\b"
r"|\bif\s+you\s+prefer\b",
re.IGNORECASE,
)
# These name no placement of their own ("Once you confirm the new title, I'll rename it"), so they
# count only in a sentence that places a chart (_PLACED_CHART_RE).
_GO_AHEAD_RE = re.compile(
r"\b(?:once|when|after|if)\s+you\s+confirm\b"
r"|\bconfirm,?\s+and\s+i(?:['’]ll|\s+will)\b"
rf"|\b(?:tell|ask)\s+me\s+to\s+(?:{_PLACEMENT_VERBS}|{_APPROVAL_VERBS})\b",
rf"|\b(?:tell|ask)\s+me\s+to\s+(?:{_PLACEMENT_VERBS}|{_APPROVAL_VERBS})\b"
rf"|\b(?:say|type)\s+{_SAY_TOKEN}\s+and\s+i(?:['’]ll|\s+will)\b",
re.IGNORECASE,
)
# Beside a shown chart, placing it named as the next step ("I've previewed it for placement at the end;
# applying it and switching the dashboard to last quarter will be the next step") is the same stop, told
# rather than asked. The placing verb and the chart come right next to the step: "placing it … will be the
# next step", "the next step is to place it", "next, I'll add it". "Next, I'll explain how to add it" and
# "adding a filter will be the next step" place nothing.
_PLACING_ING = r"replacing|placing|putting|swapping|adding|inserting|moving|applying"
_NEXT_STEP_RE = re.compile(
rf"\b(?:{_PLACING_ING})\s+(?:it|them)\b[^.;]*?\b(?:will\s+be|is)\s+(?:the|my)\s+next\s+step\b"
rf"|\bthe\s+next\s+step\s+(?:is|will\s+be)\s+(?:to\s+)?(?:{_PLACEMENT_VERBS}|apply|{_PLACING_ING})\s+(?:it|them)\b"
rf"|\bnext,?\s+i(?:['’]ll|\s+will)\s+(?:{_PLACEMENT_VERBS}|apply)\s+(?:it|them)\b",
re.IGNORECASE,
)
# A step left to the user ("once you pick a section", "for you in the UI", "which you can do from the menu",
# "place it yourself") or one the agent still lacks something for ("but I still need your section") is no
# go-ahead stop. A refusal or a claim around it is no guard: a "yes" then gets no patch, and the run fails
# on that.
_NEXT_STEP_HANDED_BACK_RE = re.compile(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit — _NEXT_STEP_HANDED_BACK_RE searches the whole sentence, so a "you can put/add …" about something else, or a refusal of another spot in the same or previous sentence, cancels the go-ahead. The test file says a refusal about another spot should not block.

Example: "Here is the chart. Next, I'll place it at the end; you can put it elsewhere later." or "I can't place it in the Sales tab, which is locked. Next, I'll place it at the end instead." → no_action, although the agent named its own placement as the next step.

Suggested fix: match the guard only against the clause holding the placing verb, and add a test for a same-sentence / adjacent refusal of a different spot.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2b6b72c. The refusal guard is gone (see the first thread), and "you can …" now counts only as "you can do …" or "you can place/add/apply/put it yourself/from/via …", so "you can put it elsewhere later" does not cancel the go-ahead. Both examples are tests (confirmation).

r"\b(?:once|when|after|if)\s+you\s+(?:pick|choose|select|specify|tell|decide|provide|name)\b"
r"|\bfor\s+you\s+(?:in|to\s+do|from)\b|\bfor\s+your\s+(?:\w+\s+)?admin\b"
r"|\byou\s+can\s+do\b|\byou\s+can\s+(?:place|add|apply|put)\s+(?:it|them)\s+(?:yourself|from|via)\b"
r"|\byourself\b|\bhow\s+to\b|\byou(?:['’]ll|\s+will)\s+need\b"
r"|\bi\s+(?:still\s+)?need\s+(?:the|a|an|your|you\s+to|to\s+know|more)\b",
re.IGNORECASE,
)
# Beside a chart the same reply shows, an offer to place it ("I can replace Order breakdown with it",
Expand Down Expand Up @@ -147,6 +212,17 @@
r"|how\s+(?:wide|tall|big|large|many)\b)",
re.IGNORECASE,
)
# A reply that places nothing yet asks where the chart should go ("tell me where to place it", "let me
# know where you want it", "which section it should go in"). The place must be for the chart:
# "let me know where else I can help" or "which other charts you need" ask for nothing to place.
_THE_CHART = rf"it|them|(?:the|this|that)\s+(?:\S+\s+){{0,3}}?(?:{_CHART_NOUNS})"
_PLACE_ASK_IN_WORDS_RE = re.compile(
rf"\b(?:let\s+me\s+know|tell\s+me|please\s+(?:{_REQUEST_VERBS}))\s+"
r"(?:where(?!\s+else)|which\s+(?:section|tab|row|column|position|spot|place))\b[^.?!]*?"
rf"\b(?:(?:{_PLACEMENT_VERBS})\s+(?:\S+\s+)?(?:{_THE_CHART})"
rf"|you(?:['’]d|\s+would)?\s+(?:want|like)\s+(?:{_THE_CHART})|(?:{_THE_CHART}|they)\s+should\s+go)\b",
re.IGNORECASE,
)
_EDIT_REQUEST_RE = re.compile(
rf"\bplease\s+(?:{_REQUEST_VERBS})\b|\breply\s+with\b|^\s*(?:select|choose|pick)\s+(?:one|a|an|the)\b",
re.IGNORECASE,
Expand Down Expand Up @@ -283,34 +359,61 @@ def classify_edit_reply(chat_result: ChatResult, rendered_text: str, *, shown_ea
I'll place it", "ask me to place it when you're ready") is a confirmation, so it is told yes. So is
an offer to place it ("I can replace Order breakdown with it") once a chart has been shown: in this
reply, or anywhere in the conversation so far (``shown_earlier``), as when the editor asks again in a
turn after its preview.
turn after its preview, and so is placing that chart named as the next step ("applying it will be
the next step").
Either kind of confirmation turns into a question when the reply also asks for something a yes
does not supply: another question ("Which size should it have? Once you confirm, I'll place it."),
a choice in the placement itself ("Should I put it before or after Orders over time?", "in Sales or
Finance"), a numbered list with two placement items, something to confirm other than going ahead
("once you confirm which section"), a request for a place ("Please specify the size") or, from the
ask on, any other request.
ask on, any other request. A reply that places nothing but asks for a place in words ("tell me
where to place it") is a question too.
"""
if has_structured_question(chat_result):
return "question"
text = (rendered_text or "").strip()
kind = classify_reply(chat_result, text)
if chat_result.alert_proposals:
return kind
plain = _EMPHASIS_RE.sub("", _FORMULA_SPAN_RE.sub(" ", text))
said = _SAY_WORD_RE.sub(rf"\1 {_SAY_TOKEN}", text)
plain = _EMPHASIS_RE.sub("", _FORMULA_SPAN_RE.sub(" ", said))
sentences = [s for s in _SENTENCE_END_RE.split(plain) if s.strip()]
# ADHOC_VIZ_ID stands for a chart the reply did not show, so it makes no offer.
created = chat_result.created_visualizations
shown = [viz for viz in (created.objects if created else []) if viz.id != ADHOC_VIZ_ID]
offers = bool(shown) or shown_earlier
placing = [
bool(_PLACEMENT_ASK_RE.search(s) or _GO_AHEAD_RE.search(s) or (offers and _PLACEMENT_OFFER_RE.search(s)))
and bool(_PLACED_CHART_RE.search(s) or (offers and _PLACED_ANYTHING_RE.search(s)))
(
(
bool(
_PLACEMENT_ASK_RE.search(s) or _GO_AHEAD_RE.search(s) or (offers and _PLACEMENT_OFFER_RE.search(s))
)
and bool(_PLACED_CHART_RE.search(s) or (offers and _PLACED_ANYTHING_RE.search(s)))
)
or (
offers
and bool(_NEXT_STEP_RE.search(s))
and not _NEXT_STEP_HANDED_BACK_RE.search(s)
and not _REQUEST_RE.search(s)
)
)
and not _WH_ASK_RE.search(s)
for s in sentences
]
if not any(placing):
return "question" if kind == "confirmation" and asks_in_text(chat_result, text) else kind
if kind == "confirmation":
return "question" if asks_in_text(chat_result, text) else kind
# "Review it, then tell me where to place it" asks for a place without a question mark. A reply that
# said before the ask that the chart is already placed asks nothing to go on with ("I replaced Order
# Breakdown with it. Let me know where you want it moved"), nor does an ask on a condition about the spot.
if kind == "no_action" and any(
_PLACE_ASK_IN_WORDS_RE.search(s)
and not _IF_ELSEWHERE_RE.search(s)
and not any(_CLAIMS_PLACED_RE.search(t) for t in sentences[:i])
for i, s in enumerate(sentences)
):
return "question"
return kind
# A shown chart's title is a name, so an "or" inside one ("Orders or Returns") is no choice. Only a
# title of three words or more with an "or" in it is masked, and only as whole words: a title like
# "a" or "or" would otherwise eat into the sentence around it.
Expand All @@ -323,6 +426,11 @@ def classify_edit_reply(chat_result: ChatResult, rendered_text: str, *, shown_ea
first = placing.index(True)
# The second option can come as a sentence of its own after the ask: "Should I replace it? Alternatively, ...".
masked = [title_re.sub(" ", s) if title_re else s for s in sentences]
offered = {
" ".join(next(word for word in m.groups() if word).lower().split()) for m in _SAY_OFFER_RE.finditer(text)
}
if len(offered) >= 2 and not offered <= _SAY_GO_AHEAD_WORDS:
return "question"
if any(
_PLACEMENT_CHOICE_RE.search(s) or (is_placing and _DESTINATION_CHOICE_RE.search(s))
for i, (s, is_placing) in enumerate(zip(masked, placing, strict=True))
Expand Down
Loading
Loading