Carlos Rosas commited on
Commit
d9b94f4
·
verified ·
1 Parent(s): 775e7e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -122,13 +122,25 @@ class pleiasBot:
122
  return None, None, None
123
 
124
  def format_references(text):
125
- ref_pattern = r'<ref name="([^"]+)">"([^"]+)"</ref>\.\s*' # Modified pattern to include the period and whitespace after ref
 
 
126
 
 
 
 
 
 
 
 
 
 
 
127
  parts = []
128
  current_pos = 0
129
- ref_number = 1
130
-
131
- for match in re.finditer(ref_pattern, text):
132
  # Add text before the reference
133
  text_before = text[current_pos:match.start()].rstrip()
134
  parts.append(text_before)
@@ -137,12 +149,11 @@ def format_references(text):
137
  ref_id = match.group(1)
138
  ref_text = match.group(2).strip()
139
 
140
- # Add the reference, keeping the existing structure but adding <br> where whitespace was
141
- tooltip_html = f'<span class="tooltip"><strong>[{ref_number}]</strong><span class="tooltiptext"><strong>{ref_id}</strong>: {ref_text}</span></span>.<br>'
142
  parts.append(tooltip_html)
143
 
144
  current_pos = match.end()
145
- ref_number += 1
146
 
147
  # Add any remaining text
148
  parts.append(text[current_pos:])
 
122
  return None, None, None
123
 
124
  def format_references(text):
125
+ # First, extract all references in order of appearance
126
+ ref_pattern = r'<ref name="([^"]+)">"([^"]+)"</ref>'
127
+ references = re.finditer(ref_pattern, text)
128
 
129
+ # Create a mapping of reference IDs to their order of appearance
130
+ ref_mapping = {}
131
+ ref_number = 1
132
+ for match in references:
133
+ ref_id = match.group(1)
134
+ if ref_id not in ref_mapping:
135
+ ref_mapping[ref_id] = ref_number
136
+ ref_number += 1
137
+
138
+ # Now process the text with the consistent reference numbers
139
  parts = []
140
  current_pos = 0
141
+
142
+ # Use finditer again to process the actual text
143
+ for match in re.finditer(ref_pattern + r'\.\s*', text):
144
  # Add text before the reference
145
  text_before = text[current_pos:match.start()].rstrip()
146
  parts.append(text_before)
 
149
  ref_id = match.group(1)
150
  ref_text = match.group(2).strip()
151
 
152
+ # Use the mapped reference number
153
+ tooltip_html = f'<span class="tooltip"><strong>[{ref_mapping[ref_id]}]</strong><span class="tooltiptext"><strong>{ref_id}</strong>: {ref_text}</span></span>.<br>'
154
  parts.append(tooltip_html)
155
 
156
  current_pos = match.end()
 
157
 
158
  # Add any remaining text
159
  parts.append(text[current_pos:])