-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathname_extractor.py
More file actions
45 lines (33 loc) · 800 Bytes
/
name_extractor.py
File metadata and controls
45 lines (33 loc) · 800 Bytes
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
import re
FILLER_PHRASES = [
"my name is",
"this is",
"i am",
"its",
"it's",
"hello",
"hi"
]
INVALID_WORDS = {
"yes", "no", "correct", "right", "bye", "and", "return"
}
def extract_name(text: str):
t = text.lower()
for p in FILLER_PHRASES:
t = t.replace(p, "")
# remove punctuation
t = re.sub(r"[^a-zA-Z\s]", "", t)
words = [w for w in t.split() if w not in INVALID_WORDS]
if not words:
return None
# take last 1–3 words
name_words = words[-3:]
name = " ".join(name_words)
if len(name) < 2:
return None
return name.title()
def extract_spelled_name(text: str):
letters = re.findall(r"[A-Za-z]", text)
if len(letters) >= 2:
return "".join(letters).upper()
return None