-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputParse.py
More file actions
60 lines (49 loc) · 1.49 KB
/
inputParse.py
File metadata and controls
60 lines (49 loc) · 1.49 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
import re
import spacy
nlp = spacy.load("en_core_web_sm")
DEST_KEYWORDS = {
"downtown": "Downtown",
"long beach": "Long Beach",
"santa monica": "Santa Monica",
"usc": "USC",
"union station": "Union Station",
}
def parse_user_query(query: str):
doc = nlp(query)
text = query.lower()
result = {
"intent": None,
"bus_number": None,
"destination": None,
"time": None,
}
# basic intent
if "next" in text and "bus" in text:
result["intent"] = "get_next_bus"
elif "leave" in text or "when should i" in text:
result["intent"] = "plan_departure"
elif "delay" in text or "late" in text:
result["intent"] = "check_delay"
else:
result["intent"] = "unknown"
# bus number: "bus 70", "route 232", "take 720"
m = re.search(r"\b(?:bus|line|route)?\s*(\d{1,4})\b", text)
if m:
result["bus_number"] = int(m.group(1))
# time from entities
for ent in doc.ents:
if ent.label_ in ("TIME", "DATE"):
result["time"] = ent.text
break
# destination from entities
for ent in doc.ents:
if ent.label_ in ("GPE", "LOC", "FAC", "FACILITY"):
result["destination"] = ent.text
break
# fallback: keyword destinations like "downtown"
if not result["destination"]:
for key, pretty in DEST_KEYWORDS.items():
if key in text:
result["destination"] = pretty
break
return result