MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / parse_input

Function parse_input

scripts/add_must_read.py:109–159  ·  view source on GitHub ↗

Parse user input to extract operation type and entities

(user_input: str)

Source from the content-addressed store, hash-verified

107
108def parse_input(user_input: str):
109 """Parse user input to extract operation type and entities"""
110
111 # Check for list/query operations first
112 list_patterns = [r'.*必读.*清单.*', r'.*当前.*清单.*', r'.*有什么.*', r'.*看看.*']
113 for pattern in list_patterns:
114 if re.search(pattern, user_input, re.IGNORECASE):
115 return ('list', None, None)
116
117 # Patterns for adding
118 add_patterns = [
119 (r'.*作者.*[::]\s*(.+)', 'add', 'authors'),
120 (r'.*机构.*[::]\s*(.+)', 'add', 'institutions'),
121 (r'.*关键词.*[::]\s*(.+)', 'add', 'keywords'),
122 ]
123 for pattern, op, entity_type in add_patterns:
124 match = re.search(pattern, user_input, re.IGNORECASE)
125 if match:
126 entities = [e.strip() for e in re.split(r'[,,]', match.group(1))]
127 return (op, entity_type, entities)
128
129 # Patterns for removing
130 remove_patterns = [
131 (r'.*删除.*作者.*[::]\s*(.+)', 'remove', 'authors'),
132 (r'.*删除.*机构.*[::]\s*(.+)', 'remove', 'institutions'),
133 (r'.*删除.*关键词.*[::]\s*(.+)', 'remove', 'keywords'),
134 (r'.*去掉.*作者.*[::]\s*(.+)', 'remove', 'authors'),
135 (r'.*去掉.*机构.*[::]\s*(.+)', 'remove', 'institutions'),
136 (r'.*去掉.*关键词.*[::]\s*(.+)', 'remove', 'keywords'),
137 ]
138 for pattern, op, entity_type in remove_patterns:
139 match = re.search(pattern, user_input, re.IGNORECASE)
140 if match:
141 entities = [e.strip() for e in re.split(r'[,,]', match.group(1))]
142 return (op, entity_type, entities)
143
144 # Patterns for updating weights
145 weight_patterns = [
146 (r'.*降低.*?(.+?)?权重.*?(到 | 为 | 至)\s*(.+)', 'update_weight', 'down'),
147 (r'.*提高.*?(.+?)?权重.*?(到 | 为 | 至)\s*(.+)', 'update_weight', 'up'),
148 (r'.*调整.*?(.+?)?权重.*?(到 | 为 | 至)\s*(.+)', 'update_weight', 'set'),
149 (r'.*?权重.*?(到 | 为 | 至)\s*([0-9.]+)', 'update_weight', 'set'),
150 ]
151 for pattern, op, direction in weight_patterns:
152 match = re.search(pattern, user_input, re.IGNORECASE)
153 if match:
154 topic = match.group(1).strip() if match.group(1) else None
155 weight = float(match.group(3).strip())
156 return ('update_weight', direction, {'topic': topic, 'weight': weight})
157
158 return None
159
160
161def add_must_read(profile, entity_type, entities):
162 """Add entities to must-read list"""

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected