| 1 | """The decision rule for Patent Secret Signals, written out as arithmetic. |
| 2 | |
| 3 | The rule itself is prose in ``skills/last30days/references/patent-signals.md`` |
| 4 | and is applied by the model writing the report. This module restates only the |
| 5 | part that is arithmetic, so the labelled set in |
| 6 | ``tests/fixtures/patent_signals_labelled.json`` can be checked against it: |
| 7 | |
| 8 | - may a filing be a signal at all (a first US publication with its text, |
| 9 | filed by an entity known to be the company's, with no contradicting |
| 10 | location); |
| 11 | - does a candidate qualify, given whether it is concrete, which of the two |
| 12 | paths of the bar it meets and what the web check found (a cluster is one |
| 13 | candidate; a tie to a story in the report never qualifies one); |
| 14 | - is the section written, and with how many signals, given a company's |
| 15 | candidates and how many jobs signals the same report carries; |
| 16 | - was the decision made properly: the strongest two concrete candidates (in |
| 17 | the order listed, strongest first) were web-checked before either was |
| 18 | dropped, no shipped or reported match was claimed without a check, and no |
| 19 | search ran when nothing was concrete. |
| 20 | |
| 21 | It does NOT test whether a model finds the right filings in a digest or judges |
| 22 | the bar correctly. Nothing here reads a digest. That needs live runs, read by a |
| 23 | person. It is test support, not shipped code. |
| 24 | """ |
| 25 | |
| 26 | from __future__ import annotations |
| 27 | |
| 28 | import re |
| 29 | from typing import Any |
| 30 | |
| 31 | OUT_OF_CHARACTER, UNSHIPPED_PRODUCT = 1, 2 |
| 32 | QUALIFYING_PATHS = frozenset({OUT_OF_CHARACTER, UNSHIPPED_PRODUCT}) |
| 33 | FIRST_PUBLICATION = "first publication" |
| 34 | # Counted in the digest, never listed there, never a signal. |
| 35 | NEVER_SIGNALS = frozenset({"continuation", "divisional", "grant published earlier", "design"}) |
| 36 | MIN_SIGNALS = 1 |
| 37 | MAX_SIGNALS = 2 |
| 38 | # Signal paragraphs across the jobs section and this one; the jobs section goes first. |
| 39 | MAX_COMBINED_SIGNALS = 3 |
| 40 | # Invention families that must share one specific focus to count together as one signal. |
| 41 | MIN_CLUSTER_FAMILIES = 3 |
| 42 | # Filings of a cluster cited in its paragraph, and sentences per paragraph. |
| 43 | CLUSTER_FILINGS_CITED = 2 |
| 44 | PARAGRAPH_SENTENCES = 2 |
| 45 | # Words in one signal paragraph, counted outside its linked filing titles, and |
| 46 | # the longest optional quote from the filing. |
| 47 | MAX_PARAGRAPH_WORDS = 60 |
| 48 | MAX_QUOTE_WORDS = 12 |
| 49 | # Concrete candidates web-checked before the section is written or left out. |
| 50 | MAX_CHECKS = 2 |
| 51 | |
| 52 | _LINK = re.compile(r"\[[^\]]*\]\([^)]*\)") |
| 53 | _SENTENCE_BREAK = re.compile(r"(?<=[.!?])\s+(?=[{A-Z])") |
| 54 | _QUOTE = re.compile(r'"([^"]+)"') |
| 55 | _FILED = re.compile(r"\(filed (?:[A-Z][a-z]+ \d{4}|\{filing month and year\})\)") |
| 56 | # Wording that turns a filing into a plan or a product announcement. |
| 57 | _OVERCLAIM = re.compile( |
| 58 | r"\b(?:is|are) (?:building|developing|launching|shipping)\b" |
| 59 | r"|\bwill (?:ship|launch|release|sell|build)\b" |
| 60 | r"|\bhas decided\b|\bplans to\b", |
| 61 | re.IGNORECASE, |
| 62 | ) |
| 63 | |
| 64 | |
| 65 | def usable(filing: dict[str, Any]) -> bool: |
| 66 | """A first US publication the report may attribute to the company.""" |
| 67 | if filing.get("kind", FIRST_PUBLICATION) != FIRST_PUBLICATION: |
| 68 | return False |
| 69 | if filing.get("not_verified") and filing.get("entity_known") is not True: |
| 70 | return False |
| 71 | return not filing.get("location_contradicts") |
| 72 | |
| 73 | |
| 74 | def families(candidate: dict[str, Any]) -> int: |
| 75 | """Invention families a candidate stands for: a cluster's distinct ones, else one. |
| 76 | |
| 77 | Filings that share one earliest priority date and one filed-as name are |
| 78 | one family. A titles-only member prints neither date, so it cannot be |
| 79 | shown to be a separate family and is not counted. |
| 80 | """ |
| 81 | members = candidate.get("cluster") |
| 82 | if members is None: |
| 83 | return 1 |
| 84 | return len({ |
| 85 | (m["priority"], m["filed_as"]) |
| 86 | for m in members if usable(m) and not m.get("titles_only") |
| 87 | }) |
| 88 | |
| 89 | |
| 90 | def _matched(candidate: dict[str, Any]) -> bool: |
| 91 | """The web check found a matching product: shipped, announced or reported.""" |
| 92 | return candidate.get("shipped_match") is True or candidate.get("reported_match") is True |
| 93 | |
| 94 | |
| 95 | def _unmatched(candidate: dict[str, Any]) -> bool: |
| 96 | """The web check ran and found no matching product of any kind.""" |
| 97 | return candidate.get("shipped_match") is False and candidate.get("reported_match") is not True |
| 98 | |
| 99 | |
| 100 | def paths_met(candidate: dict[str, Any]) -> set[int]: |
| 101 | """The paths that survive the web check: a shipped or reported match removes the second.""" |
| 102 | met = set(candidate["paths"]) & QUALIFYING_PATHS |
| 103 | if _matched(candidate): |
| 104 | met.discard(UNSHIPPED_PRODUCT) |
| 105 | return met |
| 106 | |
| 107 | |
| 108 | def qualifies(candidate: dict[str, Any]) -> bool: |
| 109 | """Out of character or an unshipped product, judged from the filing's text. |
| 110 | |
| 111 | A titles-only entry never qualifies. A cluster qualifies by either path |
| 112 | once it has three families. A tie to a story in the report is never a path. |
| 113 | """ |
| 114 | if not usable(candidate) or candidate.get("titles_only"): |
| 115 | return False |
| 116 | met = paths_met(candidate) |
| 117 | if "cluster" in candidate: |
| 118 | return families(candidate) >= MIN_CLUSTER_FAMILIES and bool(met) |
| 119 | return bool(candidate.get("concrete")) and bool(met) |
| 120 | |
| 121 | |
| 122 | def qualifying(candidates: list[dict[str, Any]]) -> list[dict[str, Any]]: |
| 123 | return [candidate for candidate in candidates if qualifies(candidate)] |
| 124 | |
| 125 | |
| 126 | def check_candidate(candidate: dict[str, Any]) -> bool: |
| 127 | """A concrete candidate whose fate turns on the web check. |
| 128 | |
| 129 | It must be one the report could use: a first US publication with its text, |
| 130 | by an entity known to be the company's, and for a cluster, enough families. |
| 131 | """ |
| 132 | if not usable(candidate) or candidate.get("titles_only") or not candidate.get("concrete"): |
| 133 | return False |
| 134 | return "cluster" not in candidate or families(candidate) >= MIN_CLUSTER_FAMILIES |
| 135 | |
| 136 | |
| 137 | def checks_owed(candidates: list[dict[str, Any]]) -> list[dict[str, Any]]: |
| 138 | """The strongest two concrete candidates; none when nothing is concrete.""" |
| 139 | return [candidate for candidate in candidates if check_candidate(candidate)][:MAX_CHECKS] |
| 140 | |
| 141 | |
| 142 | def decision_valid(candidates: list[dict[str, Any]]) -> bool: |
| 143 | """The candidates were checked as the rule requires before any was dropped. |
| 144 | |
| 145 | Every owed check ran, no other search ran, and no candidate carries a |
| 146 | shipped, reported or coverage finding that no search produced. |
| 147 | """ |
| 148 | owed = checks_owed(candidates) |
| 149 | if not all(candidate.get("checked") for candidate in owed): |
| 150 | return False |
| 151 | if sum(bool(candidate.get("checked")) for candidate in candidates) != len(owed): |
| 152 | return False |
| 153 | return not any( |
| 154 | not candidate.get("checked") |
| 155 | and True in ( |
| 156 | candidate.get("shipped_match"), candidate.get("reported_match"), candidate.get("covered"), |
| 157 | ) |
| 158 | for candidate in candidates |
| 159 | ) |
| 160 | |
| 161 | |
| 162 | def max_section_paragraphs() -> int: |
| 163 | """The lead-in runs into the first signal, so each signal is one paragraph.""" |
| 164 | return MAX_SIGNALS |
| 165 | |
| 166 | |
| 167 | def section_present(candidates: list[dict[str, Any]], *, web_search: bool = True) -> bool: |
| 168 | """One qualifying signal is enough; never without web search.""" |
| 169 | if not web_search: |
| 170 | return False |
| 171 | return len(qualifying(candidates)) >= MIN_SIGNALS |
| 172 | |
| 173 | |
| 174 | def signals_shown( |
| 175 | candidates: list[dict[str, Any]], |
| 176 | *, |
| 177 | jobs_signals: int = 0, |
| 178 | web_search: bool = True, |
| 179 | ) -> int: |
| 180 | """How many patent paragraphs the report has, after the jobs section took its own. |
| 181 | |
| 182 | One by default. Two only when two qualifying signals both had no matching |
| 183 | product on the web. |
| 184 | """ |
| 185 | if not section_present(candidates, web_search=web_search): |
| 186 | return 0 |
| 187 | unmatched = [candidate for candidate in qualifying(candidates) if _unmatched(candidate)] |
| 188 | wanted = MAX_SIGNALS if len(unmatched) >= MAX_SIGNALS else MIN_SIGNALS |
| 189 | return max(MIN_SIGNALS, min(wanted, MAX_COMBINED_SIGNALS - jobs_signals)) |
| 190 | |
| 191 | |
| 192 | def wording(candidate: dict[str, Any]) -> str: |
| 193 | """``public`` when the check found a match or coverage, else what it may say.""" |
| 194 | found = (candidate.get("shipped_match"), candidate.get("covered")) |
| 195 | if True in found or candidate.get("reported_match") is True: |
| 196 | return "public" |
| 197 | if found == (False, False): |
| 198 | return "no coverage found" |
| 199 | return "check first" |
| 200 | |
| 201 | |
| 202 | def standout(candidate: dict[str, Any]) -> bool: |
| 203 | """May also be one KEY PATTERNS line: concrete, qualifying, and the check found nothing.""" |
| 204 | return ( |
| 205 | qualifies(candidate) |
| 206 | and bool(candidate.get("concrete")) |
| 207 | and wording(candidate) == "no coverage found" |
| 208 | ) |
| 209 | |
| 210 | |
| 211 | def sentences(paragraph: str) -> list[str]: |
| 212 | body = paragraph.removeprefix("**Patent Secret Signals** - ") |
| 213 | return [part for part in _SENTENCE_BREAK.split(body) if part] |
| 214 | |
| 215 | |
| 216 | def words(paragraph: str) -> int: |
| 217 | """Words in a paragraph, not counting its linked filing titles or the lead-in.""" |
| 218 | body = paragraph.removeprefix("**Patent Secret Signals** - ") |
| 219 | return len(_LINK.sub("", body).split()) |
| 220 | |
| 221 | |
| 222 | def paragraph_ok(paragraph: str) -> bool: |
| 223 | """A signal paragraph in the plain-words shape. |
| 224 | |
| 225 | Two sentences, at most 60 words outside the linked titles, application or |
| 226 | grant named, the filing linked with its month and year of filing in |
| 227 | parentheses (a cluster cites two filings by publication date instead), any quote at most 12 words, and nothing worded as a plan. |
| 228 | """ |
| 229 | return ( |
| 230 | len(sentences(paragraph)) == PARAGRAPH_SENTENCES |
| 231 | and words(paragraph) <= MAX_PARAGRAPH_WORDS |
| 232 | and bool(re.search(r"\b(?:application|grant)\b", paragraph)) |
| 233 | and bool(_LINK.search(paragraph)) |
| 234 | and (len(_LINK.findall(paragraph)) > 1 or bool(_FILED.search(paragraph))) |
| 235 | and all(len(quote.split()) <= MAX_QUOTE_WORDS for quote in _QUOTE.findall(paragraph)) |
| 236 | and not _OVERCLAIM.search(paragraph) |
| 237 | ) |
| 238 |