Spaces:
Sleeping
Sleeping
ignore consecutive newlines
Browse files
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
|
| 9 |
-
# It inserts the tag *before* the matched text (newline or hash sequence)
|
| 10 |
def replacer(match):
|
| 11 |
-
nonlocal counter
|
| 12 |
tag = f"[INSERT_POINT_{counter:03d}]"
|
| 13 |
counter += 1
|
| 14 |
-
#
|
|
|
|
| 15 |
return tag + match.group(0)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
#
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
processed_text = re.sub(
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
# the
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
-
#
|
| 36 |
-
#
|
| 37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
return processed_text
|
| 40 |
|
| 41 |
-
# --- Gradio Interface Code (
|
| 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"),
|
| 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**
|
|
|
|
| 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__":
|