BtB-ExpC commited on
Commit
f414276
·
1 Parent(s): 96bc994

ignore consecutive newlines

Browse files
Files changed (1) hide show
  1. app.py +39 -31
app.py CHANGED
@@ -2,43 +2,49 @@ import re
2
  import gradio as gr
3
 
4
  def insert_points(text):
5
- # Initialize a counter accessible by the replacer function
6
  counter = 1
7
 
8
- # This function will be called for each match found by re.sub
9
- # It inserts the tag *before* the matched text (newline or hash sequence)
10
  def replacer(match):
11
- nonlocal counter # Use the counter from the outer scope
12
  tag = f"[INSERT_POINT_{counter:03d}]"
13
  counter += 1
14
- # match.group(0) contains the actual matched string ('\n' or '##' etc.)
 
15
  return tag + match.group(0)
16
 
17
- # The pattern looks for either:
18
- # 1) A newline character ('\n')
19
- # 2) A sequence of one or more '#' characters ('\#+')
20
- # '#' needs to be escaped ('\#') because it's a special regex character.
21
- # The parentheses create capturing groups, but match.group(0) gives the whole match anyway.
22
- pattern = r'(\n|\#+)'
23
-
24
- # Use re.sub to find all matches of the pattern and replace them
25
- # by calling the 'replacer' function for each match.
26
- processed_text = re.sub(pattern, replacer, text)
27
-
28
- # One edge case: If the *very beginning* of the text starts with '#',
29
- # the regex above won't match anything *before* it.
30
- # We need to check if the text starts with hashes (possibly after whitespace)
31
- # and prepend the first tag if necessary.
32
- # However, the current `re.sub(pattern, replacer, text)` already handles this
33
- # correctly because it finds the '#' sequence at the beginning and the
34
- # replacer adds the tag *before* it. Let's re-verify.
35
- # Example: If text is "## Title", pattern finds "##" at index 0.
36
- # Replacer runs, returns "[INSERT_POINT_001]##". Result is correct.
37
- # So, no special handling for the beginning is needed with this pattern.
 
 
 
 
 
 
 
38
 
39
  return processed_text
40
 
41
- # --- Gradio Interface Code (Unchanged from your original) ---
42
  demo = gr.Interface(
43
  fn=insert_points,
44
  inputs=gr.Textbox(
@@ -46,13 +52,15 @@ demo = gr.Interface(
46
  placeholder="Paste your text here...",
47
  label="Your Input Text"
48
  ),
49
- outputs=gr.Textbox(label="Processed Text with Tags"), # Changed output type to Textbox for better viewing
50
  title="Insert Point Tagger",
51
- # Updated description for clarity
52
  description=(
53
- "Paste a block of text and get '[INSERT_POINT_###]' tags added "
54
- "1) **before** every newline, and 2) **before** every '#' sequence (heading)."
 
55
  ),
 
 
56
  )
57
 
58
  if __name__ == "__main__":
 
2
  import gradio as gr
3
 
4
  def insert_points(text):
 
5
  counter = 1
6
 
7
+ # This function will be called for each match by re.sub in the first pass
 
8
  def replacer(match):
9
+ nonlocal counter
10
  tag = f"[INSERT_POINT_{counter:03d}]"
11
  counter += 1
12
+ # Return the tag followed by the original matched text
13
+ # group(0) will be one or more newlines OR one or more hashes
14
  return tag + match.group(0)
15
 
16
+ # --- Step 1: Insert tags before hash sequences and consolidated newline sequences ---
17
+ # Pattern matches:
18
+ # 1) '\n+' : One or more consecutive newline characters. This handles basic blank lines.
19
+ # To handle lines with only whitespace, a more complex pattern like
20
+ # '(\s*\n)+\s*' might be needed, but let's stick to '\n+' based on the example.
21
+ # This change ensures that \n\n or \n\n\n only trigger ONE tag.
22
+ # 2) '\#+' : One or more consecutive '#' characters.
23
+ # This pattern might still create "[TAG1]\n[TAG2]###" if a newline immediately precedes a heading.
24
+ pattern_initial = r'(\n+|\#+)'
25
+ processed_text = re.sub(pattern_initial, replacer, text)
26
+
27
+ # --- Step 2: Clean up potential heading splits ---
28
+ # This step addresses the case where Step 1 resulted in:
29
+ # [INSERT_POINT_XXX]<whitespace like \n>[INSERT_POINT_YYY]### Heading
30
+ # We want to remove the first tag and the intermediate whitespace, keeping the tag associated with the ###.
31
+
32
+ # The cleanup pattern finds:
33
+ # (\[INSERT_POINT_\d{3}\]) : Capture Group 1: The tag before the newline/whitespace (e.g., TAG_XXX)
34
+ # \s* : Any intermediate whitespace (importantly, including the newline)
35
+ # (\[INSERT_POINT_\d{3}\]) : Capture Group 2: The tag right before the hashes (e.g., TAG_YYY)
36
+ # (\#+) : Capture Group 3: The actual hash sequence (e.g., ###)
37
+ cleanup_pattern = r'(\[INSERT_POINT_\d{3}\])\s*(\[INSERT_POINT_\d{3}\])(\#+)'
38
+
39
+ # The replacement uses:
40
+ # \2 : Capture Group 2 (the tag we want to keep, TAG_YYY)
41
+ # \3 : Capture Group 3 (the hash sequence)
42
+ # This effectively deletes the first tag (Group 1) and the intermediate whitespace.
43
+ processed_text = re.sub(cleanup_pattern, r'\2\3', processed_text)
44
 
45
  return processed_text
46
 
47
+ # --- Gradio Interface Code (Updated Description) ---
48
  demo = gr.Interface(
49
  fn=insert_points,
50
  inputs=gr.Textbox(
 
52
  placeholder="Paste your text here...",
53
  label="Your Input Text"
54
  ),
55
+ outputs=gr.Textbox(label="Processed Text with Tags"),
56
  title="Insert Point Tagger",
 
57
  description=(
58
+ "Paste a block of text and get '[INSERT_POINT_###]' tags added:\n"
59
+ "1) **before** each sequence of one or more '#' characters (headings).\n"
60
+ "2) **before** each sequence of one or more newline characters (e.g., one tag for line breaks or blank lines)."
61
  ),
62
+ # You might add allow_flagging='never' if you don't need the flagging feature
63
+ # allow_flagging='never'
64
  )
65
 
66
  if __name__ == "__main__":