mirror of
https://github.com/TauricResearch/TradingAgents.git
synced 2026-11-27 19:46:20 +02:00
fix(rating): read a rating another party gave as evidence, not as the decision (#1466)
- a decision that opens with its rating line, plain or as the list item the prompt asks for, is read from that line
- only words naming the decision itself ("final", "our", "overall") may qualify its rating line; "Consensus rating: Buy" or "Trader's rating: Buy" is another party's
- otherwise the decision's own rating lines, or failing those all rating labels, must agree; ratings that differ need review
This commit is contained in:
@@ -251,3 +251,53 @@ def test_a_rating_the_text_quotes_does_not_replace_the_decision(quoted):
|
||||
def test_a_quoted_rating_in_a_list_table_or_quote_is_not_the_decision(quoted):
|
||||
text = f"Our rating: Hold\n\nWhat others say:\n{quoted}\n\nWe wait for margins."
|
||||
assert extract_rating(text) == "Hold"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("text, expected", [
|
||||
("Consensus rating: Buy\nRating: Sell", "Sell"),
|
||||
("Analyst rating: Overweight\n**Rating**: Underweight", "Underweight"),
|
||||
("Previous rating: Buy\n## Final Rating - Hold", "Hold"),
|
||||
("Street rating: Sell\nOur rating: Buy", "Buy"),
|
||||
("Credit rating: Sell\nRating: Buy", "Buy"),
|
||||
("Sell-side consensus rating: Buy\n**Rating**: Hold", "Hold"),
|
||||
# the decision opens with its rating, as the prompt asks
|
||||
("- **Rating**: Sell\n\nConsensus rating: Buy", "Sell"),
|
||||
("- **Rating**: Sell\n- **Investment Thesis**: consensus rating: Buy, we disagree.", "Sell"),
|
||||
("1. **Rating**: Underweight\n2. **Executive Summary**: Street rating: Buy.", "Underweight"),
|
||||
("\u2022 **Rating**: Sell\nConsensus rating: Buy", "Sell"),
|
||||
("## Decision\n\n- **Rating**: Hold\n- Rating: Buy (Goldman Sachs)", "Hold"),
|
||||
("Recommended rating: Buy\n\nConsensus rating: Sell", "Buy"),
|
||||
("Updated rating: Sell\nPrevious rating: Buy", "Sell"),
|
||||
("Our final rating: Sell\nConsensus rating: Buy", "Sell"),
|
||||
("**Overall Final Rating**: Sell\nConsensus rating: Buy", "Sell"),
|
||||
# ratings quoted as list items before the decision's own line
|
||||
("Broker views:\n- Rating: Buy (Morgan Stanley)\n- Rating: Hold (Goldman)\n\n**Rating**: Sell", "Sell"),
|
||||
("Previous decision recap:\n- **Rating**: Buy (2026-09-01)\n\n**Rating**: Sell", "Sell"),
|
||||
("Lessons:\n- Rating: Buy was wrong last time\n\n**Rating**: Hold", "Hold"),
|
||||
])
|
||||
def test_a_rating_someone_else_gave_does_not_become_the_decision(text, expected):
|
||||
"""The decision's own rating line is read as the call; another party's
|
||||
rating, even on a line of its own, is evidence, not the call (#1466)."""
|
||||
assert extract_rating(text) == expected
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("text", [
|
||||
"Trader's proposed rating: Buy\n\nPortfolio Manager's rating: Hold",
|
||||
"Research Manager rating: Buy\nPortfolio rating: Underweight",
|
||||
"Trader's rating: Buy\nRisk team rating: Sell\nPortfolio Manager's Final Rating: Hold",
|
||||
"Risk views:\n- Aggressive analyst rating: Buy\n- Conservative rating: Sell\n\nPM: Rating: Hold",
|
||||
"PM Rating: Sell\n- Rating: Buy (JPMorgan)",
|
||||
"Position rating: Underweight\n* Rating: Overweight per Citi",
|
||||
"Street views:\n- Goldman rating: Buy\n\nWeighing all of this, the Portfolio Manager's rating: Underweight.",
|
||||
"Analyst views:\n- Rating: Buy (Goldman)\n\nAfter the debate our final call is Sell (Rating: Sell).",
|
||||
"Lessons:\n- Rating: Buy was wrong last time\n\n- **Rating**: Hold",
|
||||
"Broker views:\n- Rating: Buy (MS)\n\n- **Rating**: Sell",
|
||||
"Investment rating: Underweight\nStreet rating: Buy",
|
||||
"## Street view\n1. Rating: Buy, target $250 (JPM)\n\n## Our call\n**Rating**: Underweight",
|
||||
])
|
||||
def test_ratings_that_disagree_with_no_line_of_the_decisions_own_need_review(text):
|
||||
"""When the text never states its own rating in the decision's shape and the
|
||||
ratings it names disagree, nobody can tell which is the call (#1170)."""
|
||||
assert extract_rating(text) is None
|
||||
|
||||
@@ -37,12 +37,15 @@ _RATING_SET = {r.lower() for r in RATINGS_5_TIER}
|
||||
_RATING_LABEL_RE = re.compile(r"(?<![a-z])rating\b[^:\-\u2010-\u2015]*[:\-\u2010-\u2015][\s*]*(\w+)",
|
||||
re.IGNORECASE)
|
||||
|
||||
# The same label opening its own line ("**Rating**: X", "## Final Rating - X",
|
||||
# "Our rating: X"): the shape the Portfolio Manager is asked to write its
|
||||
# decision in. Only emphasis and heading marks may precede it, so a list item,
|
||||
# table row or blockquote quoting someone else's rating is not one.
|
||||
# The decision's own rating line, in the shape the Portfolio Manager is asked to
|
||||
# open with ("- **Rating**: X", "**Final Rating**: X", "## Our rating - X"): an
|
||||
# optional list marker, emphasis and heading marks, and only words naming the
|
||||
# decision itself before "rating". "Consensus rating: Buy" or "Trader's rating:
|
||||
# Buy" is someone else's rating.
|
||||
_OWN_QUALIFIER = r"(?:(?:final|our|overall|my|recommended|updated|revised|new|current)\s+)*"
|
||||
_RATING_LINE_RE = re.compile(
|
||||
r"[\s*_#]*(?:\w+\s+)?rating[^\w:\-\u2010-\u2015]*[:\-\u2010-\u2015][\s*]*(\w+)",
|
||||
r"\s*(?P<item>(?:[-+*\u2022]|\d+[.)])\s+)?[\s*_#]*" + _OWN_QUALIFIER
|
||||
+ r"rating[^\w:\-\u2010-\u2015]*[:\-\u2010-\u2015][\s*]*(?P<value>\w+)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
@@ -54,32 +57,40 @@ def extract_rating(text: str) -> str | None:
|
||||
|
||||
Reads an explicit "Rating: X" label (tolerant of markdown bold) in the
|
||||
NFKC-normalized text, so fullwidth punctuation like ``Rating:Overweight``
|
||||
matches as ASCII does: the first one opening its own line, else the last
|
||||
one anywhere.
|
||||
matches as ASCII does: the decision's opening rating line, else its own
|
||||
rating lines or, failing those, every label, when they agree.
|
||||
"""
|
||||
if not text:
|
||||
return None
|
||||
norm = unicodedata.normalize("NFKC", text)
|
||||
|
||||
# A decision is asked to open with its rating on its own line, so the first
|
||||
# such line is the call; later ones may quote someone else's ("Consensus
|
||||
# rating: Buy"). Without one, the last label anywhere wins: prose states its
|
||||
# rating after discussing the alternatives. Lines presenting the scale itself
|
||||
# are a legend the model echoed, not a call.
|
||||
on_own_line = anywhere = None
|
||||
for line in norm.splitlines():
|
||||
if _RATING_SCALE_RE.search(line):
|
||||
continue
|
||||
# The decision is asked to open with its rating, so a first line (after any
|
||||
# headings) in that shape is the call. A list item there may also be a quote
|
||||
# heading a list of other parties' ratings, so it counts with the decision's
|
||||
# other rating lines, which leave out list items; failing those, every label
|
||||
# counts. Either way they must agree: ratings that differ, with nothing
|
||||
# marking which one is the call, are no call (#1170). Lines presenting the
|
||||
# scale itself are a legend the model echoed, not a call.
|
||||
lines = [line for line in norm.splitlines() if line.strip() and not _RATING_SCALE_RE.search(line)]
|
||||
first = next((line for line in lines if not line.lstrip().startswith("#")
|
||||
or _RATING_LINE_RE.match(line)), "")
|
||||
m = _RATING_LINE_RE.match(first)
|
||||
opening = m if m and m.group("value").lower() in _RATING_SET else None
|
||||
if opening and not opening.group("item"):
|
||||
return opening.group("value").capitalize()
|
||||
|
||||
own = [opening.group("value").capitalize()] if opening else []
|
||||
labels = []
|
||||
for line in lines:
|
||||
m = _RATING_LINE_RE.match(line)
|
||||
if on_own_line is None and m and m.group(1).lower() in _RATING_SET:
|
||||
on_own_line = m.group(1).capitalize()
|
||||
m = _RATING_LABEL_RE.search(line)
|
||||
if m and m.group(1).lower() in _RATING_SET:
|
||||
anywhere = m.group(1).capitalize()
|
||||
if m and not m.group("item") and m.group("value").lower() in _RATING_SET:
|
||||
own.append(m.group("value").capitalize())
|
||||
labels += [v.capitalize() for v in _RATING_LABEL_RE.findall(line) if v.lower() in _RATING_SET]
|
||||
# Without a label there is no call to read: a rating word in the prose may be
|
||||
# one the text argues against ("not a Sell"), and reading it reports a
|
||||
# direction nobody decided.
|
||||
return on_own_line or anywhere
|
||||
found = own or labels
|
||||
return found[0] if found and len(set(found)) == 1 else None
|
||||
|
||||
|
||||
def parse_rating(text: str, default: str = RATING_REVIEW) -> str:
|
||||
|
||||
Reference in New Issue
Block a user