返回 last30days-skill
company_names.py
根目录 / skills / last30days / scripts / lib / company_names.py
1 """Decide whether a patent applicant or assignee string is a given company.
2
3 Stdlib only, no network. Companies file under legal names ("Apple Inc.",
4 "Microsoft Technology Licensing, LLC"), so names are compared by a key with
5 case, punctuation and trailing legal designators removed. A name that adds
6 only corporate-structure words to the company's key ("OpenAI OpCo",
7 "Intellectual Ventures II") is the company's too.
8 """
9
10 from __future__ import annotations
11
12 import html
13 import re
14 from collections.abc import Iterable
15
16 CONFIRMED = "confirmed"
17 NAME_MATCHED = "name-matched"
18 NEITHER = "neither"
19
20 _DESIGNATORS = frozenset(
21 "inc incorporated corp corporation co company ltd limited llc lc lp llp "
22 "pbc plc gmbh ag sa nv bv ab kk oy sas pte the".split()
23 )
24
25 # Words a company adds to its own name for a holding, licensing or numbered
26 # entity. Two-word phrases count only as a pair: "technology" or "property"
27 # alone is a business line, not structure.
28 _STRUCTURE_WORDS = frozenset("opco holdco holding holdings group ip".split())
29 _STRUCTURE_PHRASES = (("technology", "licensing"), ("intellectual", "property"))
30 # Bare numerals, and roman numerals up to 39, so ordinary words such as "mix"
31 # or "civil" are not read as numbers.
32 _NUMERAL = re.compile(r"\d+|(?=[ivx])x{0,3}(?:ix|iv|v?i{0,3})")
33
34
35 def _structure_only(words: list[str]) -> bool:
36 """True when ``words`` is one or more corporate-structure words."""
37 if not words:
38 return False
39 i = 0
40 while i < len(words):
41 pair = tuple(words[i:i + 2])
42 if pair in _STRUCTURE_PHRASES:
43 i += 2
44 elif words[i] in _STRUCTURE_WORDS or _NUMERAL.fullmatch(words[i]):
45 i += 1
46 else:
47 return False
48 return True
49
50
51 def name_key(name: str | None) -> str:
52 """A name for comparison: case-folded, HTML entities decoded, punctuation
53 removed, whitespace collapsed, trailing legal designators dropped. The
54 last remaining word is never dropped."""
55 if not name:
56 return ""
57 text = html.unescape(str(name)).casefold()
58 text = re.sub(r"[.'’]", "", text)
59 words = re.sub(r"[^\w\s]|_", " ", text).split()
60 while len(words) > 1 and words[-1] in _DESIGNATORS:
61 del words[-1]
62 return " ".join(words)
63
64
65 def _usable_key(name: str | None) -> str:
66 """The key of a name, or "" when nothing but designators is left."""
67 key = name_key(name)
68 if not key or key in _DESIGNATORS:
69 return ""
70 return key
71
72
73 def classify_applicant(
74 applicant: str | None,
75 company: str | None,
76 known_names: Iterable[str | None] | None = None,
77 ) -> str:
78 """Return CONFIRMED, NAME_MATCHED or NEITHER for an applicant string.
79
80 CONFIRMED: the applicant's key equals the company's key or that of a known
81 name, or is one of those keys followed only by corporate-structure words
82 (opco, holdco, holding, holdings, group, technology licensing, ip,
83 intellectual property, numerals). NAME_MATCHED: the key starts with one of
84 those keys followed by at least one other whole word. NEITHER: anything
85 else.
86 """
87 applicant_key = _usable_key(applicant)
88 if not applicant_key:
89 return NEITHER
90 keys = {_usable_key(company)}
91 keys.update(_usable_key(name) for name in known_names or ())
92 keys.discard("")
93 if applicant_key in keys:
94 return CONFIRMED
95 extended = [
96 applicant_key[len(key) + 1:].split()
97 for key in keys
98 if applicant_key.startswith(key + " ")
99 ]
100 if any(_structure_only(rest) for rest in extended):
101 return CONFIRMED
102 if extended:
103 return NAME_MATCHED
104 return NEITHER
105
105 lines PYTHON