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