-
-
Notifications
You must be signed in to change notification settings - Fork 793
Expand file tree
/
Copy pathpatents.py
More file actions
66 lines (54 loc) · 1.69 KB
/
Copy pathpatents.py
File metadata and controls
66 lines (54 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import re
# Keywords that indicate patent-related references
PATENT_KEYWORDS = [
"patent pending",
"patented",
"patent application",
"patent number",
]
# Precompile keyword regex patterns (case-insensitive)
KEYWORD_REGEXES = [
re.compile(rf"\b{re.escape(keyword)}\b", re.IGNORECASE)
for keyword in PATENT_KEYWORDS
]
# Regex for patent numbers and international formats
PATENT_NUMBER_REGEX = re.compile(
r"""
\b
(?:
(?:US|EP|WO|JP|CN|KR|GB|IN) # Country codes
\s*
(?:Patent(?:\s+No\.?)?\s*)? # Optional 'Patent' or 'Patent No.'
\d+(?:[,\/]\d+)* # Number part (allow commas/slashes)
\s*(?:A1|A2|B1|B2)? # Optional kind codes
)
\b
""",
re.IGNORECASE | re.VERBOSE,
)
def find_patents(location):
"""
Detect patent references and patent-related keywords in a file.
Return a list of tuples:
(kind, value, line_number)
where:
kind: "number" or "keyword"
value: matched text (original casing preserved)
line_number: line where match occurred
"""
results = []
try:
with open(location, "r", errors="ignore") as f:
lines = f.readlines()
except Exception:
return results
for line_num, line in enumerate(lines, start=1):
# Detect patent numbers
for match in PATENT_NUMBER_REGEX.finditer(line):
results.append(("number", match.group().strip(), line_num))
# Detect keyword references
for regex in KEYWORD_REGEXES:
match = regex.search(line)
if match:
results.append(("keyword", match.group(), line_num))
return results