lunarflu HF staff commited on
Commit
0d44148
·
verified ·
1 Parent(s): 629b32c

adjusting regex for num_words

Browse files
Files changed (1) hide show
  1. app.py +3 -5
app.py CHANGED
@@ -95,16 +95,14 @@ async def on_message(message):
95
 
96
 
97
  def extract_adjacent_words(content, trigger, num_words=5):
98
- words = content.split()
99
  pattern = r'\b' + r'\b\s*\b'.join(map(re.escape, trigger)) + r'\b'
100
  regex = re.compile(pattern, re.IGNORECASE)
101
 
102
  for match in regex.finditer(content):
103
  start, end = match.span()
104
- before = content[:start].split()[-num_words:]
105
- after = content[end:].split()[:num_words]
106
- print('...' + ' '.join(before + [match.group()] + after) + '...')
107
- return '...' + ' '.join(before + [match.group()] + after) + '...'
108
  return content
109
 
110
 
 
95
 
96
 
97
  def extract_adjacent_words(content, trigger, num_words=5):
 
98
  pattern = r'\b' + r'\b\s*\b'.join(map(re.escape, trigger)) + r'\b'
99
  regex = re.compile(pattern, re.IGNORECASE)
100
 
101
  for match in regex.finditer(content):
102
  start, end = match.span()
103
+ before_words = re.findall(r'\b\w+\b', content[:start])[-num_words:]
104
+ after_words = re.findall(r'\b\w+\b', content[end:end + 100])[:num_words]
105
+ return '...' + ' '.join(before_words + [match.group()] + after_words) + '...'
 
106
  return content
107
 
108