-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
158 lines (121 loc) · 6.31 KB
/
parser.py
File metadata and controls
158 lines (121 loc) · 6.31 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def main(current_query: str, previous_query: str) -> dict:
import re
current_query_cleaned = current_query.lower().strip()
previous_query_cleaned = previous_query.lower().strip()
followup_patterns = {
# pattern_type : pattern
# "what about X??" --> X grouped and extracted
'what_about': r'^what about\s+(.+?)\?*$',
'how_about': r'^how about\s(.+?)\?*$',
'and': r'^and\s(.+?)\?*$',
'what_if': r'^what if\s(.+?)\?*$',
# "for X?" "in X?" "with X?"
'preposition': r'^(for|in|with|on|at|from|to)\s+(.+?)\?*$',
# "X instead?" "X though?"
'alternative': r'^(.+?)\s+(instead|though|however)\?*$',
# single entities, 1-2 word follow-up questions like "Japan? "
'single_entity': r'^([a-z0-9\s\-_]+)\?*$'
}
extracted_query = None
pattern_type = None
# classify the current_query's followup pattern type, then extract the core of the question
# if it's not a followup question, exit
for p_type, pattern in followup_patterns.items():
match = re.match(pattern, current_query_cleaned)
if match:
# start with the most verbose branch
if p_type == 'single_entity':
candidate = match.group(1).strip()
question_words = ['what', 'how', 'why', 'when', 'where', 'who', 'which', 'is', 'are', 'do', 'does', 'can', 'will']
candidate_words = candidate.split()
has_question_word = any(word in question_words for word in candidate_words)
if len(candidate_words) <= 3 and not has_question_word:
extracted_query = candidate
pattern_type = p_type
elif p_type == 'preposition':
extracted_query = match.group(2).strip()
pattern_type = p_type
elif p_type == 'alternative':
extracted_query = match.group(1).strip()
pattern_type = p_type
else:
extracted_query = match.group(1).strip()
pattern_type = p_type
break
if not extracted_query:
return {
"fused_query": current_query
}
# detecting previous question pattern, preparing to replace core of old question with core of new question in {entity}
question_patterns = [
# What _?
(r'^(what\s+(?:is|are|was|were|will|would|can|could|should|do|does|did))\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# How _?
(r'^(how\s+(?:do|does|did|can|could|should|would|will|to|is|are|was|were))\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# Why _?
(r'^(why\s+(?:is|are|was|were|do|does|did|can|could|should|would|will))\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# When _?
(r'^(when\s+(?:is|are|was|were|do|does|did|can|could|should|would|will))\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# Where _?
(r'^(where\s+(?:is|are|was|were|do|does|did|can|could|should|would|will))\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# Who _?
(r'^(who\s+(?:is|are|was|were|do|does|did|can|could|should|would|will))\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# Which _?
(r'^(which\s+.+?\s+(?:is|are|was|were|do|does|did|can|could|should|would|will))\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# Show/Tell me _?
(r'^(show\s+me|tell\s+me|give\s+me)\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# Can/Could _?
(r'^(can|could|would|should|will)\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
# Simple verb questions (is, are, do, does, etc.)
(r'^(is|are|was|were|do|does|did)\s+(.+?)(\s+(?:for|in|with|of|about|from|to)\s+.+?)?(\??)$',
r'\1 \2 for {entity}\4'),
]
fused_query = None
# get corresponding pattern and template by matching with old query, then replace
for pattern, template in question_patterns:
match = re.match(pattern, previous_query_cleaned)
if match:
# replace
fused_query = re.sub(pattern, template, previous_query_cleaned)
fused_query = fused_query.replace('{entity}', extracted_query)
# previous_query doesn't match any standard question pattern
if not fused_query:
# find the last significant entity in previous_query and replace it ("Accommodation limit for Japan" + "Indonesia?" --> "Accommodation limit for Indonesia?")
patterns = [
r'\b([A-Z][a-zA-Z]+(?:\s+[A-Z][a-zA-Z]+)*)\b(?=\s*\??$)', # proper nouns at end
r'\b(\w+)\s*\??$', # last word
]
for pattern in patterns:
match = re.search(pattern, previous_query_cleaned)
if match:
old_entity = match.group(1)
fused_query = previous_query_cleaned.replace(old_entity, extracted_query, 1)
break
# final try for not fused_query
if not fused_query:
# "Best place to stay?" + "Japan?" = "Best place to stay for Japan?"
if '?' in previous_query_cleaned:
fused_query = previous_query_cleaned.replace('?', f' for {extracted_query}?')
else:
fused_query = f"{previous_query_cleaned} for {extracted_query}"
# clean up result
if fused_query:
# fix capitalization
fused_query = fused_query[0].upper() + fused_query[1:] if fused_query else fused_query
# remove extra spaces
fused_query = re.sub(r'\s+', ' ', fused_query).strip()
# question mark check
if previous_query_cleaned.endswith('?') and not fused_query.endswith('?'):
fused_query += '?'
return {
"fused_query": fused_query or current_query_cleaned
}