Update conversation_logic.py
Browse files221 unmodified lines222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253326 unmodified lines580581582583584585184 unmodified lines770771772773774775101 unmodified lines87787887988088188288388483 unmodified lines96896997097197297367 unmodified lines1041104210431044221 unmodified linesreturn "median"if "range" in q:return "range"ratio_like = any(phrase in qfor phrase in ["ratio","proportion","proportional","part to whole","out of","for every","in the same ratio","in proportion","directly proportional","inversely proportional",])ratio_like = ratio_like or bool(re.search(r"\b[a-z]\s*/\s*[a-z]\b", q))ratio_like = ratio_like or bool(re.search(r"\b\d+\s*/\s*\d+\b", q))ratio_like = ratio_like or bool(re.search(r"\b[a-z]\s*:\s*[a-z]\b", q))ratio_like = ratio_like or bool(re.search(r"\b\d+\s*:\s*\d+\b", q))ratio_like = ratio_like or bool(re.search(r"\b[a-z]+\s+to\s+[a-z]+\b", q))ratio_like = ratio_like or bool(re.search(r"\b\d+\s+to\s+\d+\b", q))if ratio_like:return "ratio"if topic == "data" and any(k in q for k in ["dataset", "table", "chart", "graph"]):return "statistics"return topic326 unmodified linesreturn bool(result is not None and _get_result_steps(result))def _answer_path_from_steps(steps: List[str], verbosity: float) -> str:safe_steps = _safe_steps(steps)if not safe_steps:184 unmodified lineshint_stage=hint_stage,verbosity=verbosity,)# Merge solver result into base result only if it agrees with the classified topic.if solver_result is not None:101 unmodified linesif fallback_pack and fallback_pack.get("topic") == "statistics":qlow = (solver_input or "").lower()if any(k in qlow for k in ["variability", "spread", "standard deviation"]):if resolved_help_mode == "answer":fallback_reply_core = ("- Notice this is asking about variability, which means spread, not the mean.\n""- Compare how far the smallest and largest values sit from the middle value in each dataset.\n"83 unmodified lineshint_stage=hint_stage,topic=result.topic,)elif fallback_reply_core:reply_core = fallback_reply_coreresult.meta["response_source"] = "fallback"67 unmodified linesresult.meta["session_state"] = stateresult.meta["used_retrieval"] = Falseresult.meta["used_generator"] = Falsereturn result221 unmodified lines222223224225226227228229230231232233326 unmodified lines560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674184 unmodified lines859860861862863864865866867868869870871872101 unmodified lines97497597697797897998098198298398498598698798883 unmodified lines1072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109767 unmodified lines1165116611671168221 unmodified linesreturn "median"if "range" in q:return "range"if ("ratio" in qor re.search(r"\b[a-z]\s*/\s*[a-z]\b", q)or re.search(r"\b\d+\s*/\s*\d+\b", q)):return "ratio"if topic == "data" and any(k in q for k in ["dataset", "table", "chart", "graph"]):return "statistics"return topic326 unmodified linesreturn bool(result is not None and _get_result_steps(result))def _parse_numeric_option_set(option: str) -> Optional[List[float]]:raw = _clean_text(option)if not raw:return Nonetry:parts = [float(x.strip()) for x in raw.split(",") if x.strip()]except Exception:return Nonereturn parts if len(parts) >= 2 else Nonedef _question_specific_ratio_reply(question_text: str) -> str:q = _clean_text(question_text)low = q.lower()if re.search(r"\b[a-z]\s*/\s*[a-z]\s*=\s*\d+\s*/\s*\d+", low) and re.search(r"what is\s*\(", low):return ("- Treat the ratio as matching parts: if a/b = 3/4, you can set a = 3k and b = 4k.\n""- Substitute those part-values into the expression the question asks for instead of solving for specific numbers.\n""- After substitution, simplify the expression by cancelling the common factor k.")return ("- Rewrite the ratio using matching parts, such as 3k and 4k, before touching the target expression.\n""- Build the requested expression from those parts, then simplify only at the end.")def _question_specific_variability_reply(options_text: Optional[List[str]]) -> str:parsed = [_parse_numeric_option_set(opt) for opt in (options_text or [])]valid = [p for p in parsed if p]if valid and all(len(p) == 3 for p in valid):return ("- This is asking about variability, so compare spread rather than average.\n""- For each three-number set, use the middle value as the centre and compare how far the outer numbers sit from it.\n""- The dataset whose values stretch furthest away from the centre is the one with the greatest variability.")return ("- This is asking about variability, so focus on spread rather than the average.\n""- Compare how tightly clustered or widely spaced the values are in each answer choice.\n""- The choice with the widest spread is the strongest candidate.")def _question_specific_percent_reply(question_text: str) -> str:low = _clean_text(question_text).lower()if "increased by" in low and "decreased by" in low:return ("- For back-to-back percent changes, turn the changes into multipliers instead of trying to combine the percentages directly.\n""- Apply the increase multiplier first, then the decrease multiplier to that new amount.\n""- Compare the final multiplier with 1 to decide whether the result is above or below the original.")return ("- Identify the base quantity first, because percent relationships only make sense relative to a base.\n""- Translate the wording into either a multiplier or a percent equation before simplifying.")def _build_question_specific_reply(*,question_text: str,options_text: Optional[List[str]],classified_topic: str,help_mode: str,input_type: str,user_text: str,) -> str:q = _clean_text(question_text)low = q.lower()topic = (classified_topic or "general").lower()user_low = _clean_text(user_text).lower()if not q:return ""if any(k in low for k in ["variability", "spread", "standard deviation"]):return _question_specific_variability_reply(options_text)if topic in {"ratio", "algebra"}:if re.search(r"\b[a-z]\s*/\s*[a-z]\s*=\s*\d+\s*/\s*\d+", low):return _question_specific_ratio_reply(q)if "what is" in low and "(" in low and ")" in low and any(sym in low for sym in ["a+b", "x+y", "a-b", "x-y"]):return ("- Start by rewriting one variable in terms of the other using the relationship you were given.\n""- Then substitute into the exact expression in parentheses, rather than trying to solve for actual numbers.\n""- Simplify only after the whole target expression has been rewritten in one variable or in matching parts.")if topic == "percent" or "%" in low or "percent" in low:return _question_specific_percent_reply(q)if topic == "statistics" and any(k in low for k in ["dataset", "table", "chart", "graph"]):return ("- Read the question stem first, then decide which statistic matters before comparing answer choices.\n""- Use the structure of the choices to compare them efficiently instead of computing unnecessary extra values.")if topic == "algebra":return ("- Turn the wording into one clean relationship first.\n""- Then focus on the exact expression the question asks for, rather than solving more than you need to.")if input_type in {"hint", "next_hint", "confusion"} or any(phrase in user_low for phrase in ["how do i solve", "what do i do first", "what should i do first", "how should i start"]):return "- Start by identifying the main relationship in the question, then rewrite the target expression using that relationship before simplifying."return ""def _answer_path_from_steps(steps: List[str], verbosity: float) -> str:safe_steps = _safe_steps(steps)if not safe_steps:184 unmodified lineshint_stage=hint_stage,verbosity=verbosity,)question_specific_reply_core = _build_question_specific_reply(question_text=solver_input,options_text=options_text,classified_topic=question_topic,help_mode=resolved_help_mode,input_type=input_type,user_text=user_text,)# Merge solver result into base result only if it agrees with the classified topic.if solver_result is not None:101 unmodified linesif fallback_pack and fallback_pack.get("topic") == "statistics":qlow = (solver_input or "").lower()wants_topic = input_type == "topic_query"if any(k in qlow for k in ["variability", "spread", "standard deviation"]):if wants_topic:fallback_reply_core = ("- This is a statistics / data insight question about variability (spread).\n""- Focus on how spread out each dataset is rather than the average.\n""- Compare how far the outer values sit from the middle value in each set.")elif resolved_help_mode == "answer":fallback_reply_core = ("- Notice this is asking about variability, which means spread, not the mean.\n""- Compare how far the smallest and largest values sit from the middle value in each dataset.\n"83 unmodified lineshint_stage=hint_stage,topic=result.topic,)elif question_specific_reply_core and (_is_help_first_mode(resolved_help_mode)or input_type in {"other", "confusion", "hint", "next_hint"}or not direct_solve_requestor not fallback_reply_core):reply_core = question_specific_reply_coreresult.meta["response_source"] = "question_specific"result.meta["question_support_used"] = bool(fallback_pack)result.meta["question_support_source"] = fallback_pack.get("support_source") if fallback_pack else Noneresult.meta["question_support_topic"] = fallback_pack.get("topic") if fallback_pack else Nonereply = format_reply(reply_core,tone=tone,verbosity=verbosity,transparency=transparency,help_mode=resolved_help_mode,hint_stage=hint_stage,topic=result.topic,)elif fallback_reply_core:reply_core = fallback_reply_coreresult.meta["response_source"] = "fallback"67 unmodified linesresult.meta["session_state"] = stateresult.meta["used_retrieval"] = Falseresult.meta["used_generator"] = Falsereturn result
conversation_logic.py+151-27